Package org.apache.commons.lang3.event
Class EventListenerSupport<L>
- java.lang.Object
-
- org.apache.commons.lang3.event.EventListenerSupport<L>
-
- Type Parameters:
L- the type of event listener that is supported by this proxy.
- All Implemented Interfaces:
java.io.Serializable
public class EventListenerSupport<L> extends java.lang.Object implements java.io.SerializableManages a list of event listeners of a given generic type. This class providesaddListener(Object)andremoveListener(Object)methods for managing listeners, as well as afire()method for firing events to the listeners.For example, to support ActionEvents:
public class MyActionEventSource { private EventListenerSupport<ActionListener> actionListeners = EventListenerSupport.create(ActionListener.class); public void someMethodThatFiresAction() { ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "something"); actionListeners.fire().actionPerformed(e); } }Events are fired
Serializing an
EventListenerSupportinstance will result in any non-Serializablelisteners being silently dropped.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classEventListenerSupport.ProxyInvocationHandlerInvokes listeners throughEventListenerSupport.ProxyInvocationHandler.invoke(Object, Method, Object[])in the order added to the underlyingList.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<L>listenersHold the registered listeners.private L[]prototypeArrayEmpty typed array for #getListeners().private LproxyThe proxy representing the collection of listeners.private static longserialVersionUIDSerialization version
-
Constructor Summary
Constructors Modifier Constructor Description privateEventListenerSupport()Constructs a new EventListenerSupport instance.EventListenerSupport(java.lang.Class<L> listenerInterface)Constructs an EventListenerSupport object which supports the provided listener interface.EventListenerSupport(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)Constructs an EventListenerSupport object which supports the provided listener interface using the specified class loader to create the JDK dynamic proxy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(L listener)Adds an event listener.voidaddListener(L listener, boolean allowDuplicate)Adds an event listener.static <T> EventListenerSupport<T>create(java.lang.Class<T> listenerInterface)Creates an EventListenerSupport object which supports the specified listener type.protected java.lang.reflect.InvocationHandlercreateInvocationHandler()Creates theInvocationHandlerresponsible for calling to the managed listeners.private voidcreateProxy(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)Creates the proxy object.Lfire()Returns a proxy object which can be used to call listener methods on all of the registered event listeners.(package private) intgetListenerCount()Gets the number of registered listeners.L[]getListeners()Gets an array containing the currently registered listeners.private voidinitializeTransientFields(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)Initializes transient fields.private voidreadObject(java.io.ObjectInputStream objectInputStream)Deserializes the next object into this instance.voidremoveListener(L listener)Removes an event listener.private voidwriteObject(java.io.ObjectOutputStream objectOutputStream)Serializes this instance onto the given ObjectOutputStream.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization version- See Also:
- Constant Field Values
-
listeners
private java.util.List<L> listeners
Hold the registered listeners. This list is intentionally a thread-safe copy-on-write-array so that traversals over the list of listeners will be atomic.
-
proxy
private transient L proxy
The proxy representing the collection of listeners. Calls to this proxy object will be sent to all registered listeners.
-
prototypeArray
private transient L[] prototypeArray
Empty typed array for #getListeners().
-
-
Constructor Detail
-
EventListenerSupport
private EventListenerSupport()
Constructs a new EventListenerSupport instance.This constructor is needed for serialization.
-
EventListenerSupport
public EventListenerSupport(java.lang.Class<L> listenerInterface)
Constructs an EventListenerSupport object which supports the provided listener interface.- Parameters:
listenerInterface- the type of listener interface that will receive events posted using this class.- Throws:
java.lang.NullPointerException- iflistenerInterfaceisnull.java.lang.IllegalArgumentException- iflistenerInterfaceis not an interface.
-
EventListenerSupport
public EventListenerSupport(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)
Constructs an EventListenerSupport object which supports the provided listener interface using the specified class loader to create the JDK dynamic proxy.- Parameters:
listenerInterface- the listener interface.classLoader- the class loader.- Throws:
java.lang.NullPointerException- iflistenerInterfaceorclassLoaderisnull.java.lang.IllegalArgumentException- iflistenerInterfaceis not an interface.
-
-
Method Detail
-
create
public static <T> EventListenerSupport<T> create(java.lang.Class<T> listenerInterface)
Creates an EventListenerSupport object which supports the specified listener type.- Type Parameters:
T- the type of the listener interface- Parameters:
listenerInterface- the type of listener interface that will receive events posted using this class.- Returns:
- an EventListenerSupport object which supports the specified listener type.
- Throws:
java.lang.NullPointerException- iflistenerInterfaceisnull.java.lang.IllegalArgumentException- iflistenerInterfaceis not an interface.
-
addListener
public void addListener(L listener)
Adds an event listener.Listeners are called in the order added.
- Parameters:
listener- the event listener (may not benull).- Throws:
java.lang.NullPointerException- iflistenerisnull.
-
addListener
public void addListener(L listener, boolean allowDuplicate)
Adds an event listener. Will not add a pre-existing listener object to the list ifallowDuplicateis false.Listeners are called in the order added.
- Parameters:
listener- the event listener (may not benull).allowDuplicate- the flag for determining if duplicate listener objects are allowed to be registered.- Throws:
java.lang.NullPointerException- iflistenerisnull.- Since:
- 3.5
-
createInvocationHandler
protected java.lang.reflect.InvocationHandler createInvocationHandler()
Creates theInvocationHandlerresponsible for calling to the managed listeners. Subclasses can override to provide custom behavior.- Returns:
- ProxyInvocationHandler
-
createProxy
private void createProxy(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)
Creates the proxy object.- Parameters:
listenerInterface- the class of the listener interfaceclassLoader- the class loader to be used
-
fire
public L fire()
Returns a proxy object which can be used to call listener methods on all of the registered event listeners. All calls made to this proxy will be forwarded to all registered listeners.- Returns:
- a proxy object which can be used to call listener methods on all of the registered event listeners
-
getListenerCount
int getListenerCount()
Gets the number of registered listeners.- Returns:
- the number of registered listeners.
-
getListeners
public L[] getListeners()
Gets an array containing the currently registered listeners. Modification to this array's elements will have no effect on theEventListenerSupportinstance.- Returns:
- L[]
-
initializeTransientFields
private void initializeTransientFields(java.lang.Class<L> listenerInterface, java.lang.ClassLoader classLoader)
Initializes transient fields.- Parameters:
listenerInterface- the class of the listener interfaceclassLoader- the class loader to be used
-
readObject
private void readObject(java.io.ObjectInputStream objectInputStream) throws java.io.IOException, java.lang.ClassNotFoundExceptionDeserializes the next object into this instance.- Parameters:
objectInputStream- the input stream- Throws:
java.io.IOException- if an IO error occursjava.lang.ClassNotFoundException- if the class cannot be resolved
-
removeListener
public void removeListener(L listener)
Removes an event listener.- Parameters:
listener- the event listener (may not benull).- Throws:
java.lang.NullPointerException- iflistenerisnull.
-
writeObject
private void writeObject(java.io.ObjectOutputStream objectOutputStream) throws java.io.IOExceptionSerializes this instance onto the given ObjectOutputStream.- Parameters:
objectOutputStream- the output stream- Throws:
java.io.IOException- if an IO error occurs
-
-