package bjc.utils.data; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; class BoundLazyPair implements IPair { private Supplier leftSupplier; private Supplier rightSupplier; private BiFunction> binder; private IPair boundPair; private boolean pairBound; public BoundLazyPair(Supplier leftSupp, Supplier rightSupp, BiFunction> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; 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(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) { 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) { Supplier rightSupp = () -> { IPair newPair = boundPair; if (!pairBound) { newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } return newPair.getRight(); }; return new HalfBoundLazyPair<>(rightSupp, rightBinder); } @Override public MergedType merge( BiFunction merger) { 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)"; } @Override public IPair mapLeft( Function mapper) { 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) { 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 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)); }); }); } }