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:
Serializable
An EventListenerSupport object can be used to manage a list of event
listeners of a particular type. The class provides
addListener(Object) and removeListener(Object) methods
for registering listeners, as well as a fire() method for firing
events to the listeners.
To use this class, suppose you want to support ActionEvents. You would do:
public class MyActionEventSource
{
private EventListenerSupport<ActionListener> actionListeners =
EventListenerSupport.create(ActionListener.class);
public void someMethodThatFiresAction()
{
ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "somethingCool");
actionListeners.fire().actionPerformed(e);
}
}
Serializing an EventListenerSupport instance will result in any
non-Serializable listeners being silently dropped.
- Since:
- 3.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classAn invocation handler used to dispatch the event(s) to all the listeners. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateCreate a new EventListenerSupport instance.EventListenerSupport(Class<L> listenerInterface) Creates an EventListenerSupport object which supports the provided listener interface.EventListenerSupport(Class<L> listenerInterface, ClassLoader classLoader) Creates an EventListenerSupport object which supports the provided listener interface using the specified class loader to create the JDK dynamic proxy. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(L listener) Registers an event listener.voidaddListener(L listener, boolean allowDuplicate) Registers an event listener.static <T> EventListenerSupport<T> Creates an EventListenerSupport object which supports the specified listener type.protected InvocationHandlerCreate theInvocationHandlerresponsible for broadcasting calls to the managed listeners.private voidcreateProxy(Class<L> listenerInterface, ClassLoader classLoader) Create the proxy object.fire()Returns a proxy object which can be used to call listener methods on all of the registered event listeners.(package private) intReturns the number of registered listeners.L[]Gets an array containing the currently registered listeners.private voidinitializeTransientFields(Class<L> listenerInterface, ClassLoader classLoader) Initialize transient fields.private voidreadObject(ObjectInputStream objectInputStream) Deserialize.voidremoveListener(L listener) Unregisters an event listener.private voidwriteObject(ObjectOutputStream objectOutputStream) Serialize.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version- See Also:
-
listeners
The list used to 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
The proxy representing the collection of listeners. Calls to this proxy object will be sent to all registered listeners. -
prototypeArray
Empty typed array for #getListeners().
-
-
Constructor Details
-
EventListenerSupport
private EventListenerSupport()Create a new EventListenerSupport instance. Serialization-friendly constructor. -
EventListenerSupport
Creates 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:
NullPointerException- iflistenerInterfaceisnull.IllegalArgumentException- iflistenerInterfaceis not an interface.
-
EventListenerSupport
Creates 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:
NullPointerException- iflistenerInterfaceorclassLoaderisnull.IllegalArgumentException- iflistenerInterfaceis not an interface.
-
-
Method Details
-
create
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:
NullPointerException- iflistenerInterfaceisnull.IllegalArgumentException- iflistenerInterfaceis not an interface.
-
addListener
Registers an event listener.- Parameters:
listener- the event listener (may not benull).- Throws:
NullPointerException- iflistenerisnull.
-
addListener
Registers an event listener. Will not add a pre-existing listener object to the list ifallowDuplicateis false.- Parameters:
listener- the event listener (may not benull).allowDuplicate- the flag for determining if duplicate listener objects are allowed to be registered.- Throws:
NullPointerException- iflistenerisnull.- Since:
- 3.5
-
createInvocationHandler
Create theInvocationHandlerresponsible for broadcasting calls to the managed listeners. Subclasses can override to provide custom behavior.- Returns:
- ProxyInvocationHandler
-
createProxy
Create the proxy object.- Parameters:
listenerInterface- the class of the listener interfaceclassLoader- the class loader to be used
-
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()Returns the number of registered listeners.- Returns:
- the number of registered listeners.
-
getListeners
Gets an array containing the currently registered listeners. Modification to this array's elements will have no effect on theEventListenerSupportinstance.- Returns:
- L[]
-
initializeTransientFields
Initialize transient fields.- Parameters:
listenerInterface- the class of the listener interfaceclassLoader- the class loader to be used
-
readObject
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException Deserialize.- Parameters:
objectInputStream- the input stream- Throws:
IOException- if an IO error occursClassNotFoundException- if the class cannot be resolved
-
removeListener
Unregisters an event listener.- Parameters:
listener- the event listener (may not benull).- Throws:
NullPointerException- iflistenerisnull.
-
writeObject
Serialize.- Parameters:
objectOutputStream- the output stream- Throws:
IOException- if an IO error occurs
-