From a100f2a0d71f37320fe0f73be0d6d65094b60eb0 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 28 Jul 2016 16:22:31 -0400 Subject: Ensure supplier don't materialize more than once --- .../main/java/bjc/utils/data/SingleSupplier.java | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java') 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 implements Supplier { + private Supplier source; + + private boolean gotten; + + private long id; + + private static long nextID = 0; + + public SingleSupplier(Supplier 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(); + } +} -- cgit v1.2.3