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/main/java/bjc/utils/data/SingleSupplier.java | |
| parent | df972ae7dbdf051268c5cf7754e07c504524b197 (diff) | |
Ensure supplier don't materialize more than once
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java | 34 |
1 files changed, 34 insertions, 0 deletions
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(); + } +} |
