From e47b798a05832c8326df953320cb507f74b66ed8 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 30 Mar 2016 10:03:24 -0400 Subject: Optimized a case that would've caused premature pair materialization --- .../main/java/bjc/utils/data/lazy/LazyPair.java | 53 +++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) 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 bd02b52..70b6f2f 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 @@ -3,8 +3,11 @@ package bjc.utils.data.lazy; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.function.Supplier; +import bjc.utils.data.IHolder; import bjc.utils.data.IPair; +import bjc.utils.data.Pair; /** * A lazy holder of two values @@ -22,7 +25,50 @@ public class LazyPair implements IPair { /** * The backing store for this pair */ - protected LazyHolder> del; + protected IHolder> del; + + /** + * Create a new blank lazy pair + */ + public LazyPair() { + del = new LazyHolder<>(new Pair<>()); + } + + /** + * Create a new lazy pair with the specified initial values + * + * @param leftVal + * The initial value for the left side of the pair + * @param rightVal + * The initial value for the right side of the pair + */ + public LazyPair(L leftVal, R rightVal) { + del = new LazyHolder<>(new Pair<>(leftVal, rightVal)); + } + + /** + * Create a new lazy pair with the specified sources for initial values + * + * @param leftValSrc + * The function to call for the left initial value + * @param rightValSrc + * The function to call for the right initial value + */ + public LazyPair(Supplier leftValSrc, Supplier rightValSrc) { + del = new LazyHolder<>(() -> { + return new Pair<>(leftValSrc.get(), rightValSrc.get()); + }); + } + + /** + * Create a new lazy pair with a specified internal delegate + * + * @param deleg + * The internal delegate for the pair + */ + private LazyPair(IHolder> deleg) { + del = deleg; + } /* * (non-Javadoc) @@ -33,7 +79,10 @@ public class LazyPair implements IPair { @Override public IPair apply(Function lf, Function rf) { - return del.unwrap((par) -> par.apply(lf, rf)); + IHolder> newPair = + del.map(par -> par.apply(lf, rf)); + + return new LazyPair<>(newPair); } /* -- cgit v1.2.3