diff options
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(); + } +} |
