1 package net.sourceforge.phpeclipse.xdebug.core;
5 * Local version of org.eclipse.jface.util.ListenerList (modified)s
7 public class ListenerList {
9 * The current number of listeners.
10 * Maintains invariant: 0 <= fSize <= listeners.length.
15 * The list of listeners. Initially <code>null</code> but initialized
16 * to an array of size capacity the first time a listener is added.
17 * Maintains invariant: listeners != null if and only if fSize != 0
19 private Object[] fListeners= null;
22 * The empty array singleton instance, returned by getListeners()
25 private static final Object[] EmptyArray= new Object[0];
28 * Creates a listener list with the given initial capacity.
30 * @param capacity the number of listeners which this list can initially accept
31 * without growing its internal representation; must be at least 1
33 public ListenerList(int capacity) {
35 throw new IllegalArgumentException();
37 fListeners= new Object[capacity];
42 * Adds a listener to the list.
43 * Has no effect if an identical listener is already registered.
45 * @param listener a listener
47 public synchronized void add(Object listener) {
48 if (listener == null) {
49 throw new IllegalArgumentException();
51 // check for duplicates using identity
52 for (int i= 0; i < fSize; ++i) {
53 if (fListeners[i] == listener) {
57 // grow array if necessary
58 if (fSize == fListeners.length) {
59 Object[] temp= new Object[(fSize * 2) + 1];
60 System.arraycopy(fListeners, 0, temp, 0, fSize);
63 fListeners[fSize++]= listener;
67 * Returns an array containing all the registered listeners.
68 * The resulting array is unaffected by subsequent adds or removes.
69 * If there are no listeners registered, the result is an empty array
70 * singleton instance (no garbage is created).
71 * Use this method when notifying listeners, so that any modifications
72 * to the listener list during the notification will have no effect on the
73 * notification itself.
75 public synchronized Object[] getListeners() {
79 Object[] result= new Object[fSize];
80 System.arraycopy(fListeners, 0, result, 0, fSize);
85 * Removes a listener from the list.
86 * Has no effect if an identical listener was not already registered.
88 * @param listener a listener
90 public synchronized void remove(Object listener) {
91 if (listener == null) {
92 throw new IllegalArgumentException();
95 for (int i= 0; i < fSize; ++i) {
96 if (fListeners[i] == listener) {
98 fListeners= new Object[1];
101 fListeners[i]= fListeners[fSize];
103 fListeners[fSize]= null;
111 * Removes all the listeners from the list.
113 public synchronized void removeAll() {
114 fListeners= new Object[0];
119 * Returns the number of registered listeners
121 * @return the number of registered listeners