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
Manages a list of event listeners of a given generic type. This class provides
addListener(Object) and removeListener(Object) methods for
managing listeners, as well as a fire() 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 EventListenerSupport instance will result in any non-Serializable listeners being silently dropped.
- Since:
- 3.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classInvokes listeners throughEventListenerSupport.ProxyInvocationHandler.invoke(Object, Method, Object[])in the order added to the underlyingList. -
Constructor Summary
ConstructorsConstructorDescriptionEventListenerSupport(Class<L> listenerInterface) Constructs an EventListenerSupport object which supports the provided listener interface.EventListenerSupport(Class<L> listenerInterface, 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
Modifier and TypeMethodDescriptionvoidaddListener(L listener) Adds an event listener.voidaddListener(L listener, boolean allowDuplicate) Adds an event listener.static <T> EventListenerSupport<T> Creates an EventListenerSupport object which supports the specified listener type.protected InvocationHandlerCreates theInvocationHandlerresponsible for calling to the managed listeners.fire()Returns a proxy object which can be used to call listener methods on all of the registered event listeners.L[]Gets an array containing the currently registered listeners.voidremoveListener(L listener) Removes an event listener.
-
Constructor Details
-
EventListenerSupport
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:
NullPointerException- Thrown iflistenerInterfaceisnull.IllegalArgumentException- Thrown iflistenerInterfaceis not an interface.
-
EventListenerSupport
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:
NullPointerException- Thrown iflistenerInterfaceorclassLoaderisnull.IllegalArgumentException- Thrown 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- Thrown iflistenerInterfaceisnull.IllegalArgumentException- Thrown iflistenerInterfaceis not an interface.
-
addListener
Adds an event listener.Listeners are called in the order added.
- Parameters:
listener- The event listener (may not benull).- Throws:
NullPointerException- Thrown iflistenerisnull.
-
addListener
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:
NullPointerException- Thrown iflistenerisnull.- Since:
- 3.5
-
createInvocationHandler
Creates theInvocationHandlerresponsible for calling to the managed listeners. Subclasses can override to provide custom behavior.- Returns:
- ProxyInvocationHandler
-
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
-
getListeners
Gets an array containing the currently registered listeners. Modification to this array's elements will have no effect on theEventListenerSupportinstance.- Returns:
- L[]
-
removeListener
Removes an event listener.- Parameters:
listener- The event listener (may not benull).- Throws:
NullPointerException- Thrown iflistenerisnull.
-