E
- the type of listeners in this listpublic class ListenerList<E> extends Object implements 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.
Modifier and Type | Field and Description |
---|---|
static int |
EQUALITY
Mode constant (value 0) indicating that listeners should be considered
the same if they are equal.
|
static int |
IDENTITY
Mode constant (value 1) indicating that listeners should be considered
the same if they are identical.
|
Constructor and Description |
---|
ListenerList()
Creates a listener list in which listeners are compared using equality.
|
ListenerList(int mode)
Creates a listener list using the provided comparison mode.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E listener)
Adds a listener to this list.
|
void |
clear()
Removes all listeners from this list.
|
Object[] |
getListeners()
Returns an array containing all the registered listeners.
|
boolean |
isEmpty()
Returns whether this listener list is empty.
|
Iterator<E> |
iterator()
Returns an iterator over all the registered listeners.
|
void |
remove(Object listener)
Removes a listener from this list.
|
int |
size()
Returns the number of registered listeners.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public static final int EQUALITY
public static final int IDENTITY
public ListenerList()
public ListenerList(int mode)
mode
- The mode used to determine if listeners are the same.public void add(E listener)
listener
- the non-null
listener to addpublic Object[] getListeners()
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.
public Iterator<E> iterator()
public boolean isEmpty()
true
if there are no registered listeners, and
false
otherwisepublic void remove(Object listener)
listener
- the non-null
listener to removepublic int size()
public void clear()
Copyright (c) 2000, 2016 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.