aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2012-04-27 12:10:34 +0200
committerSamuel Tardieu <sam@rfc1149.net>2012-04-27 12:10:34 +0200
commitb924e0bbd2a035d7b65ad245777dfeb6c3ef3d06 (patch)
tree1642fbb626e76eb2646b5bcb34b37decb22156de /main/src
parentedc7a5ee58e24faea705c31cb8e693cbfb3f449a (diff)
downloadcgeo-b924e0bbd2a035d7b65ad245777dfeb6c3ef3d06.zip
cgeo-b924e0bbd2a035d7b65ad245777dfeb6c3ef3d06.tar.gz
cgeo-b924e0bbd2a035d7b65ad245777dfeb6c3ef3d06.tar.bz2
Synchronized implementation of ISubject with pull capabilities
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/utils/MemorySubject.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/utils/MemorySubject.java b/main/src/cgeo/geocaching/utils/MemorySubject.java
new file mode 100644
index 0000000..2720fc6
--- /dev/null
+++ b/main/src/cgeo/geocaching/utils/MemorySubject.java
@@ -0,0 +1,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.
+ */
+ protected 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 T getMemory() {
+ return memory;
+ }
+
+}