Skip to main content
summaryrefslogtreecommitdiffstats
blob: 632b3fd5df0d94e35b5446e32f13f1c784665c48 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.EventListener;

import org.eclipse.jpt.common.utility.internal.iterables.ArrayIterable;

/**
 * Maintain a thread-safe list of listeners that does not allow adding
 * duplicate listeners or removing non-listeners.
 */
public class ListenerList<L extends EventListener>
	implements Serializable
{
	/**
	 * We can mark this volatile and not synchronize the read methods because
	 * we never change the <em>contents</em> of the array.
	 */
	private transient volatile L[] listeners;

	private static final long serialVersionUID = 1L;


	/**
	 * Construct a listener list for listeners of the specified type.
	 */
	public ListenerList(Class<L> listenerClass) {
		super();
		this.listeners = this.buildListenerArray(listenerClass, 0);
	}

	/**
	 * Construct a listener list for listeners of the specified type.
	 * Add the specified listener to the list.
	 */
	public ListenerList(Class<L> listenerClass, L listener) {
		super();
		if (listener == null) {
			throw new NullPointerException();
		}
		this.listeners = this.buildListenerArray(listenerClass, 1);
		this.listeners[0] = listener;
	}

	@SuppressWarnings("unchecked")
	private L[] buildListenerArray(Class<L> listenerClass, int length) {
		return (L[]) Array.newInstance(listenerClass, length);
	}

	/**
	 * Return the listeners.
	 */
	public Iterable<L> getListeners() {
		return new ArrayIterable<L>(this.listeners);
	}

	/**
	 * Return the number of listeners.
	 */
	public int size() {
		return this.listeners.length;
	}

	/**
	 * Return whether the listener list has no listeners.
	 */
	public boolean isEmpty() {
		return this.listeners.length == 0;
	}

	/**
	 * Add the specified listener to the listener list.
	 * Duplicate listeners are not allowed.
	 */
	public synchronized void add(L listener) {
		if (listener == null) {
			throw new NullPointerException();
		}
		if (ArrayTools.contains(this.listeners, listener)) {
			throw new IllegalArgumentException("duplicate listener: " + listener); //$NON-NLS-1$
		}
		this.listeners = ArrayTools.add(this.listeners, listener);
	}

	/**
	 * Remove the specified listener from the listener list.
	 * Removing a listener that is not on the list is not allowed.
	 */
	public synchronized void remove(L listener) {
		if (listener == null) {
			throw new NullPointerException();
		}
		int index = ArrayTools.indexOf(this.listeners, listener);
		if (index == -1) {
			throw new IllegalArgumentException("unregistered listener: " + listener); //$NON-NLS-1$
		}
		this.listeners = ArrayTools.removeElementAtIndex(this.listeners, index);
	}

	/**
	 * Clear the listener list.
	 */
	public synchronized void clear() {
		this.listeners = ArrayTools.clear(this.listeners);
	}

	/**
	 * Return the type of listeners held by the listener list.
	 */
	@SuppressWarnings("unchecked")
	public Class<L> getListenerType() {
		return (Class<L>) this.listeners.getClass().getComponentType();
	}

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


	// ********** serialization **********

	/**
	 * Silently drop any non-serializable listeners.
	 */
	private synchronized void writeObject(ObjectOutputStream s) throws IOException {
		// write out any hidden stuff
		s.defaultWriteObject();

		@SuppressWarnings("unchecked")
		Class<L> listenerClass = (Class<L>) this.listeners.getClass().getComponentType();
		s.writeObject(listenerClass);

		// only write out serializable listeners
		for (L listener : this.listeners) {
			if (listener instanceof Serializable) {
				s.writeObject(listener);
			}
		}

		s.writeObject(null);
    }

	@SuppressWarnings("unchecked")
	private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
		// read in any hidden stuff
		s.defaultReadObject();

		Class<L> listenerClass = (Class<L>) s.readObject();
		this.listeners = this.buildListenerArray(listenerClass, 0);
		Object o;
		while ((o = s.readObject()) != null) {
			this.listeners = ArrayTools.add(this.listeners, (L) o);
		}
	}

}

Back to the top