KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > ListenerList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.editor.util;
21
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.AbstractList JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.EventListener JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * Listener list storing listeners of a single type.
34  *
35  * @author Miloslav Metelka
36  * @since 1.11
37  */

38
39 public final class ListenerList<T extends EventListener JavaDoc> implements Serializable JavaDoc {
40
41     static final long serialVersionUID = 0L;
42     
43     /** A null array to be shared by all empty listener lists */
44     private static final EventListener JavaDoc[] EMPTY_LISTENER_ARRAY = new EventListener JavaDoc[0];
45     
46     /* The array of listeners. */
47     private transient ImmutableList<T> listenersList;
48     
49     public ListenerList() {
50         listenersList = new ImmutableList<T>(EMPTY_LISTENER_ARRAY);
51     }
52     
53     /**
54      * Returns a list of listeners.
55      *
56      * <p>The listeners are returned in exactly the same order
57      * as they were added to this list. Use the following code
58      * for firing events to all the listeners you get from this
59      * method.
60      *
61      * <pre>
62      * List<MyListener> listeners = listenerList.getListeners();
63      * for (MyListener l : listeners) {
64      * l.notify(evt);
65      * }
66      * </pre>
67      *
68      * @return An immutable list of listeners contained in this listener list.
69      */

70     public synchronized List JavaDoc<T> getListeners() {
71         return listenersList;
72     }
73     
74     /**
75      * Returns the total number of listeners for this listener list.
76      */

77     public synchronized int getListenerCount() {
78         return listenersList.size();
79     }
80     
81     /**
82      * Adds the given listener to this listener list.
83      *
84      * @param listener the listener to be added. If null is passed it is ignored (nothing gets added).
85      */

86     public synchronized void add(T listener) {
87         if (listener == null)
88             return;
89
90         EventListener JavaDoc [] arr = new EventListener JavaDoc[listenersList.getArray().length + 1];
91         if (arr.length > 1) {
92             System.arraycopy(listenersList.getArray(), 0, arr, 0, arr.length - 1);
93         }
94         arr[arr.length - 1] = listener;
95         
96         listenersList = new ImmutableList<T>(arr);
97     }
98     
99     /**
100      * Removes the given listener from this listener list.
101      *
102      * @param listener the listener to be removed. If null is passed it is ignored (nothing gets removed).
103      */

104     public synchronized void remove(T listener) {
105         if (listener == null)
106             return;
107
108         int idx = listenersList.indexOf(listener);
109         if (idx == -1) {
110             return;
111         }
112         
113         EventListener JavaDoc [] arr = new EventListener JavaDoc[listenersList.getArray().length - 1];
114         if (arr.length > 0) {
115             System.arraycopy(listenersList.getArray(), 0, arr, 0, idx);
116         }
117         if (arr.length > idx) {
118             System.arraycopy(listenersList.getArray(), idx + 1, arr, idx, listenersList.getArray().length - idx - 1);
119         }
120         
121         listenersList = new ImmutableList<T>(arr);
122     }
123     
124     // Serialization support.
125
private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
126         s.defaultWriteObject();
127         
128         // Write in opposite order of adding
129
for (Iterator JavaDoc<T> i = listenersList.iterator(); i.hasNext(); ) {
130             T l = i.next();
131             // Save only the serializable listeners
132
if (l instanceof Serializable JavaDoc) {
133                 s.writeObject(l);
134             }
135         }
136         
137         s.writeObject(null);
138     }
139     
140     private void readObject(ObjectInputStream JavaDoc s) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
141         s.defaultReadObject();
142         List JavaDoc<T> lList = new ArrayList JavaDoc<T>();
143         Object JavaDoc listenerOrNull;
144         while (null != (listenerOrNull = s.readObject())) {
145             @SuppressWarnings JavaDoc("unchecked")
146             T l = (T)listenerOrNull;
147             lList.add(l);
148         }
149         this.listenersList = new ImmutableList<T>((EventListener JavaDoc [])lList.toArray(new EventListener JavaDoc[lList.size()]));
150     }
151     
152     public String JavaDoc toString() {
153         return listenersList.toString();
154     }
155
156     private static final class ImmutableList<E extends EventListener JavaDoc> extends AbstractList JavaDoc<E> {
157
158         private EventListener JavaDoc[] array;
159         
160         public ImmutableList(EventListener JavaDoc[] array) {
161             super();
162             
163             assert array != null : "The array can't be null"; //NOI18N
164
this.array = array;
165         }
166         
167         public E get(int index) {
168             if (index >= 0 && index < array.length) {
169                 @SuppressWarnings JavaDoc("unchecked")
170                 E element = (E) array[index];
171                 return element;
172             } else {
173                 throw new IndexOutOfBoundsException JavaDoc("index = " + index + ", size = " + array.length); //NOI18N
174
}
175         }
176         
177         public int size() {
178             return array.length;
179         }
180         
181         public EventListener JavaDoc[] getArray() {
182             return array;
183         }
184     } // End of ImmutableList class
185
}
186
Popular Tags