Package com.day.util
Class ListenerList
java.lang.Object
com.day.util.ListenerList
The
ListenerList class provides a utility to maintain lists of
registered listeners. It is fairly lightweight and should not impose to
much memory overhead.
Use the getListeners() method when notifying listeners. Note that
no garbage is created if no listeners are registered. The recommended code
sequence for notifying all registered listeners of say,
FooListener.eventHappened, is:
Object[] listeners = myListenerList.getListeners();
for (int i = 0; i < listeners.length; ++i) {
((FooListener) listeners[i]).eventHappened(event);
}
- Since:
- iguana
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddListener(Object listener) Adds a listener to the list of listeners if it is not yet registered in the list.voidclear()Clears the list of registered listeners.Object[]Returns the list of registered listeners.Object[]getListeners(Class requestedType) Returns the list of registered listeners in an array of the reqeusted runtime type.booleanremoveListener(Object listener) Removes a listener from the list of listeners if it is contained.intsize()Returns the number of currently registered listeners.
-
Constructor Details
-
ListenerList
public ListenerList()Creates an instance of this listener list class.
-
-
Method Details
-
addListener
Adds a listener to the list of listeners if it is not yet registered in the list.- Parameters:
listener- The listener to add to the list. Ifnullnothing is done.- Returns:
trueif the listener has been added to the list
-
removeListener
Removes a listener from the list of listeners if it is contained.- Parameters:
listener- The listener to remove from the list. Ifnullnothing is done.- Returns:
trueif the listener has been added to the list
-
clear
public void clear()Clears the list of registered listeners. -
size
public int size()Returns the number of currently registered listeners.- Returns:
- the size
-
getListeners
Returns the list of registered listeners. Note that this method returns the same array for two subsequent calls if no listeners have been added or removed in the mean time. This also means, that callers of this method MUST NOT modify the array returned.- Returns:
- the listeners
-
getListeners
Returns the list of registered listeners in an array of the reqeusted runtime type. Note that this method returns the same array for two subsequent calls if no listeners have been added or removed in the mean time and if the element type of is assignment compatible. This also means, that callers of this method MUST NOT modify the array returned.- Parameters:
requestedType- The runtime type of the components of the array to return.- Returns:
- the listeners
- Throws:
ArrayStoreException- if the runtime type is not a supertype of the runtime type of every element in this list.
-