aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/ObservedData.java
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@student.cs.ubc.ca>2023-11-23 08:09:01 +0800
committerYuuta Liang <yuutaw@student.cs.ubc.ca>2023-11-23 08:09:01 +0800
commit65ea6c17a0c1348aa9ef4e158102ddf173936882 (patch)
tree7615366f76b6c94f46d8039aa20091f9ccd5609a /src/main/model/ObservedData.java
parentb94b18c133f06cb176d8aa8bb40a8e24918d9ed6 (diff)
downloadjca-65ea6c17a0c1348aa9ef4e158102ddf173936882.tar
jca-65ea6c17a0c1348aa9ef4e158102ddf173936882.tar.gz
jca-65ea6c17a0c1348aa9ef4e158102ddf173936882.tar.bz2
jca-65ea6c17a0c1348aa9ef4e158102ddf173936882.zip
Add GUI
Signed-off-by: Yuuta Liang <yuutaw@student.cs.ubc.ca>
Diffstat (limited to 'src/main/model/ObservedData.java')
-rw-r--r--src/main/model/ObservedData.java47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/model/ObservedData.java b/src/main/model/ObservedData.java
new file mode 100644
index 0000000..5ad91a3
--- /dev/null
+++ b/src/main/model/ObservedData.java
@@ -0,0 +1,47 @@
+package model;
+
+/**
+ * A single observed data that notifies the observer once changed.
+ */
+public class ObservedData<T> {
+ /**
+ * The data.
+ */
+ private T data;
+
+ /**
+ * The observer.
+ */
+ private Observer<T> acceptor;
+
+ /**
+ * EFFECTS: Init with the given initial value and observer.
+ * REQUIRES: acceptor != null
+ */
+ public ObservedData(T initialValue, Observer<T> acceptor) {
+ this.data = initialValue;
+ this.acceptor = acceptor;
+ }
+
+ /**
+ * EFFECTS: Set the data and notify the observer with the new data and DIRECTION_CHANGE + INDEX_NOT_IN_LIST.
+ */
+ public void set(T data) {
+ this.data = data;
+ acceptor.accept(data, Observer.DIRECTION_CHANGE, Observer.INDEX_NOT_IN_LIST);
+ }
+
+ /**
+ * EFFECTS: Get the data.
+ */
+ public T get() {
+ return data;
+ }
+
+ /**
+ * EFFECTS: Get the observer.
+ */
+ public Observer<T> getAcceptor() {
+ return acceptor;
+ }
+}