com.miginfocom.util
Class ListenerSet

java.lang.Object
  extended by com.miginfocom.util.ListenerSet

public class ListenerSet
extends java.lang.Object

An listener set that supports both WeakReferences and normal references. Doesn't create the actual list until it's needed.

Good for having listeners weakly referenced, so they do not hider garbage collection. If the listener is not wrapped in a weak listener it has do be explicitly cleared when the object goes out of scope or there may be a memory leak.
Using this class to store listeners make sure that there will be no memory leak.


Field Summary
protected static com.miginfocom.util.ListenerSet.EmptyIterator EMPTY_ITERATOR
           
 
Constructor Summary
ListenerSet()
          Constructor for no runtime check of the listener classes.
ListenerSet(java.lang.Class listenerClass)
          Constructor for a runtime checked listener classes.
 
Method Summary
 void add(java.util.EventListener listener, boolean asWeakRef)
          Adds the listener, if it isn't already in the list
protected  void assertListenerType(java.lang.Object l)
          Throws a IllegalArgumentException if listener class isn't a subclass of the specified listener class and a NullPointerException if l is null.
 int findListener(java.util.EventListener listener)
          Returns the listener index.
 boolean fireEvent(java.util.EventObject e)
          Handles the fire marshalling based on the type of e.
 boolean fireVetoableEvent(java.util.EventObject e)
          Handles the fire marshalling based on the type of e.
 java.util.EventListener getListener(int ix)
          Returns the eventlistener for the index.
 java.util.List getListeners()
          Returns the listeners as a new list.
 boolean isIgnoreEvents()
          Returns if events are currently ignored.
 boolean isWeakReference(int ix)
          Returns if listener at index ix is stored in a weak reference or normal reference.
 java.util.Iterator iterator()
          Returns an iterator that iterates of the contained listeners.
 void remove(java.util.EventListener listener)
          Removes the listener, if it is in the list.
 void removeAll()
          Removes all listeners.
 boolean setIgnoreEvents(boolean b)
          Sets if events should be ignored, and thus not fired.
 int size()
          Returns the size of the listener list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ITERATOR

protected static final com.miginfocom.util.ListenerSet.EmptyIterator EMPTY_ITERATOR
Constructor Detail

ListenerSet

public ListenerSet()
Constructor for no runtime check of the listener classes.


ListenerSet

public ListenerSet(java.lang.Class listenerClass)
Constructor for a runtime checked listener classes.

Parameters:
listenerClass - The class to always check for.
Method Detail

add

public void add(java.util.EventListener listener,
                boolean asWeakRef)
Adds the listener, if it isn't already in the list

Parameters:
listener - The listener. Not null. If the listener implements Comparable the list will be sorted so that when later notifications will be conducted the liste will be notified in the sorted order. For instance MouseInteractionListener is Comparable.
asWeakRef - If the listener should be added wrapped in a WeakReference. This defers memory leak problems since the garbage collector can collect the listener if it is only referenced from this list.
Note! This (weak reference) can not be used with listeners that doesn't have another real (a.k.a Strong) reference to it, as for instance an annonymous inner class. If one such listener is added it will be removed almost immediately by the garbage collector.

remove

public void remove(java.util.EventListener listener)
Removes the listener, if it is in the list.

Parameters:
listener - The listener to remove, not null.

removeAll

public void removeAll()
Removes all listeners.


size

public int size()
Returns the size of the listener list.

Returns:
The size of the listener list.

setIgnoreEvents

public boolean setIgnoreEvents(boolean b)
Sets if events should be ignored, and thus not fired.

Parameters:
b - true turns off events
Returns:
The old state of this flag.

isIgnoreEvents

public boolean isIgnoreEvents()
Returns if events are currently ignored.

Returns:
If events are currently ignored.
See Also:
setIgnoreEvents(boolean)

isWeakReference

public boolean isWeakReference(int ix)
Returns if listener at index ix is stored in a weak reference or normal reference.

Parameters:
ix - The index to check. Must be < size.
Returns:
Returns Boolean.TRUE if a weak reference, Boolean.FALSE if normal. null if not in the list.
Throws:
java.lang.IndexOutOfBoundsException

findListener

public int findListener(java.util.EventListener listener)
Returns the listener index.

Parameters:
listener - The listener to look for
Returns:
The index of the listener or -1 if not present.

getListener

public java.util.EventListener getListener(int ix)
Returns the eventlistener for the index.

Parameters:
ix - The index into the list. Myst be < size.
Returns:
The listener. Never null.
Throws:
java.lang.IndexOutOfBoundsException

iterator

public java.util.Iterator iterator()
Returns an iterator that iterates of the contained listeners. The list it iterates over is newly created and contains only strong references. This ensures that the listeners can modify the ListenerSet in the notification method without it affecting the original list.

If ignore events is true the list will always be empty.

Returns:
An iterator that iterates over the registered listeners. Never null.
See Also:
setIgnoreEvents(boolean)

getListeners

public java.util.List getListeners()
Returns the listeners as a new list.

Returns:
The listeners as a new list. Never null.

fireEvent

public boolean fireEvent(java.util.EventObject e)
Handles the fire marshalling based on the type of e. Listener class type will be checked for match at runtime.
If ignore events is true no listeners will be notified.

Parameters:
e - The event to fire.
Returns:
If there was at least one reciever of the event.
See Also:
setIgnoreEvents(boolean)

fireVetoableEvent

public boolean fireVetoableEvent(java.util.EventObject e)
                          throws java.beans.PropertyVetoException
Handles the fire marshalling based on the type of e. Listener class type will be checked for match at runtime.
If ignore events is true no listeners will be notified.

Parameters:
e - The event to fire.
Returns:
If there was at least one reciever of the event.
Throws:
java.beans.PropertyVetoException
See Also:
setIgnoreEvents(boolean)

assertListenerType

protected void assertListenerType(java.lang.Object l)
Throws a IllegalArgumentException if listener class isn't a subclass of the specified listener class and a NullPointerException if l is null.

Parameters:
l - The listener to test.


Copyright © 2009 MiG InfoCom AB. All Rights Reserved.