Class EventListenerList


  • public class EventListenerList
    extends java.lang.Object

    A class for managing event listeners for an event source.

    This class allows registering an arbitrary number of event listeners for specific event types. Event types are specified using the EventType class. Due to the type parameters in method signatures, it is guaranteed that registered listeners are compatible with the event types they are interested in.

    There are also methods for firing events. Here all registered listeners are determined - based on the event type specified at registration time - which should receive the event to be fired. So basically, the event type at listener registration serves as a filter criterion. Because of the hierarchical nature of event types it can be determined in a fine-grained way which events are propagated to which listeners. It is also possible to register a listener multiple times for different event types.

    Implementation note: This class is thread-safe.

    Since:
    2.0
    • Constructor Detail

      • EventListenerList

        public EventListenerList()
        Creates a new instance of EventListenerList.
    • Method Detail

      • addEventListener

        public <T extends Event> void addEventListener​(EventType<T> type,
                                                       EventListener<? super T> listener)
        Adds an event listener for the specified event type. This listener is notified about events of this type and all its sub types.
        Type Parameters:
        T - the type of events processed by this listener
        Parameters:
        type - the event type (must not be null)
        listener - the listener to be registered (must not be null)
        Throws:
        java.lang.IllegalArgumentException - if a required parameter is null
      • addEventListener

        public <T extends Event> void addEventListener​(EventListenerRegistrationData<T> regData)
        Adds the specified listener registration data object to the internal list of event listeners. This is an alternative registration method; the event type and the listener are passed as a single data object.
        Type Parameters:
        T - the type of events processed by this listener
        Parameters:
        regData - the registration data object (must not be null)
        Throws:
        java.lang.IllegalArgumentException - if the registration data object is null
      • removeEventListener

        public <T extends Event> boolean removeEventListener​(EventType<T> eventType,
                                                             EventListener<? super T> listener)
        Removes the event listener registration for the given event type and listener. An event listener instance may be registered multiple times for different event types. Therefore, when removing a listener the event type of the registration in question has to be specified. The return value indicates whether a registration was removed. A value of false means that no such combination of event type and listener was found.
        Type Parameters:
        T - the type of events processed by this listener
        Parameters:
        eventType - the event type
        listener - the event listener to be removed
        Returns:
        a flag whether a listener registration was removed
      • removeEventListener

        public <T extends Event> boolean removeEventListener​(EventListenerRegistrationData<T> regData)
        Removes the event listener registration defined by the passed in data object. This is an alternative method for removing a listener which expects the event type and the listener in a single data object.
        Type Parameters:
        T - the type of events processed by this listener
        Parameters:
        regData - the registration data object
        Returns:
        a flag whether a listener registration was removed
        See Also:
        removeEventListener(EventType, EventListener)
      • fire

        public void fire​(Event event)
        Fires an event to all registered listeners matching the event type.
        Parameters:
        event - the event to be fired (must not be null)
        Throws:
        java.lang.IllegalArgumentException - if the event is null
      • getEventListeners

        public <T extends Event> java.lang.Iterable<EventListener<? super T>> getEventListeners​(EventType<T> eventType)
        Returns an Iterable allowing access to all event listeners stored in this list which are compatible with the specified event type.
        Type Parameters:
        T - the event type
        Parameters:
        eventType - the event type object
        Returns:
        an Iterable with the selected event listeners
      • getEventListenerIterator

        public <T extends EventEventListenerList.EventListenerIterator<T> getEventListenerIterator​(EventType<T> eventType)
        Returns a specialized iterator for obtaining all event listeners stored in this list which are compatible with the specified event type.
        Type Parameters:
        T - the event type
        Parameters:
        eventType - the event type object
        Returns:
        an Iterator with the selected event listeners
      • getRegistrations

        public java.util.List<EventListenerRegistrationData<?>> getRegistrations()
        Returns an (unmodifiable) list with registration information about all event listeners registered at this object.
        Returns:
        a list with event listener registration information
      • getRegistrationsForSuperType

        public <T extends Event> java.util.List<EventListenerRegistrationData<? extends T>> getRegistrationsForSuperType​(EventType<T> eventType)
        Returns a list with EventListenerRegistrationData objects for all event listener registrations of the specified event type or an event type having this type as super type (directly or indirectly). Note that this is the opposite direction than querying event types for firing events: in this case event listener registrations are searched which are super event types from a given type. This method in contrast returns event listener registrations for listeners that extend a given super type.
        Type Parameters:
        T - the event type
        Parameters:
        eventType - the event type object
        Returns:
        a list with the matching event listener registration objects
      • clear

        public void clear()
        Removes all event listeners registered at this object.
      • addAll

        public void addAll​(EventListenerList c)
        Adds all event listener registrations stored in the specified EventListenerList to this list.
        Parameters:
        c - the list to be copied (must not be null)
        Throws:
        java.lang.IllegalArgumentException - if the list to be copied is null