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`

public class EventListenerSupport<L> extends Object implements 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:
  - Serialized Form

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected class`
  `EventListenerSupport.ProxyInvocationHandler`
  Invokes listeners through `EventListenerSupport.ProxyInvocationHandler.invoke(Object, Method, Object[])` in the order added to the underlying `List`.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `EventListenerSupport(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 Type
  Method
  Description
  `void`
  `addListener(L listener)`
  Adds an event listener.
  `void`
  `addListener(L listener, boolean allowDuplicate)`
  Adds an event listener.
  `static <T> EventListenerSupport<T>`
  `create(Class<T> listenerInterface)`
  Creates an EventListenerSupport object which supports the specified listener type.
  `protected InvocationHandler`
  `createInvocationHandler()`
  Creates the `InvocationHandler` responsible for calling to the managed listeners.
  `L`
  `fire()`
  Returns a proxy object which can be used to call listener methods on all of the registered event listeners.
  `L[]`
  `getListeners()`
  Gets an array containing the currently registered listeners.
  `void`
  `removeListener(L listener)`
  Removes an event listener.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### EventListenerSupport

    public EventListenerSupport(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:
      `NullPointerException` - if `listenerInterface` is `null`.
      `IllegalArgumentException` - if `listenerInterface` is not an interface.

  - ### EventListenerSupport

    public 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.
    Parameters:
      `listenerInterface` - the listener interface.
      `classLoader` - the class loader.
    Throws:
      `NullPointerException` - if `listenerInterface` or `classLoader` is `null`.
      `IllegalArgumentException` - if `listenerInterface` is not an interface.

- ## Method Details

  - ### create

    public static <T> EventListenerSupport<T> create(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:
      `NullPointerException` - if `listenerInterface` is `null`.
      `IllegalArgumentException` - if `listenerInterface` is 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 be `null`).
    Throws:
      `NullPointerException` - if `listener` is `null`.

  - ### addListener

    public void addListener(L listener, boolean allowDuplicate)
    Adds an event listener. Will not add a pre-existing listener object to the list if `allowDuplicate` is false.

    Listeners are called in the order added.

    Parameters:
      `listener` - the event listener (may not be `null`).
      `allowDuplicate` - the flag for determining if duplicate listener objects are allowed to be registered.
    Throws:
      `NullPointerException` - if `listener` is `null`.
    Since:
      3.5

  - ### createInvocationHandler

    protected InvocationHandler createInvocationHandler()
    Creates the `InvocationHandler` responsible for calling to the managed listeners. Subclasses can override to provide custom behavior.
    Returns:
      ProxyInvocationHandler

  - ### 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

  - ### getListeners

    public L[] getListeners()
    Gets an array containing the currently registered listeners. Modification to this array's elements will have no effect on the `EventListenerSupport` instance.
    Returns:
      L[]

  - ### removeListener

    public void removeListener(L listener)
    Removes an event listener.
    Parameters:
      `listener` - the event listener (may not be `null`).
    Throws:
      `NullPointerException` - if `listener` is `null`.