Class EventListenerList
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
A specialIterator
implementation used by thegetEventListenerIterator()
method. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds all event listener registrations stored in the specifiedEventListenerList
to this list.<T extends Event>
voidaddEventListener
(EventListenerRegistrationData<T> regData) Adds the specified listener registration data object to the internal list of event listeners.<T extends Event>
voidaddEventListener
(EventType<T> type, EventListener<? super T> listener) Adds an event listener for the specified event type.void
clear()
Removes all event listeners registered at this object.void
Fires an event to all registered listeners matching the event type.<T extends Event>
EventListenerList.EventListenerIterator<T>getEventListenerIterator
(EventType<T> eventType) Gets a specialized iterator for obtaining all event listeners stored in this list which are compatible with the specified event type.<T extends Event>
Iterable<EventListener<? super T>>getEventListeners
(EventType<T> eventType) Gets anIterable
allowing access to all event listeners stored in this list which are compatible with the specified event type.Gets an (unmodifiable) list with registration information about all event listeners registered at this object.<T extends Event>
List<EventListenerRegistrationData<? extends T>>getRegistrationsForSuperType
(EventType<T> eventType) Gets a list withEventListenerRegistrationData
objects for all event listener registrations of the specified event type or an event type having this type as super type (directly or indirectly).<T extends Event>
booleanremoveEventListener
(EventListenerRegistrationData<T> regData) Removes the event listener registration defined by the passed in data object.<T extends Event>
booleanremoveEventListener
(EventType<T> eventType, EventListener<? super T> listener) Removes the event listener registration for the given event type and listener.
-
Constructor Details
-
EventListenerList
public EventListenerList()Creates a new instance ofEventListenerList
.
-
-
Method Details
-
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:
IllegalArgumentException
- if a required parameter is null
-
addEventListener
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:
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 typelistener
- the event listener to be removed- Returns:
- a flag whether a listener registration was removed
-
removeEventListener
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:
-
fire
Fires an event to all registered listeners matching the event type.- Parameters:
event
- the event to be fired (must not be null)- Throws:
IllegalArgumentException
- if the event is null
-
getEventListeners
public <T extends Event> Iterable<EventListener<? super T>> getEventListeners(EventType<T> eventType) Gets anIterable
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 Event> EventListenerList.EventListenerIterator<T> getEventListenerIterator(EventType<T> eventType) Gets 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
Gets 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> List<EventListenerRegistrationData<? extends T>> getRegistrationsForSuperType(EventType<T> eventType) Gets a list withEventListenerRegistrationData
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
Removes all event listeners registered at this object. -
addAll
Adds all event listener registrations stored in the specifiedEventListenerList
to this list.- Parameters:
c
- the list to be copied (must not be null)- Throws:
IllegalArgumentException
- if the list to be copied is null
-