From df852b8a67f4b8f1c3e6f89d8c3630e678c49c14 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 1 Mar 2016 20:43:09 -0500 Subject: Implemented support for lazy pairs --- .../src/main/java/bjc/utils/data/GenHolder.java | 10 ++ .../src/main/java/bjc/utils/data/IHolder.java | 8 ++ .../src/main/java/bjc/utils/data/LazyHolder.java | 90 ------------------ .../main/java/bjc/utils/data/lazy/LazyHolder.java | 101 +++++++++++++++++++++ .../main/java/bjc/utils/data/lazy/LazyPair.java | 31 +++++++ 5 files changed, 150 insertions(+), 90 deletions(-) delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java index d6266ca..fba801b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java @@ -1,5 +1,6 @@ package bjc.utils.data; +import java.util.function.Consumer; import java.util.function.Function; /** @@ -69,4 +70,13 @@ public class GenHolder implements IHolder { public E unwrap(Function f) { return f.apply(held); } + + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IHolder#doWith(java.util.function.Consumer) + */ + public void doWith(Consumer f) { + f.accept(held); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java index b6e752b..ddcb2f6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -1,5 +1,6 @@ package bjc.utils.data; +import java.util.function.Consumer; import java.util.function.Function; /** @@ -42,4 +43,11 @@ public interface IHolder { */ public E unwrap(Function f); + /** + * Call a provided function with the value being held + * + * @param f + * The function to call + */ + public void doWith(Consumer f); } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java deleted file mode 100644 index db5b6ef..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyHolder.java +++ /dev/null @@ -1,90 +0,0 @@ -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 - * The type of the data being held - */ -public class LazyHolder implements IHolder { - /** - * The source for a value held by this lazy holder - */ - private Supplier heldSrc; - - /** - * The value internally held by this lazy holder - */ - private T held; - - /** - * List of queued actions to be performed on realized values - */ - private FunctionalList> actions; - - /** - * Create a new lazy holder with the given supplier - * - * @param src - * The supplier for a value when it is neededs - */ - public LazyHolder(Supplier 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 IHolder map(Function f) { - return new LazyHolder(() -> { - if (held == null) { - return actions.reduceAux(heldSrc.get(), - Function::apply, f::apply); - } else { - return actions.reduceAux(held, Function::apply, - f::apply); - } - }); - } - - @Override - public IHolder transform(Function f) { - actions.add(f); - - return this; - } - - @Override - public E unwrap(Function f) { - // Actualize ourselves - if (held == null) { - held = heldSrc.get(); - } - - actions.forEach((act) -> held = act.apply(held)); - - return f.apply(held); - } - -} 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 + * The type of the data being held + */ +public class LazyHolder implements IHolder { + /** + * The source for a value held by this lazy holder + */ + private Supplier heldSrc; + + /** + * The value internally held by this lazy holder + */ + private T held; + + /** + * List of queued actions to be performed on realized values + */ + private FunctionalList> actions; + + /** + * Create a new lazy holder with the given supplier + * + * @param src + * The supplier for a value when it is neededs + */ + public LazyHolder(Supplier 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 IHolder map(Function f) { + return new LazyHolder(() -> { + if (held == null) { + return actions.reduceAux(heldSrc.get(), + Function::apply, f::apply); + } else { + return actions.reduceAux(held, Function::apply, + f::apply); + } + }); + } + + @Override + public IHolder transform(Function f) { + actions.add(f); + + return this; + } + + @Override + public E unwrap(Function 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 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 implements IPair { + private LazyHolder> del; + + @Override + public IPair apply(Function lf, + Function rf) { + return del.unwrap((par) -> par.apply(lf, rf)); + } + + @Override + public E merge(BiFunction bf) { + return del.unwrap((par) -> par.merge(bf)); + } + + @Override + public void doWith(BiConsumer bc) { + del.doWith((par) -> { + par.doWith(bc); + }); + } + +} -- cgit v1.2.3