From 843329de434bb334d90927c4d22345373a388530 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 2 Jul 2019 18:05:22 -0400 Subject: Rename package root The package root is now bjc, not io.github.bculkin2442. --- src/main/java/bjc/data/internals/BoundLazy.java | 137 +++++++++++++ .../java/bjc/data/internals/BoundLazyPair.java | 228 +++++++++++++++++++++ .../java/bjc/data/internals/BoundListHolder.java | 81 ++++++++ .../java/bjc/data/internals/HalfBoundLazyPair.java | 177 ++++++++++++++++ src/main/java/bjc/data/internals/WrappedLazy.java | 83 ++++++++ .../java/bjc/data/internals/WrappedOption.java | 96 +++++++++ 6 files changed, 802 insertions(+) create mode 100644 src/main/java/bjc/data/internals/BoundLazy.java create mode 100644 src/main/java/bjc/data/internals/BoundLazyPair.java create mode 100644 src/main/java/bjc/data/internals/BoundListHolder.java create mode 100644 src/main/java/bjc/data/internals/HalfBoundLazyPair.java create mode 100644 src/main/java/bjc/data/internals/WrappedLazy.java create mode 100644 src/main/java/bjc/data/internals/WrappedOption.java (limited to 'src/main/java/bjc/data/internals') diff --git a/src/main/java/bjc/data/internals/BoundLazy.java b/src/main/java/bjc/data/internals/BoundLazy.java new file mode 100644 index 0000000..728af8e --- /dev/null +++ b/src/main/java/bjc/data/internals/BoundLazy.java @@ -0,0 +1,137 @@ +package bjc.data.internals; + +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +import bjc.data.IHolder; +import bjc.data.Lazy; +import bjc.funcdata.FunctionalList; +import bjc.funcdata.IList; + +/** + * Implements a lazy holder that has been bound. + * + * @author Ben Culkin + */ +@SuppressWarnings("javadoc") +public class BoundLazy implements IHolder { + /* The old value. */ + private final Supplier> oldSupplier; + + /* The function to use to transform the old value into a new value. */ + private final Function> binder; + + /* The bound value being held. */ + private IHolder boundHolder; + + /* Whether the bound value has been actualized or not. */ + private boolean holderBound; + + /* Transformations currently pending on the bound value. */ + private final IList> actions = new FunctionalList<>(); + + /** + * Create a new bound lazy value. + * + * @param supp + * The supplier of the old value. + * + * @param binder + * The function to use to bind the old value to the new one. + */ + public BoundLazy(final Supplier> supp, + final Function> binder) { + oldSupplier = supp; + this.binder = binder; + } + + @Override + public IHolder bind(final Function> bindr) { + if(bindr == null) throw new NullPointerException("Binder must not be null"); + + /* Prepare a list of pending actions. */ + final IList> pendingActions = new FunctionalList<>(); + actions.forEach(pendingActions::add); + + /* Create the new supplier of a value. */ + final Supplier> typeSupplier = () -> { + IHolder oldHolder = boundHolder; + + /* Bind the value if it hasn't been bound before. */ + if(!holderBound) { + oldHolder = oldSupplier.get().unwrap(binder); + } + + /* Apply all the pending actions. */ + return pendingActions.reduceAux(oldHolder, (action, state) -> { + return state.transform(action); + }, (value) -> value); + }; + + return new BoundLazy<>(typeSupplier, bindr); + } + + @Override + public Function> lift( + final Function func) { + if(func == null) throw new NullPointerException("Function to lift must not be null"); + + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override + public IHolder map(final Function mapper) { + if(mapper == null) throw new NullPointerException("Mapper must not be null"); + + /* Prepare a list of pending actions. */ + final IList> pendingActions = new FunctionalList<>(); + actions.forEach(pendingActions::add); + + /* Prepare the new supplier. */ + final Supplier typeSupplier = () -> { + IHolder oldHolder = boundHolder; + + /* Bound the value if it hasn't been bound. */ + if(!holderBound) { + oldHolder = oldSupplier.get().unwrap(binder); + } + + /* Apply pending actions. */ + return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> { + return action.apply(state); + }, (value) -> mapper.apply(value)); + }; + + return new Lazy<>(typeSupplier); + } + + @Override + public String toString() { + if(holderBound) return boundHolder.toString(); + + return "(unmaterialized)"; + } + + @Override + public IHolder transform(final UnaryOperator transformer) { + if(transformer == null) throw new NullPointerException("Transformer must not be null"); + + actions.add(transformer); + + return this; + } + + @Override + public UnwrappedType unwrap(final Function unwrapper) { + if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + + if(!holderBound) { + boundHolder = oldSupplier.get().unwrap(binder::apply); + } + + return boundHolder.unwrap(unwrapper); + } +} diff --git a/src/main/java/bjc/data/internals/BoundLazyPair.java b/src/main/java/bjc/data/internals/BoundLazyPair.java new file mode 100644 index 0000000..105b410 --- /dev/null +++ b/src/main/java/bjc/data/internals/BoundLazyPair.java @@ -0,0 +1,228 @@ +package bjc.data.internals; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +import bjc.data.IHolder; +import bjc.data.IPair; +import bjc.data.Identity; +import bjc.data.LazyPair; + +/** + * Implements a lazy pair that has been bound. + * + * @author Ben Culkin + */ +@SuppressWarnings("javadoc") +public class BoundLazyPair implements IPair { + /* The supplier of the left value. */ + private final Supplier leftSupplier; + /* The supplier of the right value. */ + private final Supplier rightSupplier; + + /* The binder to transform values. */ + private final BiFunction> binder; + + /* The bound pair. */ + private IPair boundPair; + + /* Whether the pair has been bound yet. */ + private boolean pairBound; + + /** + * Create a new bound lazy pair. + * + * @param leftSupp + * The supplier for the left value. + * + * @param rightSupp + * The supplier for the right value. + * + * @param bindr + * The function to use to bind the left and right into a new + * pair. + */ + public BoundLazyPair(final Supplier leftSupp, final Supplier rightSupp, + final BiFunction> bindr) { + leftSupplier = leftSupp; + rightSupplier = rightSupp; + binder = bindr; + } + + @Override + public IPair bind( + final BiFunction> bindr) { + if(bindr == null) throw new NullPointerException("Binder must not be null"); + + final IHolder> newPair = new Identity<>(boundPair); + final IHolder newPairMade = new Identity<>(pairBound); + + final Supplier leftSupp = () -> { + if(!newPairMade.getValue()) { + /* + * If the pair hasn't been bound before, bind + * it. + */ + newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); + + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + final Supplier rightSupp = () -> { + if(!newPairMade.getValue()) { + /* + * If the pair hasn't been bound before, bind + * it. + */ + newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); + + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getRight()); + }; + + return new BoundLazyPair<>(leftSupp, rightSupp, bindr); + } + + @Override + public IPair bindLeft( + final Function> leftBinder) { + if(leftBinder == null) throw new NullPointerException("Left binder must not be null"); + + final Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if(!pairBound) { + /* + * If the pair hasn't been bound before, bind + * it. + */ + newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); + } + + return newPair.getLeft(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + final Function> rightBinder) { + if(rightBinder == null) throw new NullPointerException("Right binder must not be null"); + + final Supplier rightSupp = () -> { + IPair newPair = boundPair; + + if(!pairBound) { + /* + * If the pair hasn't been bound before, bind + * it. + */ + newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair combine( + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { + if(otherPair == null) { + throw new NullPointerException("Other pair must not be null"); + } else if(leftCombiner == null) { + throw new NullPointerException("Left combiner must not be null"); + } else if(rightCombiner == null) { + throw new NullPointerException("Right combiner must not be null"); + } + + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(cLeft, cRight); + }); + }); + } + + @Override + public IPair mapLeft(final Function mapper) { + if(mapper == null) throw new NullPointerException("Mapper must not be null"); + + final Supplier leftSupp = () -> { + if(!pairBound) { + final NewLeft leftVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); + + return mapper.apply(leftVal); + } + + return mapper.apply(boundPair.getLeft()); + }; + + final Supplier rightSupp = () -> { + if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); + + return boundPair.getRight(); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public IPair mapRight(final Function mapper) { + if(mapper == null) throw new NullPointerException("Mapper must not be null"); + + final Supplier leftSupp = () -> { + if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); + + return boundPair.getLeft(); + }; + + final Supplier rightSupp = () -> { + if(!pairBound) { + final NewRight rightVal = binder.apply(leftSupplier.get(), rightSupplier.get()) + .getRight(); + + return mapper.apply(rightVal); + } + + return mapper.apply(boundPair.getRight()); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public MergedType merge(final BiFunction merger) { + if(merger == null) throw new NullPointerException("Merger must not be null"); + + if(!pairBound) { + /* + * If the pair isn't bound yet, bind it. + */ + boundPair = binder.apply(leftSupplier.get(), rightSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } + + @Override + public String toString() { + if(pairBound) return boundPair.toString(); + + return "(un-materialized)"; + } +} diff --git a/src/main/java/bjc/data/internals/BoundListHolder.java b/src/main/java/bjc/data/internals/BoundListHolder.java new file mode 100644 index 0000000..8f8cab4 --- /dev/null +++ b/src/main/java/bjc/data/internals/BoundListHolder.java @@ -0,0 +1,81 @@ +package bjc.data.internals; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.data.IHolder; +import bjc.data.ListHolder; +import bjc.funcdata.IList; + +/** + * Holds a list, converted into a holder. + * + * @author Ben Culkin + */ +@SuppressWarnings("javadoc") +public class BoundListHolder implements IHolder { + /* The list of contained holders. */ + private final IList> heldHolders; + + /** + * Create a new list of holders. + * + * @param toHold + * The list of holders to, well, hold. + */ + public BoundListHolder(final IList> toHold) { + heldHolders = toHold; + } + + @Override + public IHolder bind(final Function> binder) { + if(binder == null) throw new NullPointerException("Binder must not be null"); + + final IList> boundHolders = heldHolders.map((containedHolder) -> { + return containedHolder.bind(binder); + }); + + return new BoundListHolder<>(boundHolders); + } + + @Override + public Function> lift(final Function func) { + if(func == null) throw new NullPointerException("Function to lift must not be null"); + + return (val) -> { + return new ListHolder<>(func.apply(val)); + }; + } + + @Override + public IHolder map(final Function mapper) { + if(mapper == null) throw new NullPointerException("Mapper must not be null"); + + final IList> mappedHolders = heldHolders.map((containedHolder) -> { + return containedHolder.map(mapper); + }); + + return new BoundListHolder<>(mappedHolders); + } + + @Override + public IHolder transform(final UnaryOperator transformer) { + if(transformer == null) throw new NullPointerException("Transformer must not be null"); + + heldHolders.forEach((containedHolder) -> { + containedHolder.transform(transformer); + }); + + return this; + } + + @Override + public UnwrappedType unwrap(final Function unwrapper) { + if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + + /* + * @NOTE Is there another way we could want to do this? + */ + return heldHolders.randItem().unwrap(unwrapper); + } +} diff --git a/src/main/java/bjc/data/internals/HalfBoundLazyPair.java b/src/main/java/bjc/data/internals/HalfBoundLazyPair.java new file mode 100644 index 0000000..4f28012 --- /dev/null +++ b/src/main/java/bjc/data/internals/HalfBoundLazyPair.java @@ -0,0 +1,177 @@ +package bjc.data.internals; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +import bjc.data.IHolder; +import bjc.data.IPair; +import bjc.data.Identity; +import bjc.data.LazyPair; + +/* + * @NOTE + * I am not convinced that this code works correctly. Tests should be + * written to make sure things only ever get instantiated once. + * + * Namely, my main concern is to whether the places that bind the pair + * without setting pairBound are doing the right thing. + */ +/** + * A lazy pair, with only one side bound. + * + * @author Ben Culkin + */ +@SuppressWarnings("javadoc") +public class HalfBoundLazyPair implements IPair { + /* The supplier of the old value. */ + private final Supplier oldSupplier; + + /* The function to transform the old value into a new pair. */ + private final Function> binder; + + /* The new bound pair. */ + private IPair boundPair; + /* Has the pair been bound yet or not? */ + private boolean pairBound; + + /** + * Create a new half-bound lazy pair. + * + * @param oldSupp + * The supplier of the old value. + * + * @param bindr + * The function to use to create the pair from the old value. + */ + public HalfBoundLazyPair(final Supplier oldSupp, + final Function> bindr) { + oldSupplier = oldSupp; + binder = bindr; + } + + @Override + public IPair bind( + final BiFunction> bindr) { + final IHolder> newPair = new Identity<>(boundPair); + final IHolder newPairMade = new Identity<>(pairBound); + + final Supplier leftSupp = () -> { + if(!newPairMade.getValue()) { + /* Bind the pair if it hasn't been bound yet. */ + newPair.replace(binder.apply(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + final Supplier rightSupp = () -> { + if(!newPairMade.getValue()) { + /* Bind the pair if it hasn't been bound yet. */ + newPair.replace(binder.apply(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getRight()); + }; + + return new BoundLazyPair<>(leftSupp, rightSupp, bindr); + } + + @Override + public IPair bindLeft( + final Function> leftBinder) { + final Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if(!pairBound) { + newPair = binder.apply(oldSupplier.get()); + } + + return newPair.getLeft(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + final Function> rightBinder) { + final Supplier rightSupp = () -> { + IPair newPair = boundPair; + + if(!pairBound) { + newPair = binder.apply(oldSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair combine( + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(cLeft, cRight); + }); + }); + } + + @Override + public IPair mapLeft(final Function mapper) { + final Supplier leftSupp = () -> { + if(pairBound) return mapper.apply(boundPair.getLeft()); + + final NewLeft leftVal = binder.apply(oldSupplier.get()).getLeft(); + + return mapper.apply(leftVal); + }; + + final Supplier rightSupp = () -> { + if(pairBound) return boundPair.getRight(); + + return binder.apply(oldSupplier.get()).getRight(); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public IPair mapRight(final Function mapper) { + final Supplier leftSupp = () -> { + if(pairBound) return boundPair.getLeft(); + + return binder.apply(oldSupplier.get()).getLeft(); + }; + + final Supplier rightSupp = () -> { + if(pairBound) return mapper.apply(boundPair.getRight()); + + final NewRight rightVal = binder.apply(oldSupplier.get()).getRight(); + + return mapper.apply(rightVal); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public MergedType merge(final BiFunction merger) { + if(!pairBound) { + boundPair = binder.apply(oldSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } +} diff --git a/src/main/java/bjc/data/internals/WrappedLazy.java b/src/main/java/bjc/data/internals/WrappedLazy.java new file mode 100644 index 0000000..17d2309 --- /dev/null +++ b/src/main/java/bjc/data/internals/WrappedLazy.java @@ -0,0 +1,83 @@ +package bjc.data.internals; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.data.IHolder; +import bjc.data.Lazy; + +/** + * A wrapped lazy value. + * + * @author Ben Culkin + * @param + * The type of the wrapped value. + */ +public class WrappedLazy implements IHolder { + /* Held value. */ + private final IHolder> held; + + /** + * Create a new wrapped lazy value. + * + * @param wrappedHolder + * The holder to make lazy. + */ + public WrappedLazy(final IHolder wrappedHolder) { + held = new Lazy<>(wrappedHolder); + } + + /* + * This has an extra parameter, because otherwise it erases to the same + * as the public one. + * + * This is a case where reified generics would be useful, because then + * the compiler could know which one we meant without the dummy + * parameter. + */ + private WrappedLazy(final IHolder> wrappedHolder, + @SuppressWarnings("unused") final boolean dummy) { + held = wrappedHolder; + } + + @Override + public IHolder bind(final Function> binder) { + final IHolder> newHolder = held.map((containedHolder) -> { + return containedHolder.bind(binder); + }); + + return new WrappedLazy<>(newHolder, false); + } + + @Override + public Function> lift(final Function func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override + public IHolder map(final Function mapper) { + final IHolder> newHolder = held.map((containedHolder) -> { + return containedHolder.map(mapper); + }); + + return new WrappedLazy<>(newHolder, false); + } + + @Override + public IHolder transform(final UnaryOperator transformer) { + held.transform((containedHolder) -> { + return containedHolder.transform(transformer); + }); + + return this; + } + + @Override + public UnwrappedType unwrap(final Function unwrapper) { + return held.unwrap((containedHolder) -> { + return containedHolder.unwrap(unwrapper); + }); + } +} diff --git a/src/main/java/bjc/data/internals/WrappedOption.java b/src/main/java/bjc/data/internals/WrappedOption.java new file mode 100644 index 0000000..eb4d120 --- /dev/null +++ b/src/main/java/bjc/data/internals/WrappedOption.java @@ -0,0 +1,96 @@ +package bjc.data.internals; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.data.IHolder; +import bjc.data.Option; + +/** + * A wrapped optional value. + * + * @author Ben Culkin. + * @param + * The wrapped type. + */ +public class WrappedOption implements IHolder { + /* The held value. */ + private final IHolder> held; + + /** + * Create a new wrapped option. + * + * @param seedValue + * The value to wrap. + */ + public WrappedOption(final IHolder seedValue) { + held = new Option<>(seedValue); + } + + /* + * The dummy parameter is to ensure the compiler can pick the right + * method, because without this method erases to the same type as the + * public one. + */ + private WrappedOption(final IHolder> toHold, + @SuppressWarnings("unused") final boolean dummy) { + held = toHold; + } + + @Override + public IHolder bind(final Function> binder) { + final IHolder> newHolder = held.map((containedHolder) -> { + return containedHolder.bind((containedValue) -> { + if(containedValue == null) return new Option<>(null); + + return binder.apply(containedValue); + }); + }); + + return new WrappedOption<>(newHolder, false); + } + + @Override + public Function> lift(final Function func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + + @Override + public IHolder map(final Function mapper) { + final IHolder> newHolder = held.map((containedHolder) -> { + return containedHolder.map((containedValue) -> { + if(containedValue == null) return null; + + return mapper.apply(containedValue); + }); + }); + + return new WrappedOption<>(newHolder, false); + } + + @Override + public IHolder transform(final UnaryOperator transformer) { + held.transform((containedHolder) -> { + return containedHolder.transform((containedValue) -> { + if(containedValue == null) return null; + + return transformer.apply(containedValue); + }); + }); + + return this; + } + + @Override + public UnwrappedType unwrap(final Function unwrapper) { + return held.unwrap((containedHolder) -> { + return containedHolder.unwrap((containedValue) -> { + if(containedValue == null) return null; + + return unwrapper.apply(containedValue); + }); + }); + } +} -- cgit v1.2.3