provide read access to the registered listener instances
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@986954 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
711337c3a9
commit
d2a3a2b1c6
|
@ -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;
|
||||
|
@ -68,6 +69,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -185,6 +194,16 @@ public class EventListenerSupport<L>
|
|||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
|
|
@ -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 extends TestCase
|
|||
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()
|
||||
|
|
Loading…
Reference in New Issue