diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-28 16:22:31 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-28 16:22:31 -0400 |
| commit | a100f2a0d71f37320fe0f73be0d6d65094b60eb0 (patch) | |
| tree | f2b0b1765df0817587bfe7098d9ece258927fda6 /BJC-Utils2/src | |
| parent | df972ae7dbdf051268c5cf7754e07c504524b197 (diff) | |
Ensure supplier don't materialize more than once
Diffstat (limited to 'BJC-Utils2/src')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 2 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java | 6 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java | 34 |
3 files changed, 38 insertions, 4 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java index 112a503..22f948c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -43,7 +43,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * The source of a value to use */ public Lazy(Supplier<ContainedType> supp) { - valueSupplier = supp; + valueSupplier = new SingleSupplier<>(supp); valueMaterialized = false; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java index e0b39cc..490c4fc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -29,7 +29,7 @@ public class LazyPair<LeftType, RightType> private boolean rightMaterialized; /** - * Create a new lazy pair, using the set value s + * Create a new lazy pair, using the set values * * @param leftVal * The value for the left side of the pair @@ -54,8 +54,8 @@ public class LazyPair<LeftType, RightType> */ public LazyPair(Supplier<LeftType> leftSupp, Supplier<RightType> rightSupp) { - leftSupplier = leftSupp; - rightSupplier = rightSupp; + leftSupplier = new SingleSupplier<>(leftSupp); + rightSupplier = new SingleSupplier<>(rightSupp); leftMaterialized = false; rightMaterialized = false; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java new file mode 100644 index 0000000..016f492 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -0,0 +1,34 @@ +package bjc.utils.data; + +import java.util.function.Supplier; + +public class SingleSupplier<T> implements Supplier<T> { + private Supplier<T> source; + + private boolean gotten; + + private long id; + + private static long nextID = 0; + + public SingleSupplier(Supplier<T> supp) { + source = supp; + + gotten = false; + + id = nextID++; + } + + @Override + public T get() { + if (gotten == true) { + throw new IllegalStateException( + "Attempted to get value more than once" + + " from single supplier #" + id); + } + + gotten = true; + + return source.get(); + } +} |
