KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > ListenerList


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching;
12
13
14 /**
15  * Local version of org.eclipse.jface.util.ListenerList (modified)s
16  */

17 public class ListenerList {
18     /**
19      * The current number of listeners.
20      * Maintains invariant: 0 <= fSize <= listeners.length.
21      */

22     private int fSize;
23
24     /**
25      * The list of listeners. Initially <code>null</code> but initialized
26      * to an array of size capacity the first time a listener is added.
27      * Maintains invariant: listeners != null if and only if fSize != 0
28      */

29     private Object JavaDoc[] fListeners= null;
30
31     /**
32      * The empty array singleton instance, returned by getListeners()
33      * when size == 0.
34      */

35     private static final Object JavaDoc[] EmptyArray= new Object JavaDoc[0];
36
37     /**
38      * Creates a listener list with the given initial capacity.
39      *
40      * @param capacity the number of listeners which this list can initially accept
41      * without growing its internal representation; must be at least 1
42      */

43     public ListenerList(int capacity) {
44         if (capacity < 1) {
45             throw new IllegalArgumentException JavaDoc();
46         }
47         fListeners= new Object JavaDoc[capacity];
48         fSize= 0;
49     }
50
51     /**
52      * Adds a listener to the list.
53      * Has no effect if an identical listener is already registered.
54      *
55      * @param listener a listener
56      */

57     public synchronized void add(Object JavaDoc listener) {
58         if (listener == null) {
59             throw new IllegalArgumentException JavaDoc();
60         }
61         // check for duplicates using identity
62
for (int i= 0; i < fSize; ++i) {
63             if (fListeners[i] == listener) {
64                 return;
65             }
66         }
67         // grow array if necessary
68
if (fSize == fListeners.length) {
69             Object JavaDoc[] temp= new Object JavaDoc[(fSize * 2) + 1];
70             System.arraycopy(fListeners, 0, temp, 0, fSize);
71             fListeners= temp;
72         }
73         fListeners[fSize++]= listener;
74     }
75
76     /**
77      * Returns an array containing all the registered listeners.
78      * The resulting array is unaffected by subsequent adds or removes.
79      * If there are no listeners registered, the result is an empty array
80      * singleton instance (no garbage is created).
81      * Use this method when notifying listeners, so that any modifications
82      * to the listener list during the notification will have no effect on the
83      * notification itself.
84      */

85     public synchronized Object JavaDoc[] getListeners() {
86         if (fSize == 0) {
87             return EmptyArray;
88         }
89         Object JavaDoc[] result= new Object JavaDoc[fSize];
90         System.arraycopy(fListeners, 0, result, 0, fSize);
91         return result;
92     }
93
94     /**
95      * Removes a listener from the list.
96      * Has no effect if an identical listener was not already registered.
97      *
98      * @param listener a listener
99      */

100     public synchronized void remove(Object JavaDoc listener) {
101         if (listener == null) {
102             throw new IllegalArgumentException JavaDoc();
103         }
104
105         for (int i= 0; i < fSize; ++i) {
106             if (fListeners[i] == listener) {
107                 if (--fSize == 0) {
108                     fListeners= new Object JavaDoc[1];
109                 } else {
110                     if (i < fSize) {
111                         fListeners[i]= fListeners[fSize];
112                     }
113                     fListeners[fSize]= null;
114                 }
115                 return;
116             }
117         }
118     }
119
120     /**
121      * Removes all the listeners from the list.
122      */

123     public void removeAll() {
124         fListeners= new Object JavaDoc[0];
125         fSize= 0;
126     }
127
128     /**
129      * Returns the number of registered listeners
130      *
131      * @return the number of registered listeners
132      */

133     public int size() {
134         return fSize;
135     }
136 }
137
138
Popular Tags