From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../java/bjc/utils/data/HalfBoundLazyPair.java | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java new file mode 100644 index 0000000..f32f58f --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java @@ -0,0 +1,93 @@ +package bjc.utils.data; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +class HalfBoundLazyPair + implements IPair { + private Supplier oldSupplier; + + private Function> binder; + + private IPair boundPair; + private boolean pairBound; + + public HalfBoundLazyPair(Supplier oldSupp, + Function> bindr) { + oldSupplier = oldSupp; + binder = bindr; + } + + @Override + public IPair bindLeft( + Function> leftBinder) { + Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(oldSupplier.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(oldSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @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(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + 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 MergedType merge( + BiFunction merger) { + if (!pairBound) { + boundPair = binder.apply(oldSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } +} \ No newline at end of file -- cgit v1.2.3