aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/ObservedData.java
blob: 5ad91a3dff775e94e60b824f2731fb15ce162625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
    }
}