124 * @return array of ListenerType-listener pairs
125 */
126 public Object[] getListenerList() {
127 return listenerList;
128 }
129
130 /**
131 * Return an array of all the listeners of the given type.
132 *
133 * @param <T> the type of {@code EventListener} to search for
134 * @param t the type of {@code EventListener} classes to be returned
135 * @return all of the listeners of the specified type.
136 * @exception ClassCastException if the supplied class
137 * is not assignable to EventListener
138 *
139 * @since 1.3
140 */
141 public <T extends EventListener> T[] getListeners(Class<T> t) {
142 Object[] lList = listenerList;
143 int n = getListenerCount(lList, t);
144 T[] result = (T[])Array.newInstance(t, n);
145 int j = 0;
146 for (int i = lList.length-2; i>=0; i-=2) {
147 if (lList[i] == t) {
148 result[j++] = (T)lList[i+1];
149 }
150 }
151 return result;
152 }
153
154 /**
155 * Returns the total number of listeners for this listener list.
156 *
157 * @return an integer count of total number of listeners
158 */
159 public int getListenerCount() {
160 return listenerList.length/2;
161 }
162
163 /**
164 * Returns the total number of listeners of the supplied type
165 * for this listener list.
166 *
167 * @param t the type of listeners to count
168 * @return the number of listeners of type {@code t}
169 */
170 public int getListenerCount(Class<?> t) {
171 Object[] lList = listenerList;
172 return getListenerCount(lList, t);
173 }
174
175 private int getListenerCount(Object[] list, Class t) {
176 int count = 0;
177 for (int i = 0; i < list.length; i+=2) {
178 if (t == (Class)list[i])
179 count++;
180 }
181 return count;
182 }
183
184 /**
185 * Adds the listener as a listener of the specified type.
186 *
187 * @param <T> the type of {@code EventListener} to add
188 * @param t the type of the {@code EventListener} class to add
189 * @param l the listener to be added
190 */
191 public synchronized <T extends EventListener> void add(Class<T> t, T l) {
192 if (l==null) {
193 // In an ideal world, we would do an assertion here
194 // to help developers know they are probably doing
195 // something wrong
271 if ((l!=null) && (l instanceof Serializable)) {
272 s.writeObject(t.getName());
273 s.writeObject(l);
274 }
275 }
276
277 s.writeObject(null);
278 }
279
280 private void readObject(ObjectInputStream s)
281 throws IOException, ClassNotFoundException {
282 listenerList = NULL_ARRAY;
283 s.defaultReadObject();
284 Object listenerTypeOrNull;
285
286 while (null != (listenerTypeOrNull = s.readObject())) {
287 ClassLoader cl = Thread.currentThread().getContextClassLoader();
288 EventListener l = (EventListener)s.readObject();
289 String name = (String) listenerTypeOrNull;
290 ReflectUtil.checkPackageAccess(name);
291 add((Class<EventListener>)Class.forName(name, true, cl), l);
292 }
293 }
294
295 /**
296 * Returns a string representation of the EventListenerList.
297 */
298 public String toString() {
299 Object[] lList = listenerList;
300 String s = "EventListenerList: ";
301 s += lList.length/2 + " listeners: ";
302 for (int i = 0 ; i <= lList.length-2 ; i+=2) {
303 s += " type " + ((Class)lList[i]).getName();
304 s += " listener " + lList[i+1];
305 }
306 return s;
307 }
308 }
|
124 * @return array of ListenerType-listener pairs
125 */
126 public Object[] getListenerList() {
127 return listenerList;
128 }
129
130 /**
131 * Return an array of all the listeners of the given type.
132 *
133 * @param <T> the type of {@code EventListener} to search for
134 * @param t the type of {@code EventListener} classes to be returned
135 * @return all of the listeners of the specified type.
136 * @exception ClassCastException if the supplied class
137 * is not assignable to EventListener
138 *
139 * @since 1.3
140 */
141 public <T extends EventListener> T[] getListeners(Class<T> t) {
142 Object[] lList = listenerList;
143 int n = getListenerCount(lList, t);
144 @SuppressWarnings("unchecked")
145 T[] result = (T[])Array.newInstance(t, n);
146 int j = 0;
147 for (int i = lList.length-2; i>=0; i-=2) {
148 if (lList[i] == t) {
149 @SuppressWarnings("unchecked")
150 T tmp = (T)lList[i+1];
151 result[j++] = tmp;
152 }
153 }
154 return result;
155 }
156
157 /**
158 * Returns the total number of listeners for this listener list.
159 *
160 * @return an integer count of total number of listeners
161 */
162 public int getListenerCount() {
163 return listenerList.length/2;
164 }
165
166 /**
167 * Returns the total number of listeners of the supplied type
168 * for this listener list.
169 *
170 * @param t the type of listeners to count
171 * @return the number of listeners of type {@code t}
172 */
173 public int getListenerCount(Class<?> t) {
174 Object[] lList = listenerList;
175 return getListenerCount(lList, t);
176 }
177
178 private int getListenerCount(Object[] list, Class<?> t) {
179 int count = 0;
180 for (int i = 0; i < list.length; i+=2) {
181 if (t == (Class)list[i])
182 count++;
183 }
184 return count;
185 }
186
187 /**
188 * Adds the listener as a listener of the specified type.
189 *
190 * @param <T> the type of {@code EventListener} to add
191 * @param t the type of the {@code EventListener} class to add
192 * @param l the listener to be added
193 */
194 public synchronized <T extends EventListener> void add(Class<T> t, T l) {
195 if (l==null) {
196 // In an ideal world, we would do an assertion here
197 // to help developers know they are probably doing
198 // something wrong
274 if ((l!=null) && (l instanceof Serializable)) {
275 s.writeObject(t.getName());
276 s.writeObject(l);
277 }
278 }
279
280 s.writeObject(null);
281 }
282
283 private void readObject(ObjectInputStream s)
284 throws IOException, ClassNotFoundException {
285 listenerList = NULL_ARRAY;
286 s.defaultReadObject();
287 Object listenerTypeOrNull;
288
289 while (null != (listenerTypeOrNull = s.readObject())) {
290 ClassLoader cl = Thread.currentThread().getContextClassLoader();
291 EventListener l = (EventListener)s.readObject();
292 String name = (String) listenerTypeOrNull;
293 ReflectUtil.checkPackageAccess(name);
294 @SuppressWarnings("unchecked")
295 Class<EventListener> tmp = (Class<EventListener>)Class.forName(name, true, cl);
296 add(tmp, l);
297 }
298 }
299
300 /**
301 * Returns a string representation of the EventListenerList.
302 */
303 public String toString() {
304 Object[] lList = listenerList;
305 String s = "EventListenerList: ";
306 s += lList.length/2 + " listeners: ";
307 for (int i = 0 ; i <= lList.length-2 ; i+=2) {
308 s += " type " + ((Class)lList[i]).getName();
309 s += " listener " + lList[i+1];
310 }
311 return s;
312 }
313 }
|