From 946cab444bc301d8a7c756a1bab039558288de89 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Wed, 11 Oct 2017 13:41:07 -0300 Subject: Cleanup work --- .../utils/data/internals/HalfBoundLazyPair.java | 35 +++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) (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 index 8cac38b..c3606ef 100644 --- a/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java +++ b/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java @@ -10,16 +10,39 @@ import bjc.utils.data.Identity; import bjc.utils.data.LazyPair; /* - * A lazy pair, with only one side bound + * @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 */ 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; @@ -34,6 +57,7 @@ public class HalfBoundLazyPair implements IPair leftSupp = () -> { if (!newPairMade.getValue()) { + /* Bind the pair if it hasn't been bound yet. */ newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } @@ -43,6 +67,7 @@ public class HalfBoundLazyPair implements IPair rightSupp = () -> { if (!newPairMade.getValue()) { + /* Bind the pair if it hasn't been bound yet. */ newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } @@ -92,8 +117,10 @@ public class HalfBoundLazyPair implements IPair rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { - return new LazyPair<>(leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(cLeft, cRight); }); }); } @@ -146,4 +173,4 @@ public class HalfBoundLazyPair implements IPair