diff options
Diffstat (limited to 'src/main/model/ObservedData.java')
-rw-r--r-- | src/main/model/ObservedData.java | 47 |
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; + } +} |