From 002516bd03b2ea3f731c8139c9a5f716902ab702 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 7 Apr 2020 21:03:53 -0400 Subject: Finish remove utils.data utils.data now lives in the esodata project; not in this one --- .../java/bjc/utils/data/internals/BoundLazy.java | 137 --------------------- 1 file changed, 137 deletions(-) delete mode 100644 base/src/main/java/bjc/utils/data/internals/BoundLazy.java (limited to 'base/src/main/java/bjc/utils/data/internals/BoundLazy.java') diff --git a/base/src/main/java/bjc/utils/data/internals/BoundLazy.java b/base/src/main/java/bjc/utils/data/internals/BoundLazy.java deleted file mode 100644 index f4e8f7e..0000000 --- a/base/src/main/java/bjc/utils/data/internals/BoundLazy.java +++ /dev/null @@ -1,137 +0,0 @@ -package bjc.utils.data.internals; - -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.function.UnaryOperator; - -import bjc.utils.data.IHolder; -import bjc.utils.data.Lazy; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - -/** - * Implements a lazy holder that has been bound. - * - * @author Ben Culkin - */ -@SuppressWarnings("javadoc") -public class BoundLazy implements IHolder { - /* The old value. */ - private final Supplier> oldSupplier; - - /* The function to use to transform the old value into a new value. */ - private final Function> binder; - - /* The bound value being held. */ - private IHolder boundHolder; - - /* Whether the bound value has been actualized or not. */ - private boolean holderBound; - - /* Transformations currently pending on the bound value. */ - private final IList> actions = new FunctionalList<>(); - - /** - * 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) { - oldSupplier = supp; - this.binder = binder; - } - - @Override - public IHolder bind(final Function> bindr) { - if(bindr == null) throw new NullPointerException("Binder must not be null"); - - /* Prepare a list of pending actions. */ - final IList> pendingActions = new FunctionalList<>(); - actions.forEach(pendingActions::add); - - /* Create the new supplier of a value. */ - final 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); - }; - - return new BoundLazy<>(typeSupplier, bindr); - } - - @Override - public Function> lift( - final Function func) { - if(func == null) throw new NullPointerException("Function to lift must not be null"); - - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } - - @Override - public IHolder map(final Function mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); - - /* Prepare a list of pending actions. */ - final IList> pendingActions = new FunctionalList<>(); - actions.forEach(pendingActions::add); - - /* Prepare the new supplier. */ - final Supplier typeSupplier = () -> { - IHolder oldHolder = boundHolder; - - /* 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)); - }; - - return new Lazy<>(typeSupplier); - } - - @Override - public String toString() { - if(holderBound) return boundHolder.toString(); - - return "(unmaterialized)"; - } - - @Override - public IHolder transform(final UnaryOperator transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); - - actions.add(transformer); - - return this; - } - - @Override - public UnwrappedType unwrap(final Function unwrapper) { - if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); - - if(!holderBound) { - boundHolder = oldSupplier.get().unwrap(binder::apply); - } - - return boundHolder.unwrap(unwrapper); - } -} -- cgit v1.2.3