KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > 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.corext.refactoring;
12
13 import org.eclipse.jdt.internal.corext.Assert;
14
15 /**
16  * Local copy of org.eclipse.jface.ListenerList
17  */

18 public class ListenerList {
19
20     static {
21         //XXX: 1GCQD0A: ITPVCM:WIN2000 - ListenerList should be part of a core project
22
}
23     
24     /**
25      * The initial capacity of the list. Always >= 1.
26      */

27     private int capacity;
28
29     /**
30      * The current number of listeners.
31      * Maintains invariant: 0 <= size <= listeners.length.
32      */

33     private int size;
34
35     /**
36      * The list of listeners. Initially <code>null</code> but initialized
37      * to an array of size capacity the first time a listener is added.
38      * Maintains invariant: listeners != null IFF size != 0
39      */

40     private Object JavaDoc[] listeners= null;
41
42     /**
43      * The empty array singleton instance, returned by getListeners()
44      * when size == 0.
45      */

46     private static final Object JavaDoc[] EmptyArray= new Object JavaDoc[0];
47     
48     /**
49      * Creates a listener list with an initial capacity of 3.
50      */

51     public ListenerList() {
52         this(3);
53     }
54     
55     /**
56      * Creates a listener list with the given initial capacity.
57      *
58      * @param capacity the number of listeners which this list can initially accept
59      * without growing its internal representation; must be at least 1
60      */

61     public ListenerList(int capacity) {
62         Assert.isTrue(capacity >= 1);
63         this.capacity= capacity;
64     }
65     
66     /**
67      * Adds the given listener to this list. Has no effect if an identical listener
68      * is already registered.
69      *
70      * @param listener the listener
71      */

72     public void add(Object JavaDoc listener) {
73         Assert.isNotNull(listener);
74         if (size == 0) {
75             listeners= new Object JavaDoc[capacity];
76         } else {
77             // check for duplicates using identity
78
for (int i= 0; i < size; ++i) {
79                 if (listeners[i] == listener) {
80                     return;
81                 }
82             }
83             // grow array if necessary
84
if (size == listeners.length) {
85                 System.arraycopy(listeners, 0, listeners= new Object JavaDoc[size * 2 + 1], 0, size);
86             }
87         }
88         listeners[size++]= listener;
89     }
90     
91     /**
92      * Returns an array containing all the registered listeners.
93      * The resulting array is unaffected by subsequent adds or removes.
94      * If there are no listeners registered, the result is an empty array
95      * singleton instance (no garbage is created).
96      * Use this method when notifying listeners, so that any modifications
97      * to the listener list during the notification will have no effect on the
98      * notification itself.
99      *
100      * @return the list of registered listeners
101      */

102     public Object JavaDoc[] getListeners() {
103         if (size == 0)
104             return EmptyArray;
105         Object JavaDoc[] result= new Object JavaDoc[size];
106         System.arraycopy(listeners, 0, result, 0, size);
107         return result;
108     }
109     
110     /**
111      * Returns whether this listener list is empty.
112      *
113      * @return <code>true</code> if there are no registered listeners, and
114      * <code>false</code> otherwise
115      */

116     public boolean isEmpty() {
117         return size == 0;
118     }
119     
120     /**
121      * Removes the given listener from this list. Has no effect if an identical
122      * listener was not already registered.
123      *
124      * @param listener the listener
125      */

126     public void remove(Object JavaDoc listener) {
127         Assert.isNotNull(listener);
128         for (int i= 0; i < size; ++i) {
129             if (listeners[i] == listener) {
130                 if (--size == 0) {
131                     listeners= new Object JavaDoc[1];
132                 } else {
133                     if (i < size) {
134                         listeners[i]= listeners[size];
135                     }
136                     listeners[size]= null;
137                 }
138                 return;
139             }
140         }
141     }
142     
143     /**
144      * Returns the number of registered listeners.
145      *
146      * @return the number of registered listeners
147      */

148     public int size() {
149         return size;
150     }
151 }
152
Popular Tags