From 61fd71f69e080790da722e0e03b71ecd7c2538a2 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 10 May 2016 16:02:45 -0400 Subject: General update --- .../src/main/java/bjc/utils/data/BoundLazy.java | 69 +++++++++++++++++----- .../main/java/bjc/utils/data/BoundListHolder.java | 10 ++-- BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 10 ++-- .../src/main/java/bjc/utils/data/ListHolder.java | 10 ++-- BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 26 ++++++++ 5 files changed, 94 insertions(+), 31 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java index 9ab3c05..1256e31 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java @@ -5,20 +5,42 @@ import java.util.function.Supplier; import java.util.function.UnaryOperator; import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IList; +/* + * Implements a lazy holder that has been bound + */ class BoundLazy implements IHolder { + /* + * The old value + */ private Supplier> oldSupplier; + /* + * The function to use to transform the old value into a new value + */ private Function> binder; + /* + * The bound value being held + */ private IHolder boundHolder; + /* + * Whether the bound value has been actualized or not + */ private boolean holderBound; - private IFunctionalList> actions = new FunctionalList<>(); + /* + * Transformations currently pending on the bound value + */ + private IList> actions = + new FunctionalList<>(); + /* + * Create a new bound lazy value + */ public BoundLazy(Supplier> supp, Function> binder) { oldSupplier = supp; @@ -26,19 +48,31 @@ class BoundLazy } @Override - public IHolder bind( - Function> bindr) { - IFunctionalList> pendingActions = new FunctionalList<>(); - + public IHolder + bind(Function> bindr) { + /* + * Prepare a list of pending actions + */ + IList> pendingActions = + new FunctionalList<>(); actions.forEach(pendingActions::add); + /* + * Create the new supplier of a value + */ Supplier> typeSupplier = () -> { IHolder oldHolder = boundHolder; + /* + * Bind the value if it hasn't been bound before + */ if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } + /* + * Apply all the pending actions + */ return pendingActions.reduceAux(oldHolder, (action, state) -> { return state.transform(action); }, (value) -> value); @@ -48,15 +82,18 @@ class BoundLazy } @Override - public IHolder map( - Function mapper) { - IFunctionalList> pendingActions = new FunctionalList<>(); - + public IHolder + map(Function mapper) { + // Prepare a list of pending actions + IList> pendingActions = + new FunctionalList<>(); actions.forEach(pendingActions::add); + // Prepare the new supplier Supplier typeSupplier = () -> { IHolder oldHolder = boundHolder; + // Bound the value if it hasn't been bound if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } @@ -80,16 +117,16 @@ class BoundLazy } @Override - public IHolder transform( - UnaryOperator transformer) { + public IHolder + transform(UnaryOperator transformer) { actions.add(transformer); return this; } @Override - public UnwrappedType unwrap( - Function unwrapper) { + public UnwrappedType + unwrap(Function unwrapper) { if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } @@ -98,8 +135,8 @@ class BoundLazy } @Override - public Function> lift( - Function func) { + public Function> + lift(Function func) { return (val) -> { return new Lazy<>(func.apply(val)); }; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java index fcb62f6..85ec8f6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java @@ -3,20 +3,20 @@ package bjc.utils.data; import java.util.function.Function; import java.util.function.UnaryOperator; -import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IList; class BoundListHolder implements IHolder { - private IFunctionalList> heldHolders; + private IList> heldHolders; public BoundListHolder( - IFunctionalList> toHold) { + IList> toHold) { heldHolders = toHold; } @Override public IHolder bind( Function> binder) { - IFunctionalList> boundHolders = heldHolders + IList> boundHolders = heldHolders .map((containedHolder) -> { return containedHolder.bind(binder); }); @@ -27,7 +27,7 @@ class BoundListHolder implements IHolder { @Override public IHolder map( Function mapper) { - IFunctionalList> mappedHolders = heldHolders + IList> mappedHolders = heldHolders .map((containedHolder) -> { return containedHolder.map(mapper); }); diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java index 62b0bb0..fb1a517 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -5,7 +5,7 @@ import java.util.function.Supplier; import java.util.function.UnaryOperator; import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IList; /** * A holder that holds a means to create a value, but doesn't actually @@ -18,7 +18,7 @@ import bjc.utils.funcdata.IFunctionalList; public class Lazy implements IHolder { private Supplier valueSupplier; - private IFunctionalList> actions = new FunctionalList<>(); + private IList> actions = new FunctionalList<>(); private boolean valueMaterialized; @@ -49,7 +49,7 @@ public class Lazy implements IHolder { } private Lazy(Supplier supp, - IFunctionalList> pendingActions) { + IList> pendingActions) { valueSupplier = supp; actions = pendingActions; @@ -58,7 +58,7 @@ public class Lazy implements IHolder { @Override public IHolder bind( Function> binder) { - IFunctionalList> pendingActions = new FunctionalList<>(); + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -78,7 +78,7 @@ public class Lazy implements IHolder { @Override public IHolder map( Function mapper) { - IFunctionalList> pendingActions = new FunctionalList<>(); + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java index 8dc33d3..03765ed 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -4,7 +4,7 @@ import java.util.function.Function; import java.util.function.UnaryOperator; import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IList; /** * A holder that represents a set of non-deterministic computations @@ -15,9 +15,9 @@ import bjc.utils.funcdata.IFunctionalList; * The type of contained value */ public class ListHolder implements IHolder { - private IFunctionalList heldValues; + private IList heldValues; - private ListHolder(IFunctionalList toHold) { + private ListHolder(IList toHold) { heldValues = toHold; } @@ -41,7 +41,7 @@ public class ListHolder implements IHolder { @Override public IHolder bind( Function> binder) { - IFunctionalList> boundValues = heldValues + IList> boundValues = heldValues .map(binder); return new BoundListHolder<>(boundValues); @@ -50,7 +50,7 @@ public class ListHolder implements IHolder { @Override public IHolder map( Function mapper) { - IFunctionalList mappedValues = heldValues.map(mapper); + IList mappedValues = heldValues.map(mapper); return new ListHolder<>(mappedValues); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index 0d2c1b0..eb421bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -15,7 +15,9 @@ import java.util.function.Function; */ public class Pair implements IPair { + // The left value private LeftType leftValue; + // The right value private RightType rightValue; /** @@ -40,24 +42,40 @@ public class Pair @Override public IPair bind( BiFunction> binder) { + if (binder == null) { + throw new NullPointerException("Binder must not be null."); + } + return binder.apply(leftValue, rightValue); } @Override public IPair bindLeft( Function> leftBinder) { + if (leftBinder == null) { + throw new NullPointerException("Binder must not be null"); + } + return leftBinder.apply(leftValue); } @Override public IPair bindRight( Function> rightBinder) { + if (rightBinder == null) { + throw new NullPointerException("Binder must not be null"); + } + return rightBinder.apply(rightValue); } @Override public MergedType merge(BiFunction merger) { + if (merger == null) { + throw new NullPointerException("Merger must not be null"); + } + return merger.apply(leftValue, rightValue); } @@ -70,12 +88,20 @@ public class Pair @Override public IPair mapLeft(Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + return new Pair<>(mapper.apply(leftValue), rightValue); } @Override public IPair mapRight(Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + return new Pair<>(leftValue, mapper.apply(rightValue)); } } \ No newline at end of file -- cgit v1.2.3