diff options
-rw-r--r-- | src/jrummikub/util/Connection.java | 5 | ||||
-rw-r--r-- | src/jrummikub/util/Event.java | 3 | ||||
-rw-r--r-- | src/jrummikub/util/Event1.java | 3 | ||||
-rw-r--r-- | src/jrummikub/util/Event2.java | 3 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent.java | 2 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent1.java | 2 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent2.java | 2 | ||||
-rw-r--r-- | test/jrummikub/util/Event1Test.java | 25 | ||||
-rw-r--r-- | test/jrummikub/util/Event2Test.java | 29 | ||||
-rw-r--r-- | test/jrummikub/util/EventTest.java | 25 |
10 files changed, 93 insertions, 6 deletions
diff --git a/src/jrummikub/util/Connection.java b/src/jrummikub/util/Connection.java new file mode 100644 index 0000000..b6a5a07 --- /dev/null +++ b/src/jrummikub/util/Connection.java @@ -0,0 +1,5 @@ +package jrummikub.util; + +public interface Connection { + public void remove(); +} diff --git a/src/jrummikub/util/Event.java b/src/jrummikub/util/Event.java index 64a70b3..f76b861 100644 --- a/src/jrummikub/util/Event.java +++ b/src/jrummikub/util/Event.java @@ -7,8 +7,9 @@ public class Event implements IEvent { private HashSet<IListener> listeners = new HashSet<IListener>(); @Override - public void add(IListener listener) { + public Connection add(IListener listener) { listeners.add(listener); + return null; } @Override diff --git a/src/jrummikub/util/Event1.java b/src/jrummikub/util/Event1.java index 3f5056d..ff6c037 100644 --- a/src/jrummikub/util/Event1.java +++ b/src/jrummikub/util/Event1.java @@ -12,8 +12,9 @@ public class Event1<T> implements IEvent1<T> { private HashSet<IListener1<T>> listeners = new HashSet<IListener1<T>>(); @Override - public void add(IListener1<T> listener) { + public Connection add(IListener1<T> listener) { listeners.add(listener); + return null; } @Override diff --git a/src/jrummikub/util/Event2.java b/src/jrummikub/util/Event2.java index 764004f..4b6575f 100644 --- a/src/jrummikub/util/Event2.java +++ b/src/jrummikub/util/Event2.java @@ -14,8 +14,9 @@ public class Event2<T1, T2> implements IEvent2<T1, T2> { private HashSet<IListener2<T1, T2>> listeners = new HashSet<IListener2<T1, T2>>(); @Override - public void add(IListener2<T1, T2> listener) { + public Connection add(IListener2<T1, T2> listener) { listeners.add(listener); + return null; } @Override diff --git a/src/jrummikub/util/IEvent.java b/src/jrummikub/util/IEvent.java index 3aa13ba..52893f2 100644 --- a/src/jrummikub/util/IEvent.java +++ b/src/jrummikub/util/IEvent.java @@ -8,7 +8,7 @@ public interface IEvent { * @param listener * target listener */ - public void add(IListener listener); + public Connection add(IListener listener); /** * Stop publishing events to a given listener diff --git a/src/jrummikub/util/IEvent1.java b/src/jrummikub/util/IEvent1.java index 52b9bf4..fa031b8 100644 --- a/src/jrummikub/util/IEvent1.java +++ b/src/jrummikub/util/IEvent1.java @@ -13,7 +13,7 @@ public interface IEvent1<T> { * @param listener * target listener */ - public void add(IListener1<T> listener); + public Connection add(IListener1<T> listener); /** * Stop publishing events to a given listener diff --git a/src/jrummikub/util/IEvent2.java b/src/jrummikub/util/IEvent2.java index 967b65c..70bb3e6 100644 --- a/src/jrummikub/util/IEvent2.java +++ b/src/jrummikub/util/IEvent2.java @@ -15,7 +15,7 @@ public interface IEvent2<T1, T2> { * @param listener * target listener */ - public void add(IListener2<T1, T2> listener); + public Connection add(IListener2<T1, T2> listener); /** * Stop publishing events to a given listener diff --git a/test/jrummikub/util/Event1Test.java b/test/jrummikub/util/Event1Test.java index 3f8839d..c3f388e 100644 --- a/test/jrummikub/util/Event1Test.java +++ b/test/jrummikub/util/Event1Test.java @@ -80,4 +80,29 @@ public class Event1Test { testEvent.emit(10); assertEquals(fired, 10); } + + @Test + public void removeListenerByConnection() { + fired = 0; + Event1<Integer> testEvent = new Event1<Integer>(); + testEvent.add(new IListener1<Integer>() { + + @Override + public void handle(Integer n) { + fired += n; + + } + }); + IListener1<Integer> rem = new IListener1<Integer>() { + + @Override + public void handle(Integer n) { + fail(); + } + }; + Connection connection = testEvent.add(rem); + connection.remove(); + testEvent.emit(10); + assertEquals(fired, 10); + } } diff --git a/test/jrummikub/util/Event2Test.java b/test/jrummikub/util/Event2Test.java index 6d1958e..c258ddf 100644 --- a/test/jrummikub/util/Event2Test.java +++ b/test/jrummikub/util/Event2Test.java @@ -93,4 +93,33 @@ public class Event2Test { assertEquals(fired, 10); assertEquals(fired2, 20); } + + @Test + public void removeListenerByConnection() { + fired = 0; + fired2 = 0; + Event2<Integer, Integer> testEvent = new Event2<Integer, Integer>(); + testEvent.add(new IListener2<Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m) { + fired += n; + fired2 += m; + } + }); + IListener2<Integer, Integer> rem = new IListener2<Integer, Integer>() { + + @Override + public void handle(Integer n, Integer m) { + fail(); + } + }; + assertEquals(fired, 0); + assertEquals(fired2, 0); + Connection connection = testEvent.add(rem); + connection.remove(); + testEvent.emit(10, 20); + assertEquals(fired, 10); + assertEquals(fired2, 20); + } } diff --git a/test/jrummikub/util/EventTest.java b/test/jrummikub/util/EventTest.java index 15bd632..f8c505c 100644 --- a/test/jrummikub/util/EventTest.java +++ b/test/jrummikub/util/EventTest.java @@ -79,5 +79,30 @@ public class EventTest { testEvent.emit(); assertTrue(fired); } + + @Test + public void removeListenerByConnection() { + fired = false; + Event testEvent = new Event(); + testEvent.add(new IListener() { + + @Override + public void handle() { + fired = true; + + } + }); + IListener rem = new IListener() { + + @Override + public void handle() { + fail(); + } + }; + Connection connection = testEvent.add(rem); + connection.remove(); + testEvent.emit(); + assertTrue(fired); + } } |