Package com.day.util
Class ListenerList
- java.lang.Object
-
- com.day.util.ListenerList
-
public class ListenerList extends java.lang.Object
TheListenerList
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 Constructor Description ListenerList()
Creates an instance of this listener list class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addListener(java.lang.Object listener)
Adds a listener to the list of listeners if it is not yet registered in the list.void
clear()
Clears the list of registered listeners.java.lang.Object[]
getListeners()
Returns the list of registered listeners.java.lang.Object[]
getListeners(java.lang.Class requestedType)
Returns the list of registered listeners in an array of the reqeusted runtime type.boolean
removeListener(java.lang.Object listener)
Removes a listener from the list of listeners if it is contained.int
size()
Returns the number of currently registered listeners.
-
-
-
Method Detail
-
addListener
public boolean addListener(java.lang.Object listener)
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. Ifnull
nothing is done.- Returns:
true
if the listener has been added to the list
-
removeListener
public boolean removeListener(java.lang.Object listener)
Removes a listener from the list of listeners if it is contained.- Parameters:
listener
- The listener to remove from the list. Ifnull
nothing is done.- Returns:
true
if 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
public java.lang.Object[] 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
public java.lang.Object[] getListeners(java.lang.Class requestedType)
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:
java.lang.ArrayStoreException
- if the runtime type is not a supertype of the runtime type of every element in this list.
-
-