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 --- .../java/bjc/utils/data/internals/BoundLazy.java | 61 ++++++++---------- .../bjc/utils/data/internals/BoundLazyPair.java | 75 +++++++++++++++------- .../bjc/utils/data/internals/BoundListHolder.java | 19 +++++- .../utils/data/internals/HalfBoundLazyPair.java | 35 ++++++++-- .../java/bjc/utils/data/internals/WrappedLazy.java | 22 ++++++- .../bjc/utils/data/internals/WrappedOption.java | 17 +++++ 6 files changed, 161 insertions(+), 68 deletions(-) (limited to 'base/src/main/java/bjc/utils/data/internals') diff --git a/base/src/main/java/bjc/utils/data/internals/BoundLazy.java b/base/src/main/java/bjc/utils/data/internals/BoundLazy.java index f71d32b..b160c0c 100644 --- a/base/src/main/java/bjc/utils/data/internals/BoundLazy.java +++ b/base/src/main/java/bjc/utils/data/internals/BoundLazy.java @@ -9,37 +9,35 @@ import bjc.utils.data.Lazy; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; -/* - * Implements a lazy holder that has been bound +/** + * Implements a lazy holder that has been bound. + * + * @author Ben Culkin */ public class BoundLazy implements IHolder { - /* - * The old value - */ + /* The old value. */ private final Supplier> oldSupplier; - /* - * The function to use to transform the old value into a new value - */ + /* The function to use to transform the old value into a new value. */ private final Function> binder; - /* - * The bound value being held - */ + /* The bound value being held. */ private IHolder boundHolder; - /* - * Whether the bound value has been actualized or not - */ + /* Whether the bound value has been actualized or not. */ private boolean holderBound; - /* - * Transformations currently pending on the bound value - */ + /* Transformations currently pending on the bound value. */ private final IList> actions = new FunctionalList<>(); - /* - * Create a new bound lazy value + /** + * Create a new bound lazy value. + * + * @param supp + * The supplier of the old value. + * + * @param binder + * The function to use to bind the old value to the new one. */ public BoundLazy(final Supplier> supp, final Function> binder) { @@ -51,28 +49,20 @@ public class BoundLazy implements IHolder IHolder bind(final Function> bindr) { if (bindr == null) throw new NullPointerException("Binder must not be null"); - /* - * Prepare a list of pending actions - */ + /* Prepare a list of pending actions. */ final IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); - /* - * Create the new supplier of a value - */ + /* Create the new supplier of a value. */ final Supplier> typeSupplier = () -> { IHolder oldHolder = boundHolder; - /* - * Bind the value if it hasn't been bound before - */ + /* Bind the value if it hasn't been bound before. */ if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } - /* - * Apply all the pending actions - */ + /* Apply all the pending actions. */ return pendingActions.reduceAux(oldHolder, (action, state) -> { return state.transform(action); }, (value) -> value); @@ -95,19 +85,20 @@ public class BoundLazy implements IHolder IHolder map(final Function mapper) { if (mapper == null) throw new NullPointerException("Mapper must not be null"); - // Prepare a list of pending actions + /* Prepare a list of pending actions. */ final IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); - // Prepare the new supplier + /* Prepare the new supplier. */ final Supplier typeSupplier = () -> { IHolder oldHolder = boundHolder; - // Bound the value if it hasn't been bound + /* Bound the value if it hasn't been bound. */ if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } + /* Apply pending actions. */ return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> { return action.apply(state); }, (value) -> mapper.apply(value)); @@ -142,4 +133,4 @@ public class BoundLazy implements IHolder implements IPair { - /* - * The supplier of the left value - */ + /* The supplier of the left value. */ private final Supplier leftSupplier; - /* - * The supplier of the right value - */ + /* The supplier of the right value. */ private final Supplier rightSupplier; - /* - * The binder to transform values - */ + /* The binder to transform values. */ private final BiFunction> binder; - /* - * The bound pair - */ + /* The bound pair. */ private IPair boundPair; - /* - * Whether the pair has been bound yet - */ + /* Whether the pair has been bound yet. */ private boolean pairBound; + /** + * Create a new bound lazy pair. + * + * @param leftSupp + * The supplier for the left value. + * + * @param rightSupp + * The supplier for the right value. + * + * @param bindr + * The function to use to bind the left and right into a new pair. + */ public BoundLazyPair(final Supplier leftSupp, final Supplier rightSupp, final BiFunction> bindr) { leftSupplier = leftSupp; @@ -50,10 +54,14 @@ public class BoundLazyPair implements IPai if (bindr == null) throw new NullPointerException("Binder must not be null"); final IHolder> newPair = new Identity<>(boundPair); - final IHolder newPairMade = new Identity<>(pairBound); + final IHolder newPairMade = new Identity<>(pairBound); final Supplier leftSupp = () -> { if (!newPairMade.getValue()) { + /* + * If the pair hasn't been bound before, bind + * it. + */ newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); @@ -64,6 +72,10 @@ public class BoundLazyPair implements IPai final Supplier rightSupp = () -> { if (!newPairMade.getValue()) { + /* + * If the pair hasn't been bound before, bind + * it. + */ newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); @@ -84,6 +96,10 @@ public class BoundLazyPair implements IPai IPair newPair = boundPair; if (!pairBound) { + /* + * If the pair hasn't been bound before, bind + * it. + */ newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -102,6 +118,10 @@ public class BoundLazyPair implements IPai IPair newPair = boundPair; if (!pairBound) { + /* + * If the pair hasn't been bound before, bind + * it. + */ newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -116,16 +136,20 @@ public class BoundLazyPair implements IPai final IPair otherPair, final BiFunction leftCombiner, final BiFunction rightCombiner) { - if (otherPair == null) + if (otherPair == null) { throw new NullPointerException("Other pair must not be null"); - else if (leftCombiner == null) + } else if (leftCombiner == null) { throw new NullPointerException("Left combiner must not be null"); - else if (rightCombiner == null) throw new NullPointerException("Right combiner must not be null"); + } else if (rightCombiner == null) { + throw new NullPointerException("Right combiner must not be null"); + } 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); }); }); } @@ -182,6 +206,9 @@ public class BoundLazyPair implements IPai if (merger == null) throw new NullPointerException("Merger must not be null"); if (!pairBound) { + /* + * If the pair isn't bound yet, bind it. + */ boundPair = binder.apply(leftSupplier.get(), rightSupplier.get()); pairBound = true; @@ -196,4 +223,4 @@ public class BoundLazyPair implements IPai return "(un-materialized)"; } -} \ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java b/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java index f3799fd..8f9e87f 100644 --- a/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java +++ b/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java @@ -7,12 +7,21 @@ import bjc.utils.data.IHolder; import bjc.utils.data.ListHolder; import bjc.utils.funcdata.IList; -/* - * Holds a list, converted into a holder +/** + * Holds a list, converted into a holder. + * + * @author Ben Culkin */ public class BoundListHolder implements IHolder { + /* The list of contained holders. */ private final IList> heldHolders; + /** + * Create a new list of holders. + * + * @param toHold + * The list of holders to, well, hold. + */ public BoundListHolder(final IList> toHold) { heldHolders = toHold; } @@ -63,6 +72,10 @@ public class BoundListHolder implements IHolder { public UnwrappedType unwrap(final Function unwrapper) { if (unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + /* + * @NOTE + * Is there another way we could want to do this? + */ return heldHolders.randItem().unwrap(unwrapper); } -} \ No newline at end of file +} 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 implements IHolder { + /* Held value. */ private final IHolder> held; + /** + * Create a new wrapped lazy value. + * + * @param wrappedHolder + * The holder to make lazy. + */ public WrappedLazy(final IHolder wrappedHolder) { held = new Lazy<>(wrappedHolder); } - // This has an extra parameter, because otherwise it erases to the same - // as the public one + /* + * This has an extra parameter, because otherwise it erases to the same + * as the public one. + * + * This is a case where reified generics would be useful, because then + * the compiler could know which one we meant without the dummy + * parameter. + */ private WrappedLazy(final IHolder> wrappedHolder, final boolean dummy) { held = wrappedHolder; } diff --git a/base/src/main/java/bjc/utils/data/internals/WrappedOption.java b/base/src/main/java/bjc/utils/data/internals/WrappedOption.java index 512c699..872295f 100644 --- a/base/src/main/java/bjc/utils/data/internals/WrappedOption.java +++ b/base/src/main/java/bjc/utils/data/internals/WrappedOption.java @@ -6,13 +6,30 @@ import java.util.function.UnaryOperator; import bjc.utils.data.IHolder; import bjc.utils.data.Option; +/** + * A wrapped optional value. + * + * @author Ben Culkin. + */ public class WrappedOption implements IHolder { + /* The held value. */ private final IHolder> held; + /** + * Create a new wrapped option. + * + * @param seedValue + * The value to wrap. + */ public WrappedOption(final IHolder seedValue) { held = new Option<>(seedValue); } + /* + * The dummy parameter is to ensure the compiler can pick the right + * method, because without this method erases to the same type as the + * public one. + */ private WrappedOption(final IHolder> toHold, final boolean dummy) { held = toHold; } -- cgit v1.2.3