summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/lazy
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/lazy')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java101
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java31
2 files changed, 132 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java
new file mode 100644
index 0000000..43dec86
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java
@@ -0,0 +1,101 @@
+package bjc.utils.data.lazy;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import bjc.utils.data.IHolder;
+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);
+ }
+
+ @Override
+ public void doWith(Consumer<T> f) {
+ transform((val) -> {
+ f.accept(val);
+
+ return val;
+ });
+ }
+
+}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java
new file mode 100644
index 0000000..f680ed7
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java
@@ -0,0 +1,31 @@
+package bjc.utils.data.lazy;
+
+import java.util.function.BiConsumer;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+import bjc.utils.data.IPair;
+import bjc.utils.data.Pair;
+
+public class LazyPair<L, R> implements IPair<L, R> {
+ private LazyHolder<Pair<L, R>> del;
+
+ @Override
+ public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf,
+ Function<R, R2> rf) {
+ return del.unwrap((par) -> par.apply(lf, rf));
+ }
+
+ @Override
+ public <E> E merge(BiFunction<L, R, E> bf) {
+ return del.unwrap((par) -> par.merge(bf));
+ }
+
+ @Override
+ public void doWith(BiConsumer<L, R> bc) {
+ del.doWith((par) -> {
+ par.doWith(bc);
+ });
+ }
+
+}