diff options
| author | bjculkin <bjculkin@BECK-DZ9BJB2.wvu-ad.wvu.edu> | 2018-04-11 15:29:25 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@BECK-DZ9BJB2.wvu-ad.wvu.edu> | 2018-04-11 15:29:25 -0400 |
| commit | dcb6c2159446135a142cad41eec185bb24c45bfb (patch) | |
| tree | 965dcc07fc0256a9a8254d5f968e316a1e928617 /CSMath/src/bezier/Holder.java | |
| parent | 8ecf52b6c5821e5c3de8a0f5c9e7ed3e357cb282 (diff) | |
Split #9 into one class/file
Diffstat (limited to 'CSMath/src/bezier/Holder.java')
| -rw-r--r-- | CSMath/src/bezier/Holder.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/CSMath/src/bezier/Holder.java b/CSMath/src/bezier/Holder.java new file mode 100644 index 0000000..d6deba0 --- /dev/null +++ b/CSMath/src/bezier/Holder.java @@ -0,0 +1,112 @@ +package bezier;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.function.Consumer;
+
+/**
+ * Dummy class for holding a value, and being notified when it changes.
+ *
+ * Essentially a pointer with change notifications.
+ *
+ * @author bjculkin
+ *
+ * @param <E>
+ * The type of held value.
+ */
+public class Holder<E> {
+ /*
+ * The held value.
+ */
+ private E val;
+
+ /*
+ * The listeners to notify.
+ */
+ private final List<Consumer<E>> listeners;
+
+ /**
+ * Create a new holder holding nothing.
+ */
+ public Holder() {
+ listeners = new LinkedList<>();
+ }
+
+ /**
+ * Create a new holder holding a specific value.
+ *
+ * @param val
+ * The value being held.
+ */
+ public Holder(E val) {
+ this();
+
+ this.val = val;
+ }
+
+ /**
+ * Get the contained value.
+ *
+ * @return The contained value.
+ */
+ public E getVal() {
+ return val;
+ }
+
+ /**
+ * Set the contained value, and notify listeners.
+ *
+ * @param val
+ * The new value.
+ */
+ public void setVal(E val) {
+ this.val = val;
+
+ /*
+ * Notify listeners.
+ */
+ for (Consumer<E> listen : listeners) {
+ listen.accept(val);
+ }
+ }
+
+ /**
+ * Add a listener.
+ *
+ * @param listen
+ * The listener to add.
+ */
+ public void addHolderListener(Consumer<E> listen) {
+ listeners.add(listen);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((val == null) ? 0 : val.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ Holder<?> other = (Holder<?>) obj;
+ if (val == null) {
+ if (other.val != null)
+ return false;
+ } else if (!val.equals(other.val))
+ return false;
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "Holder [val=" + val + "]";
+ }
+}
\ No newline at end of file |
