Skip to main content
summaryrefslogtreecommitdiffstats
blob: 83aa5e26bda8c4d102a12d8a1144f8fcaee286fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Mikael Barbero (Eclipse Foundation) - Bug 509234
 *******************************************************************************/
package org.eclipse.core.runtime;

import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * This class is a thread safe list that is designed for storing lists of listeners.
 * The implementation is optimized for minimal memory footprint, frequent reads 
 * and infrequent writes.  Modification of the list is synchronized and relatively
 * expensive, while accessing the listeners is very fast.  For legacy code, readers are given access 
 * to the underlying array data structure for reading, with the trust that they will 
 * not modify the underlying array.
 * <p>
 * A listener list handles the <i>same</i> listener being added 
 * multiple times, and tolerates removal of listeners that are the same as other
 * listeners in the list.  For this purpose, listeners can be compared with each other 
 * using either equality or identity, as specified in the list constructor.
 * </p>
 * <p>
 * Use an enhanced 'for' loop to notify listeners. The recommended
 * code sequence for notifying all registered listeners of say,
 * <code>FooListener#eventHappened(Event)</code>, is:
 * </p>
 * <pre>
ListenerList&lt;FooListener&gt; fooListeners = new ListenerList&lt;&gt;();
//...
for (FooListener listener : fooListeners) {
	listener.eventHappened(event);
}
 * </pre>
 * <p>
 * Legacy code may still call {@link #getListeners()} and then use a 'for' loop
 * to iterate the {@code Object[]}. This might be insignificantly faster, but
 * it lacks type-safety and risks inadvertent modifications to the array.
 * </p>
 * <p>
 * This class can be used without OSGi running.
 * </p>
 * 
 * @param <E> the type of listeners in this list
 * @since org.eclipse.equinox.common 3.2
 */
public class ListenerList<E> implements Iterable<E> {

	/**
	 * The empty array singleton instance.
	 */
	private static final Object[] EmptyArray = new Object[0];

	/**
	 * Mode constant (value 0) indicating that listeners should be considered
	 * the <a href="ListenerList.html#same">same</a> if they are equal.
	 */
	public static final int EQUALITY = 0;

	/**
	 * Mode constant (value 1) indicating that listeners should be considered
	 * the <a href="ListenerList.html#same">same</a> if they are identical.
	 */
	public static final int IDENTITY = 1;

	/**
	 * Indicates the comparison mode used to determine if two
	 * listeners are equivalent
	 */
	private final boolean identity;

	/**
	 * The list of listeners.  Initially empty but initialized
	 * to an array of size capacity the first time a listener is added.
	 * Maintains invariant: listeners != null
	 */
	private volatile Object[] listeners = EmptyArray;

	/**
	 * Creates a listener list in which listeners are compared using equality.
	 */
	public ListenerList() {
		this(EQUALITY);
	}

	/**
	 * Creates a listener list using the provided comparison mode.
	 * 
	 * @param mode The mode used to determine if listeners are the <a href="ListenerList.html#same">same</a>.
	 */
	public ListenerList(int mode) {
		if (mode != EQUALITY && mode != IDENTITY)
			throw new IllegalArgumentException();
		this.identity = mode == IDENTITY;
	}

	/**
	 * Adds a listener to this list. This method has no effect if the <a href="ListenerList.html#same">same</a>
	 * listener is already registered.
	 * 
	 * @param listener the non-<code>null</code> listener to add
	 */
	public synchronized void add(E listener) {
		// This method is synchronized to protect against multiple threads adding 
		// or removing listeners concurrently. This does not block concurrent readers.
		if (listener == null)
			throw new IllegalArgumentException();
		// check for duplicates 
		final int oldSize = listeners.length;
		for (int i = 0; i < oldSize; ++i) {
			Object listener2 = listeners[i];
			if (identity ? listener == listener2 : listener.equals(listener2))
				return;
		}
		// Thread safety: create new array to avoid affecting concurrent readers
		Object[] newListeners = new Object[oldSize + 1];
		System.arraycopy(listeners, 0, newListeners, 0, oldSize);
		newListeners[oldSize] = listener;
		//atomic assignment
		this.listeners = newListeners;
	}

	/**
	 * Returns an array containing all the registered listeners.
	 * The resulting array is unaffected by subsequent adds or removes.
	 * If there are no listeners registered, the result is an empty array.
	 * Use this method when notifying listeners, so that any modifications
	 * to the listener list during the notification will have no effect on 
	 * the notification itself.
	 * <p>
	 * Note: Callers of this method <b>must not</b> modify the returned array.
	 * </p>
	 * <p>
	 * Note: The recommended and type-safe way to iterate this list is to use
	 * an enhanced 'for' statement, see {@link ListenerList}.
	 * This method is deprecated for new code.
	 * </p>
	 *
	 * @return the list of registered listeners
	 */
	public Object[] getListeners() {
		return listeners;
	}

	/**
	 * Returns an iterator over all the registered listeners.
	 * The resulting iterator is unaffected by subsequent adds or removes.
	 * Use this method when notifying listeners, so that any modifications
	 * to the listener list during the notification will have no effect on 
	 * the notification itself.
	 * 
	 * @return an iterator
	 * @since org.eclipse.equinox.common 3.8
	 */
	@Override
	public Iterator<E> iterator() {
		return new ListenerListIterator<>(listeners);
	}

	private static class ListenerListIterator<E> implements Iterator<E> {
		private Object[] listeners;
		private int i;

		public ListenerListIterator(Object[] listeners) {
			this.listeners = listeners;
		}

		@Override
		public boolean hasNext() {
			return i < listeners.length;
		}

		@Override
		public E next() {
			if (i >= listeners.length) {
				throw new NoSuchElementException();
			}
			@SuppressWarnings("unchecked") // (E) is safe, because #add(E) only accepts Es
			E next = (E) listeners[i++];
			return next;
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
	}

	/**
	 * Returns whether this listener list is empty.
	 *
	 * @return <code>true</code> if there are no registered listeners, and
	 *   <code>false</code> otherwise
	 */
	public boolean isEmpty() {
		return listeners.length == 0;
	}

	/**
	 * Removes a listener from this list. Has no effect if the <a href="ListenerList.html#same">same</a> 
	 * listener was not already registered.
	 *
	 * @param listener the non-<code>null</code> listener to remove
	 */
	public synchronized void remove(Object listener) {
		// This method is synchronized to protect against multiple threads adding 
		// or removing listeners concurrently. This does not block concurrent readers.
		if (listener == null)
			throw new IllegalArgumentException();
		int oldSize = listeners.length;
		for (int i = 0; i < oldSize; ++i) {
			Object listener2 = listeners[i];
			if (identity ? listener == listener2 : listener.equals(listener2)) {
				if (oldSize == 1) {
					listeners = EmptyArray;
				} else {
					// Thread safety: create new array to avoid affecting concurrent readers
					Object[] newListeners = new Object[oldSize - 1];
					System.arraycopy(listeners, 0, newListeners, 0, i);
					System.arraycopy(listeners, i + 1, newListeners, i, oldSize - i - 1);
					//atomic assignment to field
					this.listeners = newListeners;
				}
				return;
			}
		}
	}

	/**
	 * Returns the number of registered listeners.
	 *
	 * @return the number of registered listeners
	 */
	public int size() {
		return listeners.length;
	}

	/**
	 * Removes all listeners from this list.
	 */
	public synchronized void clear() {
		listeners = EmptyArray;
	}

	/**
	 * Returns a Spliterator covering the registered listeners.
	 * <p>
	 * The spliterator reports Spliterator.SIZED, Spliterator.SUBSIZED, Spliterator.ORDERED, and Spliterator.IMMUTABLE
	 * 
	 * @return a spliterator for listeners
	 * @since org.eclipse.equinox.common 3.9
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Spliterator<E> spliterator() {
		return (Spliterator<E>) Arrays.spliterator(listeners);
	}

	/**
	 * Returns a sequential {@code Stream} over the registered listeners.
	 * 
	 * @return a sequential {@code Stream} over the registered listeners.
	 * @since org.eclipse.equinox.common 3.9
	 */
	public Stream<E> stream() {
		return StreamSupport.stream(spliterator(), false);
	}

	/**
	 * Returns a parallel {@code Stream} over the registered listeners.
	 * 
	 * @return a parallel {@code Stream} over the registered listeners.
	 * @since org.eclipse.equinox.common 3.9
	 */
	public Stream<E> parallelStream() {
		return StreamSupport.stream(spliterator(), true);
	}

	@Override
	public String toString() {
		return Arrays.toString(listeners);
	}
}

Back to the top