From dcb6c2159446135a142cad41eec185bb24c45bfb Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 11 Apr 2018 15:29:25 -0400 Subject: Split #9 into one class/file --- CSMath/src/bezier/Holder.java | 112 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 CSMath/src/bezier/Holder.java (limited to 'CSMath/src/bezier/Holder.java') 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 + * The type of held value. + */ +public class Holder { + /* + * The held value. + */ + private E val; + + /* + * The listeners to notify. + */ + private final List> 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 listen : listeners) { + listen.accept(val); + } + } + + /** + * Add a listener. + * + * @param listen + * The listener to add. + */ + public void addHolderListener(Consumer 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 -- cgit v1.2.3