KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > ListenerList


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.ltk.internal.core.refactoring;
12
13 /**
14  * Local copy of org.eclipse.jface.ListenerList
15  */

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

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

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

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

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

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

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

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

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

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

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

146     public int size() {
147         return size;
148     }
149 }
150
Popular Tags