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 final Supplier oldSupplier; private final Function> binder; private IPair boundPair; private boolean pairBound; 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()) { newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } return newPair.unwrap((pair) -> pair.getLeft()); }; final 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( 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) -> { return new LazyPair<>(leftCombiner.apply(leftVal, otherLeft), rightCombiner.apply(rightVal, otherRight)); }); }); } @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); } }