You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2010/08/18 23:28:06 UTC
svn commit: r986954 - in /commons/proper/lang/trunk/src:
main/java/org/apache/commons/lang3/event/EventListenerSupport.java
test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
Author: mbenson
Date: Wed Aug 18 21:28:05 2010
New Revision: 986954
URL: http://svn.apache.org/viewvc?rev=986954&view=rev
Log:
provide read access to the registered listener instances
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java?rev=986954&r1=986953&r2=986954&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java Wed Aug 18 21:28:05 2010
@@ -17,6 +17,7 @@
package org.apache.commons.lang3.event;
+import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@@ -69,6 +70,11 @@ public class EventListenerSupport<L>
private final L proxy;
/**
+ * Empty typed array for #getListeners().
+ */
+ private final L[] prototypeArray;
+
+ /**
* Creates an EventListenerSupport object which supports the specified
* listener type.
*
@@ -128,6 +134,9 @@ public class EventListenerSupport<L>
proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader,
new Class[]{listenerInterface},
new ProxyInvocationHandler()));
+ @SuppressWarnings("unchecked")
+ final L[] prototypeArray = (L[]) Array.newInstance(listenerInterface, 0);
+ this.prototypeArray = prototypeArray;
}
/**
@@ -186,6 +195,16 @@ public class EventListenerSupport<L>
}
/**
+ * Get an array containing the currently registered listeners.
+ * Modification to this array's elements will have no effect on the
+ * {@link EventListenerSupport} instance.
+ * @return L[]
+ */
+ public L[] getListeners() {
+ return listeners.toArray(prototypeArray);
+ }
+
+ /**
* An invocation handler used to dispatch the event(s) to all the listeners.
*/
private class ProxyInvocationHandler implements InvocationHandler
Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java?rev=986954&r1=986953&r2=986954&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java Wed Aug 18 21:28:05 2010
@@ -24,6 +24,8 @@ import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
+import org.easymock.EasyMock;
+
/**
* @since 3.0
* @version $Id$
@@ -111,6 +113,28 @@ public class EventListenerSupportTest ex
assertEquals(listenerSupport.getListenerCount(), 0);
}
+ public void testGetListeners() {
+ final EventListenerSupport<ActionListener> listenerSupport = EventListenerSupport.create(ActionListener.class);
+
+ ActionListener[] listeners = listenerSupport.getListeners();
+ assertEquals(0, listeners.length);
+ ActionListener[] empty = listeners;
+ //for fun, show that the same empty instance is used
+ assertSame(empty, listenerSupport.getListeners());
+
+ ActionListener listener1 = EasyMock.createNiceMock(ActionListener.class);
+ listenerSupport.addListener(listener1);
+ assertEquals(1, listenerSupport.getListeners().length);
+ assertEquals(ActionListener.class, listenerSupport.getListeners().getClass().getComponentType());
+ ActionListener listener2 = EasyMock.createNiceMock(ActionListener.class);
+ listenerSupport.addListener(listener2);
+ assertEquals(2, listenerSupport.getListeners().length);
+ listenerSupport.removeListener(listener1);
+ assertEquals(1, listenerSupport.getListeners().length);
+ listenerSupport.removeListener(listener2);
+ assertSame(empty, listenerSupport.getListeners());
+ }
+
private void addDeregisterListener(final EventListenerSupport<ActionListener> listenerSupport)
{
listenerSupport.addListener(new ActionListener()