Repository: commons-lang Updated Branches: refs/heads/master 61bec859a -> 19dace1b8
Added new addListener() method that takes a boolean for if duplicates are allowed. Added test method to make sure that duplicate listeners are not added. Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/a48071b0 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/a48071b0 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/a48071b0 Branch: refs/heads/master Commit: a48071b0d489420bfdc0c2ed7254d1dba243f700 Parents: 71d7c32 Author: Matthew Aguirre <matt.agui...@gmail.com> Authored: Mon May 11 13:22:50 2015 -0400 Committer: Matthew Aguirre <matt.agui...@gmail.com> Committed: Mon May 11 13:22:50 2015 -0400 ---------------------------------------------------------------------- .../lang3/event/EventListenerSupport.java | 21 +++++++++++++++++++- .../lang3/event/EventListenerSupportTest.java | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/a48071b0/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java index 0f51796..4f3a9a7 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java +++ b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java @@ -179,8 +179,27 @@ public class EventListenerSupport<L> implements Serializable { * <code>null</code>. */ public void addListener(final L listener) { + addListener(listener, true); + } + + /** + * Registers an event listener. Will not add a pre-existing listener + * object to the list if <code>allowDuplicate</code> is false. + * + * @param listener the event listener (may not be <code>null</code>). + * @param allowDuplicate the flag for determining if duplicate listener + * objects are allowed to be registered. + * + * @throws NullPointerException if <code>listener</code> is + * <code>null</code>. + */ + public void addListener(final L listener, boolean allowDuplicate) { Validate.notNull(listener, "Listener object cannot be null."); - listeners.add(listener); + if (allowDuplicate) { + listeners.add(listener); + } else if (!allowDuplicate && !listeners.contains(listener)) { + listeners.add(listener); + } } /** http://git-wip-us.apache.org/repos/asf/commons-lang/blob/a48071b0/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java index 5f65125..d8772a3 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java @@ -41,6 +41,26 @@ import org.junit.Test; */ public class EventListenerSupportTest { + @Test + public void testAddListenerNoDuplicates() { + final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); + + final VetoableChangeListener[] listeners = listenerSupport.getListeners(); + assertEquals(0, listeners.length); + assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType()); + final VetoableChangeListener[] empty = listeners; + //for fun, show that the same empty instance is used + assertSame(empty, listenerSupport.getListeners()); + + final VetoableChangeListener listener1 = EasyMock.createNiceMock(VetoableChangeListener.class); + listenerSupport.addListener(listener1); + assertEquals(1, listenerSupport.getListeners().length); + listenerSupport.addListener(listener1, false); + assertEquals(1, listenerSupport.getListeners().length); + listenerSupport.removeListener(listener1); + assertSame(empty, listenerSupport.getListeners()); + } + @Test(expected=NullPointerException.class) public void testAddNullListener() {