ice.util.alg
Class Listeners

java.lang.Object
  extended byice.util.alg.Listeners

public class Listeners
extends Object

Set of utilities to support adding/removing of listeners in memory efficient way. Possible usage:

        private volatile Object _myListeners;

        public void addMyListener(TextListener l) {
                synchronized (this) {
                        _myListeners = Listeners.add(_myListeners, l);
                }
        }

        public void removeTextListener(TextListener l) {
                synchronized (this) {
                        _myListeners = Listeners.remove(_myListeners, l);
                }
        }

        public void fireMyEvent() {
        // Get immune local copy
                Object listeners = _myListeners;
                if (listeners != null) {
                        MyEvent e = new MyEvent(this);
                        for (int i = 0; ; ++i) {
                                MyListener l = (MyListener)Listeners.get(listeners, i);
                                if (l == null) { break; }
                                l.onMyEvent(e);
                        }
                }
        }


Constructor Summary
Listeners()
           
 
Method Summary
static Object add(Object listenerSet, Object listener)
          Add listener to listenerSet listener collection and return the resulting collection.
static Object get(Object listenerSet, int index)
          Get listener at index in listenerSet or null if index equals to listenerSet length.
static Object remove(Object listenerSet, Object listener)
          Removelistener from listenerSet listener collection and return the resulting collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Listeners

public Listeners()
Method Detail

add

public static Object add(Object listenerSet,
                         Object listener)
Add listener to listenerSet listener collection and return the resulting collection. The function does not modify listenerSet and if listener is presents there, it simply returns listenerSet.

Parameters:
listener - Listener to add to listenerSet
listenerSet - Current collection of listeners. Empty collection always represented as the null value
Returns:
A listener collection that contains listener and all listeners from listenerSet
See Also:
remove(Object listenerSet, Object listener), get(Object listenerSet, int index)

remove

public static Object remove(Object listenerSet,
                            Object listener)
Removelistener from listenerSet listener collection and return the resulting collection. The function does not modify listenerSet and if listener is not presents there, it simply returns listenerSet.

Parameters:
listener - Listener to remove from listenerSet
listenerSet - Current collection of listeners.
Returns:
A listener collection with listener removed.
See Also:
add(Object listenerSet, Object listener), get(Object listenerSet, int index)

get

public static Object get(Object listenerSet,
                         int index)
Get listener at index in listenerSet or null if index equals to listenerSet length.

Parameters:
listenerSet - Collection of listeners.
Throws:
RuntimeException - if listenerSet is null
RuntimeException - if index ≥ length of listenerSet
See Also:
add(Object listenerSet, Object listener), remove(Object listenerSet, Object listener)