From e6459a351f14d76dbac83e95a55664820387ad7b Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 27 Feb 2017 10:11:43 -0500 Subject: Package reorganization --- .../java/bjc/utils/data/internals/BoundLazy.java | 164 ++++++++++++++ .../bjc/utils/data/internals/BoundLazyPair.java | 241 +++++++++++++++++++++ .../bjc/utils/data/internals/BoundListHolder.java | 86 ++++++++ .../utils/data/internals/HalfBoundLazyPair.java | 163 ++++++++++++++ .../java/bjc/utils/data/internals/WrappedLazy.java | 62 ++++++ .../bjc/utils/data/internals/WrappedOption.java | 83 +++++++ 6 files changed, 799 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/internals') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java new file mode 100644 index 0000000..beb2465 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java @@ -0,0 +1,164 @@ +package bjc.utils.data.internals; + +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +import bjc.utils.data.IHolder; +import bjc.utils.data.Lazy; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + +/** + * Implements a lazy holder that has been bound + */ +public class BoundLazy + implements IHolder { + /* + * The old value + */ + private Supplier> oldSupplier; + + /* + * The function to use to transform the old value into a new value + */ + private 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 IList> actions = new FunctionalList<>(); + + /* + * Create a new bound lazy value + */ + public BoundLazy(Supplier> supp, + Function> binder) { + oldSupplier = supp; + this.binder = binder; + } + + @Override + public IHolder bind( + Function> bindr) { + if (bindr == null) { + throw new NullPointerException("Binder must not be null"); + } + + /* + * Prepare a list of pending actions + */ + IList> pendingActions = new FunctionalList<>(); + actions.forEach(pendingActions::add); + + /* + * Create the new supplier of a value + */ + 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( + 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( + Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + + // Prepare a list of pending actions + IList> pendingActions = new FunctionalList<>(); + actions.forEach(pendingActions::add); + + // Prepare the new supplier + Supplier typeSupplier = () -> { + IHolder oldHolder = boundHolder; + + // Bound the value if it hasn't been bound + if (!holderBound) { + oldHolder = oldSupplier.get().unwrap(binder); + } + + 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( + UnaryOperator transformer) { + if (transformer == null) { + throw new NullPointerException("Transformer must not be null"); + } + + actions.add(transformer); + + return this; + } + + @Override + public UnwrappedType unwrap( + 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); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java new file mode 100644 index 0000000..977fb37 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java @@ -0,0 +1,241 @@ +package bjc.utils.data.internals; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; +import bjc.utils.data.Identity; +import bjc.utils.data.LazyPair; + +/** + * Implements a lazy pair that has been bound + */ +public class BoundLazyPair + implements IPair { + /* + * The supplier of the left value + */ + private Supplier leftSupplier; + /* + * The supplier of the right value + */ + private Supplier rightSupplier; + + /* + * The binder to transform values + */ + private BiFunction> binder; + + /* + * The bound pair + */ + private IPair boundPair; + + /* + * Whether the pair has been bound yet + */ + private boolean pairBound; + + public BoundLazyPair(Supplier leftSupp, + Supplier rightSupp, + BiFunction> bindr) { + leftSupplier = leftSupp; + rightSupplier = rightSupp; + binder = bindr; + } + + @Override + public IPair bind( + BiFunction> bindr) { + if (bindr == null) { + throw new NullPointerException("Binder must not be null"); + } + + IHolder> newPair = new Identity<>( + boundPair); + IHolder newPairMade = new Identity<>(pairBound); + + Supplier leftSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(leftSupplier.get(), + rightSupplier.get())); + + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + Supplier rightSupp = () -> { + if (!newPairMade.getValue()) { + 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( + Function> leftBinder) { + if (leftBinder == null) { + throw new NullPointerException("Left binder must not be null"); + } + + Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + } + + return newPair.getLeft(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + Function> rightBinder) { + if (rightBinder == null) { + throw new NullPointerException( + "Right binder must not be null"); + } + + Supplier rightSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + 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) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); + } + + @Override + public IPair mapLeft( + Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + + Supplier leftSupp = () -> { + if (!pairBound) { + NewLeft leftVal = binder + .apply(leftSupplier.get(), rightSupplier.get()) + .getLeft(); + + return mapper.apply(leftVal); + } + + return mapper.apply(boundPair.getLeft()); + }; + + Supplier rightSupp = () -> { + if (!pairBound) { + return binder + .apply(leftSupplier.get(), rightSupplier.get()) + .getRight(); + } + + return boundPair.getRight(); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public IPair mapRight( + Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + + Supplier leftSupp = () -> { + if (!pairBound) { + return binder + .apply(leftSupplier.get(), rightSupplier.get()) + .getLeft(); + } + + return boundPair.getLeft(); + }; + + Supplier rightSupp = () -> { + if (!pairBound) { + 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( + BiFunction merger) { + if (merger == null) { + throw new NullPointerException("Merger must not be null"); + } + + if (!pairBound) { + 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)"; + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java new file mode 100644 index 0000000..7a3eaa8 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java @@ -0,0 +1,86 @@ +package bjc.utils.data.internals; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.utils.data.IHolder; +import bjc.utils.data.ListHolder; +import bjc.utils.funcdata.IList; + +/* + * Holds a list, converted into a holder + */ +public class BoundListHolder implements IHolder { + private IList> heldHolders; + + public BoundListHolder(IList> toHold) { + heldHolders = toHold; + } + + @Override + public IHolder bind( + Function> binder) { + if (binder == null) { + throw new NullPointerException("Binder must not be null"); + } + + IList> boundHolders = heldHolders + .map((containedHolder) -> { + return containedHolder.bind(binder); + }); + + return new BoundListHolder<>(boundHolders); + } + + @Override + public Function> lift( + 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( + Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + + IList> mappedHolders = heldHolders + .map((containedHolder) -> { + return containedHolder.map(mapper); + }); + + return new BoundListHolder<>(mappedHolders); + } + + @Override + public IHolder transform( + 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( + Function unwrapper) { + if (unwrapper == null) { + throw new NullPointerException("Unwrapper must not be null"); + } + + return heldHolders.randItem().unwrap(unwrapper); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java new file mode 100644 index 0000000..1e8d109 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java @@ -0,0 +1,163 @@ +package bjc.utils.data.internals; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; +import bjc.utils.data.Identity; +import bjc.utils.data.LazyPair; + +/* + * A lazy pair, with only one side bound + */ +public class HalfBoundLazyPair + implements IPair { + private Supplier oldSupplier; + + private Function> binder; + + private IPair boundPair; + private boolean pairBound; + + public HalfBoundLazyPair(Supplier oldSupp, + Function> bindr) { + oldSupplier = oldSupp; + binder = bindr; + } + + @Override + public IPair bind( + BiFunction> bindr) { + IHolder> newPair = new Identity<>( + boundPair); + IHolder newPairMade = new Identity<>(pairBound); + + Supplier leftSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + Supplier rightSupp = () -> { + if (!newPairMade.getValue()) { + 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( + Function> leftBinder) { + Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(oldSupplier.get()); + } + + return newPair.getLeft(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + Function> rightBinder) { + Supplier rightSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(oldSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); + } + + @Override + public IPair mapLeft( + Function mapper) { + Supplier leftSupp = () -> { + if (pairBound) { + return mapper.apply(boundPair.getLeft()); + } + + NewLeft leftVal = binder.apply(oldSupplier.get()).getLeft(); + + return mapper.apply(leftVal); + }; + + Supplier rightSupp = () -> { + if (pairBound) { + return boundPair.getRight(); + } + + return binder.apply(oldSupplier.get()).getRight(); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public IPair mapRight( + Function mapper) { + Supplier leftSupp = () -> { + if (pairBound) { + return boundPair.getLeft(); + } + + return binder.apply(oldSupplier.get()).getLeft(); + }; + + Supplier rightSupp = () -> { + if (pairBound) { + return mapper.apply(boundPair.getRight()); + } + + NewRight rightVal = binder.apply(oldSupplier.get()).getRight(); + + return mapper.apply(rightVal); + }; + + return new LazyPair<>(leftSupp, rightSupp); + } + + @Override + public MergedType merge( + BiFunction merger) { + if (!pairBound) { + boundPair = binder.apply(oldSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java new file mode 100644 index 0000000..7f0b8db --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java @@ -0,0 +1,62 @@ +package bjc.utils.data.internals; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.utils.data.IHolder; +import bjc.utils.data.Lazy; + +public class WrappedLazy implements IHolder { + private IHolder> held; + + public WrappedLazy(IHolder wrappedHolder) { + held = new Lazy<>(wrappedHolder); + } + + // This has an extra parameter, because otherwise it erases to the same + // as the public one + private WrappedLazy(IHolder> wrappedHolder, boolean dummy) { + held = wrappedHolder; + } + + @Override + public IHolder bind(Function> binder) { + IHolder> newHolder = held.map((containedHolder) -> { + return containedHolder.bind(binder); + }); + + return new WrappedLazy<>(newHolder, false); + } + + @Override + public Function> lift(Function func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override + public IHolder map(Function mapper) { + IHolder> newHolder = held.map((containedHolder) -> { + return containedHolder.map(mapper); + }); + + return new WrappedLazy<>(newHolder, false); + } + + @Override + public IHolder transform(UnaryOperator transformer) { + held.transform((containedHolder) -> { + return containedHolder.transform(transformer); + }); + + return this; + } + + @Override + public UnwrappedType unwrap(Function unwrapper) { + return held.unwrap((containedHolder) -> { + return containedHolder.unwrap(unwrapper); + }); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java new file mode 100644 index 0000000..65c2463 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java @@ -0,0 +1,83 @@ +package bjc.utils.data.internals; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +import bjc.utils.data.IHolder; +import bjc.utils.data.Option; + +public class WrappedOption implements IHolder { + private IHolder> held; + + public WrappedOption(IHolder seedValue) { + held = new Option<>(seedValue); + } + + private WrappedOption(IHolder> toHold, boolean dummy) { + held = toHold; + } + + @Override public IHolder bind(Function> binder) { + 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(Function func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + + @Override + public IHolder map(Function mapper) { + 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(UnaryOperator transformer) { + held.transform((containedHolder) -> { + return containedHolder.transform((containedValue) -> { + if (containedValue == null) { + return null; + } + + return transformer.apply(containedValue); + }); + }); + + return this; + } + + @Override + public UnwrappedType unwrap(Function unwrapper) { + return held.unwrap((containedHolder) -> { + return containedHolder.unwrap((containedValue) -> { + if (containedValue == null) { + return null; + } + + return unwrapper.apply(containedValue); + }); + }); + } +} -- cgit v1.2.3