org.mozilla.javascript
Class ListenerArray

java.lang.Object
  |
  +--org.mozilla.javascript.ListenerArray

public class ListenerArray
extends java.lang.Object

Utility class to manage listeners array. A possible usage would be:

private Object[] listeners;
...
void addListener(ListenerType listener) {
synchronized (this) {
listeners = ListenerArray.add(listeners, listener);
}
}

void removeListener(ListenerType listener) {
synchronized (this) {
listeners = ListenerArray.remove(listeners, listener);
}
}
Here is a thread safe while synchronization free example of event firing
void fireEvent(EventType event) {
Object[] array = listeners;
if (array != null) {
for (int i = array.length; i-- != 0;) {
((ListenerType)array[i]).onEvent(event);
}
}

}
or if listeners of different types can present in listeners array:
void fireEvent(EventType event) {
Object[] array = listeners;
if (array != null) {
for (int i = array.length; i-- != 0;) {
Object obj = array[i];
if (obj instanceof ListenerType) {
((ListenerType)obj).onEvent(event);
}
}
}

}


Constructor Summary
ListenerArray()
           
 
Method Summary
static java.lang.Object[] add(java.lang.Object[] data, java.lang.Object listener)
          Return newly allocated array that contains listener and all elements from data array.
static java.lang.Object[] remove(java.lang.Object[] data, java.lang.Object listener)
          Return a copy of data array with the first occurrence of listener removed.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListenerArray

public ListenerArray()
Method Detail

add

public static java.lang.Object[] add(java.lang.Object[] data,
                                     java.lang.Object listener)
Return newly allocated array that contains listener and all elements from data array. Note: listener is added to resulting array even if it is already present in data

remove

public static java.lang.Object[] remove(java.lang.Object[] data,
                                        java.lang.Object listener)
Return a copy of data array with the first occurrence of listener removed. If listener is not present in data, simply return data. Note: return null if listener is the single element of data.