blob: c4245284431ab53ce6b9ca371fe9460d60a2ea17 (
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
|
package cgeo.geocaching.utils;
/**
* Synchronized implementation of the {@link ISubject} interface with an added pull interface.
*
* @param <T>
* the kind of data to observe
*/
public class MemorySubject<T> extends Subject<T> {
/**
* The latest version of the observed data.
* <p/>
* A child class implementation may want to set this field from its constructors, in case early observers request
* the data before it got a chance to get updated. Otherwise, <code>null</code> will be returned until updated
* data is available.
*/
private T memory;
@Override
public synchronized boolean addObserver(final IObserver<? super T> observer) {
final boolean added = super.addObserver(observer);
if (added && memory != null) {
observer.update(memory);
}
return added;
}
@Override
public synchronized boolean notifyObservers(final T data) {
memory = data;
return super.notifyObservers(data);
}
/**
* Get the memorized version of the data.
*
* @return the initial data set by the subject (which may be <code>null</code>),
* or the updated data if it is available
*/
public synchronized T getMemory() {
return memory;
}
}
|