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 +++++++++++++++++----- 1 file changed, 53 insertions(+), 16 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java') 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)); }; -- cgit v1.2.3