From e6459a351f14d76dbac83e95a55664820387ad7b Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 27 Feb 2017 10:11:43 -0500 Subject: Package reorganization --- .../bjc/utils/data/internals/BoundLazyPair.java | 241 +++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java') 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 -- cgit v1.2.3