summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-30 10:03:24 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-30 10:03:24 -0400
commite47b798a05832c8326df953320cb507f74b66ed8 (patch)
tree588ad6d7a1afea9c37ea6f562e7255ac6c6f9c23 /BJC-Utils2/src/main
parent3b28d379d404b042c8e60cd84d7a44821792ea36 (diff)
Optimized a case that would've caused premature pair materialization
Diffstat (limited to 'BJC-Utils2/src/main')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java53
1 files 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<L, R> implements IPair<L, R> {
/**
* The backing store for this pair
*/
- protected LazyHolder<IPair<L, R>> del;
+ protected IHolder<IPair<L, R>> 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<L> leftValSrc, Supplier<R> 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<IPair<L, R>> deleg) {
+ del = deleg;
+ }
/*
* (non-Javadoc)
@@ -33,7 +79,10 @@ public class LazyPair<L, R> implements IPair<L, R> {
@Override
public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf,
Function<R, R2> rf) {
- return del.unwrap((par) -> par.apply(lf, rf));
+ IHolder<IPair<L2, R2>> newPair =
+ del.map(par -> par.apply(lf, rf));
+
+ return new LazyPair<>(newPair);
}
/*