summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-04-07 21:03:53 -0400
committerBen Culkin <scorpress@gmail.com>2020-04-07 21:03:53 -0400
commit002516bd03b2ea3f731c8139c9a5f716902ab702 (patch)
tree4b45260e0b411dd070ae5d2f45e8f2795bf91b79 /base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java
parentd6afd99961aacd6ce915629773723cb6c6e145a7 (diff)
Finish remove utils.data
utils.data now lives in the esodata project; not in this one
Diffstat (limited to 'base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java')
-rw-r--r--base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java228
1 files changed, 0 insertions, 228 deletions
diff --git a/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java b/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java
deleted file mode 100644
index 2ef0e4c..0000000
--- a/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java
+++ /dev/null
@@ -1,228 +0,0 @@
-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.
- *
- * @author Ben Culkin
- */
-@SuppressWarnings("javadoc")
-public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPair<NewLeft, NewRight> {
- /* The supplier of the left value. */
- private final Supplier<OldLeft> leftSupplier;
- /* The supplier of the right value. */
- private final Supplier<OldRight> rightSupplier;
-
- /* The binder to transform values. */
- private final BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> binder;
-
- /* The bound pair. */
- private IPair<NewLeft, NewRight> 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<OldLeft> leftSupp, final Supplier<OldRight> rightSupp,
- final BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> bindr) {
- leftSupplier = leftSupp;
- rightSupplier = rightSupp;
- binder = bindr;
- }
-
- @Override
- public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind(
- final BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) {
- if(bindr == null) throw new NullPointerException("Binder must not be null");
-
- final IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>(boundPair);
- final IHolder<Boolean> newPairMade = new Identity<>(pairBound);
-
- final Supplier<NewLeft> 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<NewRight> 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 <BoundLeft> IPair<BoundLeft, NewRight> bindLeft(
- final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) {
- if(leftBinder == null) throw new NullPointerException("Left binder must not be null");
-
- final Supplier<NewLeft> leftSupp = () -> {
- IPair<NewLeft, NewRight> 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 <BoundRight> IPair<NewLeft, BoundRight> bindRight(
- final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) {
- if(rightBinder == null) throw new NullPointerException("Right binder must not be null");
-
- final Supplier<NewRight> rightSupp = () -> {
- IPair<NewLeft, NewRight> 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 <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine(
- final IPair<OtherLeft, OtherRight> otherPair,
- final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner,
- final BiFunction<NewRight, OtherRight, CombinedRight> 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 <NewLeftType> IPair<NewLeftType, NewRight> mapLeft(final Function<NewLeft, NewLeftType> mapper) {
- if(mapper == null) throw new NullPointerException("Mapper must not be null");
-
- final Supplier<NewLeftType> leftSupp = () -> {
- if(!pairBound) {
- final NewLeft leftVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft();
-
- return mapper.apply(leftVal);
- }
-
- return mapper.apply(boundPair.getLeft());
- };
-
- final Supplier<NewRight> rightSupp = () -> {
- if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight();
-
- return boundPair.getRight();
- };
-
- return new LazyPair<>(leftSupp, rightSupp);
- }
-
- @Override
- public <NewRightType> IPair<NewLeft, NewRightType> mapRight(final Function<NewRight, NewRightType> mapper) {
- if(mapper == null) throw new NullPointerException("Mapper must not be null");
-
- final Supplier<NewLeft> leftSupp = () -> {
- if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft();
-
- return boundPair.getLeft();
- };
-
- final Supplier<NewRightType> 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> MergedType merge(final BiFunction<NewLeft, NewRight, MergedType> 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)";
- }
-}