From 002516bd03b2ea3f731c8139c9a5f716902ab702 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 7 Apr 2020 21:03:53 -0400 Subject: Finish remove utils.data utils.data now lives in the esodata project; not in this one --- .../utils/data/internals/HalfBoundLazyPair.java | 177 --------------------- 1 file changed, 177 deletions(-) delete mode 100644 base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java (limited to 'base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java') diff --git a/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java b/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java deleted file mode 100644 index 4803bc2..0000000 --- a/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java +++ /dev/null @@ -1,177 +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; - -/* - * @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); - } -} -- cgit v1.2.3