From a48071b0d489420bfdc0c2ed7254d1dba243f700 Mon Sep 17 00:00:00 2001 From: Matthew Aguirre Date: Mon, 11 May 2015 13:22:50 -0400 Subject: [PATCH] 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. --- .../lang3/event/EventListenerSupport.java | 21 ++++++++++++++++++- .../lang3/event/EventListenerSupportTest.java | 20 ++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) 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 0f51796fbaf..4f3a9a77ac3 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 L fire() { * null. */ 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 allowDuplicate is false. + * + * @param listener the event listener (may not be null). + * @param allowDuplicate the flag for determining if duplicate listener + * objects are allowed to be registered. + * + * @throws NullPointerException if listener is + * null. + */ + 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); + } } /** 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 5f65125f610..d8772a30908 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 @@ */ public class EventListenerSupportTest { + @Test + public void testAddListenerNoDuplicates() { + final EventListenerSupport 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() {