diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java | 69 |
1 files changed, 53 insertions, 16 deletions
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<OldType, BoundContainedType> implements IHolder<BoundContainedType> { + /* + * The old value + */ private Supplier<IHolder<OldType>> oldSupplier; + /* + * The function to use to transform the old value into a new value + */ private Function<OldType, IHolder<BoundContainedType>> binder; + /* + * The bound value being held + */ private IHolder<BoundContainedType> boundHolder; + /* + * Whether the bound value has been actualized or not + */ private boolean holderBound; - private IFunctionalList<UnaryOperator<BoundContainedType>> actions = new FunctionalList<>(); + /* + * Transformations currently pending on the bound value + */ + private IList<UnaryOperator<BoundContainedType>> actions = + new FunctionalList<>(); + /* + * Create a new bound lazy value + */ public BoundLazy(Supplier<IHolder<OldType>> supp, Function<OldType, IHolder<BoundContainedType>> binder) { oldSupplier = supp; @@ -26,19 +48,31 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public <BoundType> IHolder<BoundType> bind( - Function<BoundContainedType, IHolder<BoundType>> bindr) { - IFunctionalList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); - + public <BoundType> IHolder<BoundType> + bind(Function<BoundContainedType, IHolder<BoundType>> bindr) { + /* + * Prepare a list of pending actions + */ + IList<UnaryOperator<BoundContainedType>> pendingActions = + new FunctionalList<>(); actions.forEach(pendingActions::add); + /* + * Create the new supplier of a value + */ Supplier<IHolder<BoundContainedType>> typeSupplier = () -> { IHolder<BoundContainedType> 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<OldType, BoundContainedType> } @Override - public <MappedType> IHolder<MappedType> map( - Function<BoundContainedType, MappedType> mapper) { - IFunctionalList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); - + public <MappedType> IHolder<MappedType> + map(Function<BoundContainedType, MappedType> mapper) { + // Prepare a list of pending actions + IList<UnaryOperator<BoundContainedType>> pendingActions = + new FunctionalList<>(); actions.forEach(pendingActions::add); + // Prepare the new supplier Supplier<MappedType> typeSupplier = () -> { IHolder<BoundContainedType> oldHolder = boundHolder; + // Bound the value if it hasn't been bound if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } @@ -80,16 +117,16 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public IHolder<BoundContainedType> transform( - UnaryOperator<BoundContainedType> transformer) { + public IHolder<BoundContainedType> + transform(UnaryOperator<BoundContainedType> transformer) { actions.add(transformer); return this; } @Override - public <UnwrappedType> UnwrappedType unwrap( - Function<BoundContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType + unwrap(Function<BoundContainedType, UnwrappedType> unwrapper) { if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } @@ -98,8 +135,8 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public <NewType> Function<BoundContainedType, IHolder<NewType>> lift( - Function<BoundContainedType, NewType> func) { + public <NewType> Function<BoundContainedType, IHolder<NewType>> + lift(Function<BoundContainedType, NewType> func) { return (val) -> { return new Lazy<>(func.apply(val)); }; |
