Class ListenerList<E>
- Type Parameters:
E
- the type of listeners in this list
- All Implemented Interfaces:
Iterable<E>
A listener list handles the same listener being added multiple times, and tolerates removal of listeners that are the same as other listeners in the list. For this purpose, listeners can be compared with each other using either equality or identity, as specified in the list constructor.
Use an enhanced 'for' loop to notify listeners. The recommended code sequence
for notifying all registered listeners of say,
FooListener#eventHappened(Event)
, is:
ListenerList<FooListener> fooListeners = new ListenerList<>(); //... for (FooListener listener : fooListeners) { listener.eventHappened(event); }
Legacy code may still call getListeners()
and then use a 'for' loop
to iterate the Object[]
. This might be insignificantly faster, but it
lacks type-safety and risks inadvertent modifications to the array.
This class can be used without OSGi running.
- Since:
- org.eclipse.equinox.common 3.2
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a listener list in which listeners are compared using equality.ListenerList
(int mode) Creates a listener list using the provided comparison mode. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a listener to this list.void
clear()
Removes all listeners from this list.Object[]
Returns an array containing all the registered listeners.boolean
isEmpty()
Returns whether this listener list is empty.iterator()
Returns an iterator over all the registered listeners.Returns a parallelStream
over the registered listeners.void
Removes a listener from this list.int
size()
Returns the number of registered listeners.Returns a Spliterator covering the registered listeners.stream()
Returns a sequentialStream
over the registered listeners.toString()
-
Field Details
-
EQUALITY
public static final int EQUALITYMode constant (value 0) indicating that listeners should be considered the same if they are equal.- See Also:
-
IDENTITY
public static final int IDENTITYMode constant (value 1) indicating that listeners should be considered the same if they are identical.- See Also:
-
-
Constructor Details
-
ListenerList
public ListenerList()Creates a listener list in which listeners are compared using equality. -
ListenerList
public ListenerList(int mode) Creates a listener list using the provided comparison mode.- Parameters:
mode
- The mode used to determine if listeners are the same.
-
-
Method Details
-
add
Adds a listener to this list. This method has no effect if the same listener is already registered.- Parameters:
listener
- the non-null
listener to add
-
getListeners
Returns an array containing all the registered listeners. The resulting array is unaffected by subsequent adds or removes. If there are no listeners registered, the result is an empty array. Use this method when notifying listeners, so that any modifications to the listener list during the notification will have no effect on the notification itself.Note: Callers of this method must not modify the returned array.
Note: The recommended and type-safe way to iterate this list is to use an enhanced 'for' statement, see
ListenerList
. This method is deprecated for new code.- Returns:
- the list of registered listeners
-
iterator
Returns an iterator over all the registered listeners. The resulting iterator is unaffected by subsequent adds or removes. Use this method when notifying listeners, so that any modifications to the listener list during the notification will have no effect on the notification itself. -
isEmpty
public boolean isEmpty()Returns whether this listener list is empty.- Returns:
true
if there are no registered listeners, andfalse
otherwise
-
remove
Removes a listener from this list. Has no effect if the same listener was not already registered.- Parameters:
listener
- the non-null
listener to remove
-
size
public int size()Returns the number of registered listeners.- Returns:
- the number of registered listeners
-
clear
public void clear()Removes all listeners from this list. -
spliterator
Returns a Spliterator covering the registered listeners.The spliterator reports Spliterator.SIZED, Spliterator.SUBSIZED, Spliterator.ORDERED, and Spliterator.IMMUTABLE
- Specified by:
spliterator
in interfaceIterable<E>
- Returns:
- a spliterator for listeners
- Since:
- org.eclipse.equinox.common 3.9
-
stream
Returns a sequentialStream
over the registered listeners.- Returns:
- a sequential
Stream
over the registered listeners. - Since:
- org.eclipse.equinox.common 3.9
-
parallelStream
Returns a parallelStream
over the registered listeners.- Returns:
- a parallel
Stream
over the registered listeners. - Since:
- org.eclipse.equinox.common 3.9
-
toString
-