summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java
diff options
context:
space:
mode:
authorbjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu>2017-02-27 10:11:43 -0500
committerbjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu>2017-02-27 10:11:43 -0500
commite6459a351f14d76dbac83e95a55664820387ad7b (patch)
tree3fba82ce642fb6da2359bf55510b87fcd8064a88 /BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java
parent0b4fd83d36eb82c92a39af8b8e1c521d822bd4c3 (diff)
Package reorganization
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java164
1 files changed, 164 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java
new file mode 100644
index 0000000..beb2465
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java
@@ -0,0 +1,164 @@
+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
+ */
+public 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;
+
+ /*
+ * 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;
+ this.binder = binder;
+ }
+
+ @Override
+ public <BoundType> IHolder<BoundType> bind(
+ Function<BoundContainedType, IHolder<BoundType>> bindr) {
+ if (bindr == null) {
+ throw new NullPointerException("Binder must not be null");
+ }
+
+ /*
+ * 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);
+ };
+
+ return new BoundLazy<>(typeSupplier, bindr);
+ }
+
+ @Override
+ public <NewType> Function<BoundContainedType, IHolder<NewType>> lift(
+ Function<BoundContainedType, NewType> func) {
+ if (func == null) {
+ throw new NullPointerException(
+ "Function to lift must not be null");
+ }
+
+ return (val) -> {
+ return new Lazy<>(func.apply(val));
+ };
+ }
+
+ @Override
+ public <MappedType> IHolder<MappedType> map(
+ Function<BoundContainedType, MappedType> mapper) {
+ if (mapper == null) {
+ throw new NullPointerException("Mapper must not be null");
+ }
+
+ // 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);
+ }
+
+ 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<BoundContainedType> transform(
+ UnaryOperator<BoundContainedType> transformer) {
+ if (transformer == null) {
+ throw new NullPointerException("Transformer must not be null");
+ }
+
+ actions.add(transformer);
+
+ return this;
+ }
+
+ @Override
+ public <UnwrappedType> UnwrappedType unwrap(
+ Function<BoundContainedType, UnwrappedType> unwrapper) {
+ if (unwrapper == null) {
+ throw new NullPointerException("Unwrapper must not be null");
+ }
+
+ if (!holderBound) {
+ boundHolder = oldSupplier.get().unwrap(binder::apply);
+ }
+
+ return boundHolder.unwrap(unwrapper);
+ }
+} \ No newline at end of file