Importing the XDebugProxy code in the HEAD. The repo was tagged with T_BEFORE_XDEBUGP...
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / core / ListenerList.java
1 package net.sourceforge.phpeclipse.xdebug.core;
2
3
4 /**
5  * Local version of org.eclipse.jface.util.ListenerList (modified)s
6  */
7 public class ListenerList {
8         /**
9          * The current number of listeners.
10          * Maintains invariant: 0 <= fSize <= listeners.length.
11          */
12         private int fSize;
13
14         /**
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
18          */
19         private Object[] fListeners= null;
20
21         /**
22          * The empty array singleton instance, returned by getListeners()
23          * when size == 0.
24          */
25         private static final Object[] EmptyArray= new Object[0];
26
27         /**
28          * Creates a listener list with the given initial capacity.
29          *
30          * @param capacity the number of listeners which this list can initially accept 
31          *    without growing its internal representation; must be at least 1
32          */
33         public ListenerList(int capacity) {
34                 if (capacity < 1) {
35                         throw new IllegalArgumentException();
36                 }
37                 fListeners= new Object[capacity];
38                 fSize= 0;
39         }
40
41         /**
42          * Adds a listener to the list.
43          * Has no effect if an identical listener is already registered.
44          *
45          * @param listener a listener
46          */
47         public synchronized void add(Object listener) {
48                 if (listener == null) {
49                         throw new IllegalArgumentException();
50                 }
51                 // check for duplicates using identity
52                 for (int i= 0; i < fSize; ++i) {
53                         if (fListeners[i] == listener) {
54                                 return;
55                         }
56                 }
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);
61                         fListeners= temp;
62                 }
63                 fListeners[fSize++]= listener;
64         }
65
66         /**
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.
74          */
75         public synchronized Object[] getListeners() {
76                 if (fSize == 0) {
77                         return EmptyArray;
78                 }
79                 Object[] result= new Object[fSize];
80                 System.arraycopy(fListeners, 0, result, 0, fSize);
81                 return result;
82         }
83
84         /**
85          * Removes a listener from the list.
86          * Has no effect if an identical listener was not already registered.
87          *
88          * @param listener a listener
89          */
90         public synchronized void remove(Object listener) {
91                 if (listener == null) {
92                         throw new IllegalArgumentException();
93                 }
94
95                 for (int i= 0; i < fSize; ++i) {
96                         if (fListeners[i] == listener) {
97                                 if (--fSize == 0) {
98                                         fListeners= new Object[1];
99                                 } else {
100                                         if (i < fSize) {
101                                                 fListeners[i]= fListeners[fSize];
102                                         }
103                                         fListeners[fSize]= null;
104                                 }
105                                 return;
106                         }
107                 }
108         }
109
110         /**
111          * Removes all the listeners from the list.
112          */
113         public synchronized void removeAll() {
114                 fListeners= new Object[0];
115                 fSize= 0;
116         }
117
118         /**
119          * Returns the number of registered listeners
120          *
121          * @return the number of registered listeners
122          */
123         public int size() {
124                 return fSize;
125         }
126 }
127
128