|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc.event; |
|
27 |
|
28 import java.util.*; |
|
29 |
|
30 /** |
|
31 * The {@code EventListeners} class encapsulates event listener management. |
|
32 * <p/> |
|
33 * A note about thread safety: the {@link #add} and {@link #remove} methods are |
|
34 * synchronized to prevent concurrent modifications to the listener list. |
|
35 * However, {@link #dispatch} may safely iterate over this list while it's being |
|
36 * modified. |
|
37 * <p/> |
|
38 * The {@code Iterator}s returned by {@link #iterator}, however, will throw a |
|
39 * {@code ConcurrentModificationException} if the list changes during iteration. |
|
40 */ |
|
41 public class EventListeners<L> implements Iterable<L> { |
|
42 // |
|
43 // Inner classes |
|
44 // |
|
45 |
|
46 protected static class Entry<L> { |
|
47 public L data; |
|
48 public Entry<L> next; |
|
49 |
|
50 public Entry(L data) { |
|
51 this.data = data; |
|
52 } |
|
53 } |
|
54 |
|
55 public class EntryIterator implements Iterator<L> { |
|
56 // |
|
57 // Instance data |
|
58 // |
|
59 |
|
60 private int expectedModCount = modCount; |
|
61 private Entry<L> entry = first; |
|
62 |
|
63 // |
|
64 // Iterator methods |
|
65 // |
|
66 |
|
67 @Override |
|
68 public boolean hasNext() { |
|
69 synchronized (EventListeners.this) { |
|
70 checkForComodification(); |
|
71 return entry.next != null; |
|
72 } |
|
73 } |
|
74 |
|
75 @Override |
|
76 public L next() { |
|
77 synchronized (EventListeners.this) { |
|
78 checkForComodification(); |
|
79 entry = entry.next; |
|
80 return entry.data; |
|
81 } |
|
82 } |
|
83 |
|
84 @Override |
|
85 public void remove() { |
|
86 synchronized (EventListeners.this) { |
|
87 checkForComodification(); |
|
88 if (EventListeners.this.remove(entry.data)) { |
|
89 expectedModCount++; |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 // |
|
95 // Private methods |
|
96 // |
|
97 |
|
98 private void checkForComodification() { |
|
99 if (modCount != expectedModCount) { |
|
100 throw new ConcurrentModificationException(); |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 // |
|
106 // Instance data |
|
107 // |
|
108 |
|
109 private Entry<L> first; |
|
110 private int modCount; |
|
111 |
|
112 // |
|
113 // Iterable methods |
|
114 // |
|
115 |
|
116 @Override |
|
117 public Iterator<L> iterator() { |
|
118 return new EntryIterator(); |
|
119 } |
|
120 |
|
121 // |
|
122 // EventListeners methods |
|
123 // |
|
124 |
|
125 /** |
|
126 * Adds a listener to the list to be notified by {@link #dispatch}. |
|
127 */ |
|
128 public synchronized void add(L listener) { |
|
129 if (first == null) { |
|
130 first = new Entry<L>(listener); |
|
131 modCount++; |
|
132 } else { |
|
133 for (Entry<L> entry = first; ; entry = entry.next) { |
|
134 if (entry.data == listener) { |
|
135 return; |
|
136 } |
|
137 if (entry.next == null) { |
|
138 entry.next = new Entry<L>(listener); |
|
139 modCount++; |
|
140 break; |
|
141 } |
|
142 } |
|
143 } |
|
144 } |
|
145 |
|
146 /** |
|
147 * Removes all managed listeners. |
|
148 */ |
|
149 public synchronized void clear() { |
|
150 first = null; |
|
151 modCount = 0; |
|
152 } |
|
153 |
|
154 /** |
|
155 * Notifies each listener of the given event, in the order in which they |
|
156 * were {@link #add add}ed. |
|
157 * |
|
158 * @param dispatcher |
|
159 * the dispatcher that knows how to deliver the event |
|
160 * |
|
161 * @param event |
|
162 * the event to be delivered |
|
163 */ |
|
164 protected <E extends EventObject> void dispatch( |
|
165 EventDispatcher<E, L> dispatcher, E event) { |
|
166 |
|
167 for (Entry<L> entry = first; entry != null; entry = entry.next) { |
|
168 L listener = entry.data; |
|
169 dispatcher.dispatch(listener, event); |
|
170 } |
|
171 } |
|
172 |
|
173 /** |
|
174 * Removes a listener from the list to be notified by {@link #dispatch}. |
|
175 * |
|
176 * @return {@code true} if the list contained the given data, {@code |
|
177 * false} otherwise |
|
178 */ |
|
179 public synchronized boolean remove(L listener) { |
|
180 boolean found = false; |
|
181 if (first != null) { |
|
182 if (first.data == listener) { |
|
183 first = first.next; |
|
184 found = true; |
|
185 modCount++; |
|
186 } else { |
|
187 for (Entry<L> entry = first; entry != null && |
|
188 entry.next != null; entry = entry.next) { |
|
189 |
|
190 if (entry.next.data == listener) { |
|
191 entry.next = entry.next.next; |
|
192 found = true; |
|
193 modCount++; |
|
194 } |
|
195 } |
|
196 } |
|
197 } |
|
198 return found; |
|
199 } |
|
200 } |