diff options
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.java | 28 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java | 40 |
2 files changed, 49 insertions, 19 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 index 43dec86..6321cc2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java @@ -21,9 +21,9 @@ import bjc.utils.funcdata.FunctionalList; */ public class LazyHolder<T> implements IHolder<T> { /** - * The source for a value held by this lazy holder + * List of queued actions to be performed on realized values */ - private Supplier<T> heldSrc; + private FunctionalList<Function<T, T>> actions; /** * The value internally held by this lazy holder @@ -31,9 +31,9 @@ public class LazyHolder<T> implements IHolder<T> { private T held; /** - * List of queued actions to be performed on realized values + * The source for a value held by this lazy holder */ - private FunctionalList<Function<T, T>> actions; + private Supplier<T> heldSrc; /** * Create a new lazy holder with the given supplier @@ -58,8 +58,17 @@ public class LazyHolder<T> implements IHolder<T> { } @Override + public void doWith(Consumer<T> f) { + transform((val) -> { + f.accept(val); + + return val; + }); + } + + @Override public <NewT> IHolder<NewT> map(Function<T, NewT> f) { - return new LazyHolder<NewT>(() -> { + return new LazyHolder<>(() -> { if (held == null) { return actions.reduceAux(heldSrc.get(), Function<T, T>::apply, f::apply); @@ -89,13 +98,4 @@ public class LazyHolder<T> implements IHolder<T> { 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 index 2aab4ce..bd02b52 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java @@ -6,20 +6,41 @@ import java.util.function.Function; import bjc.utils.data.IPair; +/** + * A lazy holder of two values + * + * Lazy variant of {@link IPair} + * + * @author ben + * + * @param <L> + * The type of value stored on the left side of the pair + * @param <R> + * The type of value stored on the right side of the pair + */ public class LazyPair<L, R> implements IPair<L, R> { + /** + * The backing store for this pair + */ protected LazyHolder<IPair<L, R>> del; + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#apply(java.util.function.Function, + * java.util.function.Function) + */ @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)); - } - + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer) + */ @Override public void doWith(BiConsumer<L, R> bc) { del.doWith((par) -> { @@ -27,4 +48,13 @@ public class LazyPair<L, R> implements IPair<L, R> { }); } + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#merge(java.util.function.BiFunction) + */ + @Override + public <E> E merge(BiFunction<L, R, E> bf) { + return del.unwrap((par) -> par.merge(bf)); + } } |
