|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.openbp.common.listener.ListenerSupport
public class ListenerSupport
This class holds a list of event listeners.
It can be used to implement generic listener functionality.
It combines listeners of various type into a single 'listener manager' class.
Listeners can be added as regular 'hard' references (addListener(java.lang.Class, java.util.EventListener)) or as 'weak' references
(addWeakListener(java.lang.Class, java.util.EventListener)), which may be garbage-collected if not referenced otherwise.
The add-methods also ensure that the same listener will not be registered twice
(comparing the listener objects using the == operation).
Listeners that have not been removed from the object they have been registered with
are a common cause for memory leaks. By adding them as weak listeners, you may not just
add and forget them. They will be automatically garbage-collected if not referenced otherwise
and automatically (i. e. after a call to the getListenerIterator(java.lang.Class) method) removed from
the listener list.
ATTENTION: Never add an automatic class (i. e new FocusListener () { ... }) or an inner
class that is not referenced otherwise as a weak listener to the list. These objects
will be cleared by the garbage collector during the next gc run!
The listeners and the corresponding listener classes will be stored in a single array list
of type WeakArrayList the listener classes using hard links and the listeners as either
hard or weak references. The WeakArrayList class is allocated lazily. This minimizes the
footprint of the ListenerSupport class as long as no listeners have been added.
For access to the listeners, you may use the getListenerIterator(java.lang.Class) method.
It will return a static instance of EmptyIterator if no listeners of the desired type
have been registered, minimizing allocation overhead. However, if matching listeners have
been created, a small-footprint iterator class will be constructed, but this should be
tolerable.
| Nested Class Summary | |
|---|---|
class |
ListenerSupport.ListenerIterator
Convenience class that can serve as an empty Iterator. |
| Constructor Summary | |
|---|---|
ListenerSupport()
Default constructor. |
|
| Method Summary | |
|---|---|
void |
addListener(java.lang.Class listenerClass,
java.util.EventListener listener)
Adds a listener to the list (using a regular hard reference). |
void |
addWeakListener(java.lang.Class listenerClass,
java.util.EventListener listener)
Adds a listener to the list (using a weak reference). |
boolean |
containsListener(java.lang.Class listenerClass,
java.util.EventListener listener)
Checks if the list contains a particular listener or listeners of a particular type. |
boolean |
containsListeners(java.lang.Class listenerClass)
Checks if the list contains listeners of a particular type. |
int |
getListenerCount(java.lang.Class listenerClass)
Gets the number of listeners of the specified type. |
java.util.Iterator |
getListenerIterator(java.lang.Class listenerClass)
Gets an iterator over listeners of the specified type. |
void |
removeAllListeners(java.lang.Class listenerClass)
Removes all listeners from the list. |
void |
removeListener(java.lang.Class listenerClass,
java.util.EventListener listener)
Removes a listener from the list. |
void |
trim()
Removes 'dead' (i\.e\. garbage-collected) weak listeners from the list. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ListenerSupport()
| Method Detail |
|---|
public void addListener(java.lang.Class listenerClass,
java.util.EventListener listener)
listenerClass - Listener class of the listenerlistener - Listener to add
public void addWeakListener(java.lang.Class listenerClass,
java.util.EventListener listener)
listenerClass - Listener class of the listenerlistener - Listener to add
public void removeListener(java.lang.Class listenerClass,
java.util.EventListener listener)
listenerClass - Listener class of the listenerlistener - Listener to removepublic void removeAllListeners(java.lang.Class listenerClass)
listenerClass - Class of the listeners to remove.public void trim()
public boolean containsListeners(java.lang.Class listenerClass)
listenerClass - Listener class to search for
public boolean containsListener(java.lang.Class listenerClass,
java.util.EventListener listener)
listenerClass - Listener class to search forlistener - Listener instance to search forpublic int getListenerCount(java.lang.Class listenerClass)
listenerClass - Listener class
public java.util.Iterator getListenerIterator(java.lang.Class listenerClass)
listenerClass - Listener class
EmptyIterator if no listeners of the desired type
have been registered, minimizing allocation overhead. However, if matching listeners have
been created, a small-footprint iterator class will be constructed, but this should be
tolerable.addListener(java.lang.Class, java.util.EventListener), addWeakListener(java.lang.Class, java.util.EventListener) or trim()
while iterating the listener list.
The returned iterator will automatically perform a trim() operation after the Iterator.hasNext
method returns false (i. e. the end of the list has been reached) and 'dead' listener
references have been detected in order to keep the listener list clean.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||