diff options
| -rw-r--r-- | main/src/cgeo/geocaching/utils/IObserver.java | 22 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/ISubject.java | 57 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/MemorySubject.java | 45 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/Subject.java | 76 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/MemorySubjectTest.java | 79 |
5 files changed, 0 insertions, 279 deletions
diff --git a/main/src/cgeo/geocaching/utils/IObserver.java b/main/src/cgeo/geocaching/utils/IObserver.java deleted file mode 100644 index bfcc798..0000000 --- a/main/src/cgeo/geocaching/utils/IObserver.java +++ /dev/null @@ -1,22 +0,0 @@ -package cgeo.geocaching.utils; - -/** - * Observer interface. - * <p/> - * An observer will receive updates about the observed object (implementing the {@link ISubject} interface) through its - * {@link #update(Object)} method. - * - * @param <T> - * the kind of data to observe - */ -public interface IObserver<T> { - - /** - * Called when an observed object has updated its data. - * - * @param data - * the updated data - */ - void update(final T data); - -} diff --git a/main/src/cgeo/geocaching/utils/ISubject.java b/main/src/cgeo/geocaching/utils/ISubject.java deleted file mode 100644 index c325db0..0000000 --- a/main/src/cgeo/geocaching/utils/ISubject.java +++ /dev/null @@ -1,57 +0,0 @@ -package cgeo.geocaching.utils; - -/** - * Interface for subjects objects. Those can be observed by objects implementing the {@link IObserver} interface. - * - * @param <T> - * the kind of data to observe - */ - -public interface ISubject<T> { - - /** - * Add an observer to the observers list. - * <p/> - * Observers will be notified with no particular order. - * - * @param observer - * the observer to add - * @return true if the observer has been added, false if it was present already - */ - public boolean addObserver(final IObserver<? super T> observer); - - /** - * Delete an observer from the observers list. - * - * @param observer - * the observer to remove - * @return true if the observer has been removed, false if it was not in the list of observers - */ - public boolean deleteObserver(final IObserver<? super T> observer); - - /** - * Number of observers currently observing the object. - * - * @return the number of observers - */ - public int sizeObservers(); - - /** - * Notify all the observers that new data is available. - * <p/> - * The {@link IObserver#update(Object)} method of each observer will be called with no particular order. - * - * @param data - * the updated data - * @return true if at least one observer was notified, false if there were no observers - */ - public boolean notifyObservers(final T data); - - /** - * Clear the observers list. - * - * @return true if there were observers before calling this method, false if the observers list was empty - */ - public boolean clearObservers(); - -} diff --git a/main/src/cgeo/geocaching/utils/MemorySubject.java b/main/src/cgeo/geocaching/utils/MemorySubject.java deleted file mode 100644 index c424528..0000000 --- a/main/src/cgeo/geocaching/utils/MemorySubject.java +++ /dev/null @@ -1,45 +0,0 @@ -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; - } - -} diff --git a/main/src/cgeo/geocaching/utils/Subject.java b/main/src/cgeo/geocaching/utils/Subject.java deleted file mode 100644 index b1754cc..0000000 --- a/main/src/cgeo/geocaching/utils/Subject.java +++ /dev/null @@ -1,76 +0,0 @@ -package cgeo.geocaching.utils; - -import java.util.LinkedHashSet; -import java.util.Set; - -/** - * Synchronized implementation of the {@link ISubject} interface. - * - * @param <T> - * the kind of data to observe - */ -public class Subject<T> implements ISubject<T> { - - /** - * Collection of observers. - */ - protected final Set<IObserver<? super T>> observers = new LinkedHashSet<IObserver<? super T>>(); - - @Override - public synchronized boolean addObserver(final IObserver<? super T> observer) { - final boolean added = observers.add(observer); - if (added && observers.size() == 1) { - onFirstObserver(); - } - return added; - } - - @Override - public synchronized boolean deleteObserver(final IObserver<? super T> observer) { - final boolean removed = observers.remove(observer); - if (removed && observers.isEmpty()) { - onLastObserver(); - } - return removed; - } - - @Override - public synchronized boolean notifyObservers(final T arg) { - final boolean nonEmpty = !observers.isEmpty(); - for (final IObserver<? super T> observer : observers) { - observer.update(arg); - } - return nonEmpty; - } - - @Override - public synchronized int sizeObservers() { - return observers.size(); - } - - @Override - public synchronized boolean clearObservers() { - final boolean nonEmpty = !observers.isEmpty(); - for (final IObserver<? super T> observer : observers) { - deleteObserver(observer); - } - return nonEmpty; - } - - /** - * Method called when the collection of observers goes from empty to non-empty. - * <p/> - * The default implementation does nothing and may be overwritten by child classes. - */ - protected void onFirstObserver() { - } - - /** - * Method called when the collection of observers goes from non-empty to empty. - * <p/> - * The default implementation does nothing and may be overwritten by child classes. - */ - protected void onLastObserver() { - } - -} diff --git a/tests/src/cgeo/geocaching/utils/MemorySubjectTest.java b/tests/src/cgeo/geocaching/utils/MemorySubjectTest.java deleted file mode 100644 index ee46950..0000000 --- a/tests/src/cgeo/geocaching/utils/MemorySubjectTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package cgeo.geocaching.utils; - -import android.test.AndroidTestCase; - -public class MemorySubjectTest extends AndroidTestCase { - - private static class Observer implements IObserver<Integer> { - private int times = 0; - private Integer value; - - @Override - public void update(final Integer data) { - value = data; - times++; - } - } - - private Observer observer; - private MemorySubject<Integer> memorySubject; - - @Override - public void setUp() throws Exception { - super.setUp(); - observer = new Observer(); - memorySubject = new MemorySubject<Integer>(); - } - - public void testInitial() { - assertNull(observer.value); - assertEquals(0, observer.times); - assertNull(memorySubject.getMemory()); - } - - public void testMemory() { - memorySubject.addObserver(observer); - memorySubject.notifyObservers(10); - assertEquals(Integer.valueOf(10), observer.value); - assertEquals(1, observer.times); - assertEquals(Integer.valueOf(10), memorySubject.getMemory()); - memorySubject.notifyObservers(20); - assertEquals(Integer.valueOf(20), observer.value); - assertEquals(2, observer.times); - assertEquals(Integer.valueOf(20), memorySubject.getMemory()); - } - - public void testAttach() { - memorySubject.notifyObservers(10); - assertNull(observer.value); - assertEquals(0, observer.times); - memorySubject.addObserver(observer); - assertEquals(Integer.valueOf(10), observer.value); - assertEquals(1, observer.times); - memorySubject.notifyObservers(20); - assertEquals(Integer.valueOf(20), observer.value); - assertEquals(2, observer.times); - } - - public void testDetach() { - memorySubject.addObserver(observer); - memorySubject.notifyObservers(10); - assertEquals(Integer.valueOf(10), observer.value); - assertEquals(1, observer.times); - assertEquals(Integer.valueOf(10), memorySubject.getMemory()); - memorySubject.deleteObserver(observer); - memorySubject.notifyObservers(20); - assertEquals(Integer.valueOf(10), observer.value); - assertEquals(1, observer.times); - assertEquals(Integer.valueOf(20), memorySubject.getMemory()); - } - - public void testMultiple() { - final Observer otherObserver = new Observer(); - memorySubject.addObserver(otherObserver); - testDetach(); - assertEquals(Integer.valueOf(20), otherObserver.value); - assertEquals(2, otherObserver.times); - } - -} |
