summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-29 09:50:20 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-29 09:50:20 -0500
commit68faea64a4b1ef23acba209ad502e4458eb16290 (patch)
tree4c8d59e3a9eb927b09ba0850bf6e622112745c69
parentb3eb57f0796ef79f58ed76a1baf1cdb315772b6a (diff)
Implemented lazy variant of IHolder
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java
new file mode 100644
index 0000000..db5b6ef
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java
@@ -0,0 +1,90 @@
+package bjc.utils.data;
+
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import bjc.utils.funcdata.FunctionalList;
+
+/**
+ * Holds a single value of a specific type. This is used for indirect
+ * references to data, and more specifically for accessing non-final
+ * variables from a lambda. AKA the identity monad
+ *
+ * This is a lazy variant of {@link IHolder}
+ *
+ * @author ben
+ *
+ * @param <T>
+ * The type of the data being held
+ */
+public class LazyHolder<T> implements IHolder<T> {
+ /**
+ * The source for a value held by this lazy holder
+ */
+ private Supplier<T> heldSrc;
+
+ /**
+ * The value internally held by this lazy holder
+ */
+ private T held;
+
+ /**
+ * List of queued actions to be performed on realized values
+ */
+ private FunctionalList<Function<T, T>> actions;
+
+ /**
+ * Create a new lazy holder with the given supplier
+ *
+ * @param src
+ * The supplier for a value when it is neededs
+ */
+ public LazyHolder(Supplier<T> src) {
+ heldSrc = src;
+
+ held = null;
+ }
+
+ /**
+ * Create a new lazy holder with the given value
+ *
+ * @param val
+ * The value held in the holder
+ */
+ public LazyHolder(T val) {
+ held = val;
+ }
+
+ @Override
+ public <NewT> IHolder<NewT> map(Function<T, NewT> f) {
+ return new LazyHolder<NewT>(() -> {
+ if (held == null) {
+ return actions.reduceAux(heldSrc.get(),
+ Function<T, T>::apply, f::apply);
+ } else {
+ return actions.reduceAux(held, Function<T, T>::apply,
+ f::apply);
+ }
+ });
+ }
+
+ @Override
+ public IHolder<T> transform(Function<T, T> f) {
+ actions.add(f);
+
+ return this;
+ }
+
+ @Override
+ public <E> E unwrap(Function<T, E> f) {
+ // Actualize ourselves
+ if (held == null) {
+ held = heldSrc.get();
+ }
+
+ actions.forEach((act) -> held = act.apply(held));
+
+ return f.apply(held);
+ }
+
+}