diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java new file mode 100644 index 0000000..f4bc24a --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -0,0 +1,136 @@ +package bjc.utils.data; + +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; + +/** + * A holder that holds a means to create a value, but doesn't actually + * compute the value until it's needed + * + * @author ben + * + * @param <ContainedType> + */ +public class Lazy<ContainedType> implements IHolder<ContainedType> { + private Supplier<ContainedType> valueSupplier; + + private IFunctionalList<UnaryOperator<ContainedType>> actions = new FunctionalList<>(); + + private boolean valueMaterialized; + + private ContainedType heldValue; + + /** + * Create a new lazy value from the specified seed value + * + * @param value + * The seed value to use + */ + public Lazy(ContainedType value) { + heldValue = value; + + valueMaterialized = true; + } + + /** + * Create a new lazy value from the specified value source + * + * @param supp + * The source of a value to use + */ + public Lazy(Supplier<ContainedType> supp) { + valueSupplier = supp; + + valueMaterialized = false; + } + + private Lazy(Supplier<ContainedType> supp, + IFunctionalList<UnaryOperator<ContainedType>> pendingActions) { + valueSupplier = supp; + + actions = pendingActions; + } + + @Override + public <BoundType> IHolder<BoundType> bind( + Function<ContainedType, IHolder<BoundType>> binder) { + IFunctionalList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); + + actions.forEach(pendingActions::add); + + Supplier<ContainedType> supplier = () -> { + if (valueMaterialized) { + return heldValue; + } + + return valueSupplier.get(); + }; + + return new BoundLazy<>(() -> { + return new Lazy<>(supplier, pendingActions); + }, binder); + } + + @Override + public <MappedType> IHolder<MappedType> map( + Function<ContainedType, MappedType> mapper) { + IFunctionalList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); + + actions.forEach(pendingActions::add); + + return new Lazy<>(() -> { + ContainedType currVal = heldValue; + + if (!valueMaterialized) { + currVal = valueSupplier.get(); + } + + return pendingActions.reduceAux(currVal, + UnaryOperator<ContainedType>::apply, + (value) -> mapper.apply(value)); + }); + } + + @Override + public IHolder<ContainedType> transform( + UnaryOperator<ContainedType> transformer) { + actions.add(transformer); + + return this; + } + + @Override + public <UnwrappedType> UnwrappedType unwrap( + Function<ContainedType, UnwrappedType> unwrapper) { + if (!valueMaterialized) { + heldValue = valueSupplier.get(); + + valueMaterialized = true; + } + + actions.forEach((action) -> { + heldValue = action.apply(heldValue); + }); + + actions = new FunctionalList<>(); + + return unwrapper.apply(heldValue); + } + + @Override + public String toString() { + if (valueMaterialized) { + if (actions.isEmpty()) { + return "value[v='" + heldValue + "']"; + } + + return "value[v='" + heldValue + "'] (has pending transforms)"; + } + + return "(unmaterialized)"; + } +} |
