summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-04-07 16:06:18 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-04-07 16:06:18 -0400
commitf4baa925b0b5590bc8b12ba5f32e0218384c8efc (patch)
tree6c8f0eceaeaabc807e8cfd17e13bba86b8981970 /BJC-Utils2/src/main/java/bjc/utils/data
parent7692fa077a84972231948354d3f0de99f27a9ad7 (diff)
Add simple toggle values
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java76
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java35
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java58
3 files changed, 169 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java b/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java
new file mode 100644
index 0000000..68399a0
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java
@@ -0,0 +1,76 @@
+package bjc.utils.data;
+
+/**
+ * A simple {@link ValueToggle} that swaps between true and false.
+ *
+ * @author EVE
+ *
+ */
+public class BooleanToggle implements Toggle<Boolean> {
+ private boolean val;
+
+ /**
+ * Create a new, initially false, flip-flop.
+ */
+ public BooleanToggle() {
+ this(false);
+ }
+
+ /**
+ * Create a flip-flop with the specified initial value.
+ *
+ * @param initial
+ * The initial value of the flip-flop.
+ */
+ public BooleanToggle(boolean initial) {
+ val = initial;
+ }
+
+ @Override
+ public Boolean get() {
+ boolean res = val;
+
+ val = !res;
+
+ return res;
+ }
+
+ @Override
+ public Boolean peek() {
+ return val;
+ }
+
+ @Override
+ public void set(boolean vl) {
+ val = vl;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+
+ int result = 1;
+
+ result = prime * result + (val ? 1231 : 1237);
+
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj) return true;
+ if(obj == null) return false;
+ if(!(obj instanceof BooleanToggle)) return false;
+
+ BooleanToggle other = (BooleanToggle) obj;
+
+ if(val != other.val) return false;
+
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("BooleanToggle [val=%s]", val);
+ }
+} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java b/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java
new file mode 100644
index 0000000..8ebc4d8
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java
@@ -0,0 +1,35 @@
+package bjc.utils.data;
+
+/**
+ * A stateful holder that swaps between two values of the same type.
+ *
+ * @author EVE
+ *
+ * @param <E>
+ * The value stored in the toggle.
+ */
+public interface Toggle<E> {
+ /**
+ * Retrieve the currently-aligned value of this toggle, and swap the
+ * alignment.
+ *
+ * @return The previously-aligned value.
+ */
+ E get();
+
+ /**
+ * Retrieve the currently-aligned value without altering the alignment.
+ *
+ * @return The currently-aligned value.
+ */
+ E peek();
+
+ /**
+ * Change the alignment of the toggle.
+ *
+ * @param isLeft
+ * Whether the toggle should be left-aligned or not.
+ */
+ void set(boolean isLeft);
+
+} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java b/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java
new file mode 100644
index 0000000..5b5cb83
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java
@@ -0,0 +1,58 @@
+package bjc.utils.data;
+
+/**
+ * A simple implementation of {@link Toggle}.
+ *
+ * @author EVE
+ *
+ * @param <E>
+ * The type of value to toggle between.
+ */
+public class ValueToggle<E> implements Toggle<E> {
+ private final E lft;
+ private final E rght;
+
+ private BooleanToggle alignment;
+
+ /**
+ * Create a new toggle.
+ *
+ * All toggles start right-aligned.
+ *
+ * @param left
+ * The value when the toggle is left-aligned.
+ *
+ * @param right
+ * The value when the toggle is right-aligned.
+ */
+ public ValueToggle(E left, E right) {
+ lft = left;
+
+ rght = right;
+
+ alignment = new BooleanToggle();
+ }
+
+ @Override
+ public E get() {
+ if(alignment.get()) {
+ return lft;
+ } else {
+ return rght;
+ }
+ }
+
+ @Override
+ public E peek() {
+ if(alignment.peek()) {
+ return lft;
+ } else {
+ return rght;
+ }
+ }
+
+ @Override
+ public void set(boolean isLeft) {
+ alignment.set(isLeft);
+ }
+}