From 5e7c489921f3141e62f229646d7f16c7b691be3d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 May 2011 00:18:55 +0200 Subject: Add 2-argument events git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@43 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/util/Event2.java | 23 +++++++++ src/jrummikub/util/IEvent2.java | 6 +++ src/jrummikub/util/IListener2.java | 5 ++ test/jrummikub/util/Event2Test.java | 95 +++++++++++++++++++++++++++++++++++++ test/jrummikub/util/EventTest.java | 1 - 5 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/jrummikub/util/Event2.java create mode 100644 src/jrummikub/util/IEvent2.java create mode 100644 src/jrummikub/util/IListener2.java create mode 100644 test/jrummikub/util/Event2Test.java diff --git a/src/jrummikub/util/Event2.java b/src/jrummikub/util/Event2.java new file mode 100644 index 0000000..19d2d58 --- /dev/null +++ b/src/jrummikub/util/Event2.java @@ -0,0 +1,23 @@ +package jrummikub.util; + +import java.util.HashSet; + +public class Event2 implements IEvent2 { +private HashSet> listeners = new HashSet>(); + + @Override + public void add(IListener2 listener) { + listeners.add(listener); + } + + @Override + public void remove(IListener2 listener) { + listeners.remove(listener); + } + + public void fire(T1 value1, T2 value2) { + for (IListener2 listener : listeners) { + listener.fire(value1, value2); + } + } +} diff --git a/src/jrummikub/util/IEvent2.java b/src/jrummikub/util/IEvent2.java new file mode 100644 index 0000000..f677a2d --- /dev/null +++ b/src/jrummikub/util/IEvent2.java @@ -0,0 +1,6 @@ +package jrummikub.util; + +public interface IEvent2 { + public void add(IListener2 listener); + public void remove(IListener2 listener); +} diff --git a/src/jrummikub/util/IListener2.java b/src/jrummikub/util/IListener2.java new file mode 100644 index 0000000..b1832c1 --- /dev/null +++ b/src/jrummikub/util/IListener2.java @@ -0,0 +1,5 @@ +package jrummikub.util; + +public interface IListener2 { + public void fire(T1 value1, T2 value2); +} diff --git a/test/jrummikub/util/Event2Test.java b/test/jrummikub/util/Event2Test.java new file mode 100644 index 0000000..cbfecd5 --- /dev/null +++ b/test/jrummikub/util/Event2Test.java @@ -0,0 +1,95 @@ +package jrummikub.util; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class Event2Test { + + int fired, fired2, fired3, fired4; + + @Test + public void singleListener() { + fired = 0; + fired2 = 0; + Event2 testEvent = new Event2(); + testEvent.add(new IListener2() { + + @Override + public void fire(Integer n, Integer m) { + fired += n; + fired2 += m; + } + }); + assertEquals(fired, 0); + assertEquals(fired2, 0); + testEvent.fire(10, 5); + assertEquals(fired, 10); + assertEquals(fired2, 5); + testEvent.fire(20, 45); + assertEquals(fired, 30); + assertEquals(fired2, 50); + } + + @Test + public void twoListeners() { + fired = 0; + fired2 = 0; + fired3 = 0; + fired4 = 0; + Event2 testEvent = new Event2(); + testEvent.add(new IListener2() { + + @Override + public void fire(Integer n, Integer m) { + fired += n; + fired2 += m; + } + }); + testEvent.add(new IListener2() { + + @Override + public void fire(Integer n, Integer m) { + fired3 -= n; + fired4 -= m; + } + }); + assertEquals(fired, 0); + assertEquals(fired2, 0); + assertEquals(fired3, 0); + assertEquals(fired4, 0); + testEvent.fire(5, 10); + assertEquals(fired, 5); + assertEquals(fired2, 10); + assertEquals(fired3, -5); + assertEquals(fired4, -10); + } + + @Test public void removeListener() { + fired = 0; + fired2 = 0; + Event2 testEvent = new Event2(); + testEvent.add(new IListener2() { + + @Override + public void fire(Integer n, Integer m) { + fired += n; + fired2 += m; + } + }); + IListener2 rem = new IListener2() { + + @Override + public void fire(Integer n, Integer m) { + fail(); + } + }; + assertEquals(fired, 0); + assertEquals(fired2, 0); + testEvent.add(rem); + testEvent.remove(rem); + testEvent.fire(10, 20); + assertEquals(fired, 10); + assertEquals(fired2, 20); + } +} diff --git a/test/jrummikub/util/EventTest.java b/test/jrummikub/util/EventTest.java index 0802247..0595306 100644 --- a/test/jrummikub/util/EventTest.java +++ b/test/jrummikub/util/EventTest.java @@ -1,6 +1,5 @@ package jrummikub.util; -import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; -- cgit v1.2.3