summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils
diff options
context:
space:
mode:
authorstudent <student@localhost>2018-03-02 11:54:36 -0500
committerstudent <student@localhost>2018-03-02 11:54:36 -0500
commitcff383ea8c42cdd224ae29d5274ca001b7513559 (patch)
tree4adb03ab8c8fb8f31790d5f608718e017af5861b /base/src/main/java/bjc/utils
parented51142cd7abaaeedcb9ef1439f26690b57ce839 (diff)
Add an additional toggle type
Diffstat (limited to 'base/src/main/java/bjc/utils')
-rw-r--r--base/src/main/java/bjc/utils/data/OneWayToggle.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/data/OneWayToggle.java b/base/src/main/java/bjc/utils/data/OneWayToggle.java
new file mode 100644
index 0000000..edec4dd
--- /dev/null
+++ b/base/src/main/java/bjc/utils/data/OneWayToggle.java
@@ -0,0 +1,53 @@
+package bjc.utils.data;
+
+/**
+ * A toggle that will only give the first value once, only yielding the second
+ * value afterwards.
+ *
+ * @author student
+ *
+ * @param <E>
+ * The type of value stored
+ */
+public class OneWayToggle<E> implements Toggle<E> {
+ private E first;
+ private E second;
+
+ private boolean gotFirst;
+
+ /**
+ * Create a new one-way toggle
+ *
+ * @param first
+ * The value to offer first, and only once
+ * @param second
+ * The value to offer second and repeatedly
+ */
+ public OneWayToggle(E first, E second) {
+ this.first = first;
+
+ this.second = second;
+ }
+
+ @Override
+ public E get() {
+ if (gotFirst)
+ return second;
+
+ gotFirst = true;
+ return first;
+ }
+
+ @Override
+ public E peek() {
+ if (gotFirst)
+ return second;
+ return first;
+ }
+
+ @Override
+ public void set(boolean gotFirst) {
+ this.gotFirst = gotFirst;
+ }
+
+}