1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.3 |
|
9 |
dl |
1.1 |
import java.util.*; |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* A {@link java.util.Set} that uses an internal {@link CopyOnWriteArrayList} |
13 |
|
|
* for all of its operations. Thus, it shares the same basic properties: |
14 |
|
|
* <ul> |
15 |
|
|
* <li>It is best suited for applications in which set sizes generally |
16 |
|
|
* stay small, read-only operations |
17 |
|
|
* vastly outnumber mutative operations, and you need |
18 |
|
|
* to prevent interference among threads during traversal. |
19 |
|
|
* <li>It is thread-safe. |
20 |
jsr166 |
1.2 |
* <li>Mutative operations ({@code add}, {@code set}, {@code remove}, etc.) |
21 |
dl |
1.1 |
* are expensive since they usually entail copying the entire underlying |
22 |
|
|
* array. |
23 |
jsr166 |
1.2 |
* <li>Iterators do not support the mutative {@code remove} operation. |
24 |
dl |
1.1 |
* <li>Traversal via iterators is fast and cannot encounter |
25 |
|
|
* interference from other threads. Iterators rely on |
26 |
|
|
* unchanging snapshots of the array at the time the iterators were |
27 |
|
|
* constructed. |
28 |
|
|
* </ul> |
29 |
|
|
* |
30 |
|
|
* <p><b>Sample Usage.</b> The following code sketch uses a |
31 |
|
|
* copy-on-write set to maintain a set of Handler objects that |
32 |
|
|
* perform some action upon state updates. |
33 |
|
|
* |
34 |
|
|
* <pre> {@code |
35 |
|
|
* class Handler { void handle(); ... } |
36 |
|
|
* |
37 |
|
|
* class X { |
38 |
|
|
* private final CopyOnWriteArraySet<Handler> handlers |
39 |
|
|
* = new CopyOnWriteArraySet<Handler>(); |
40 |
|
|
* public void addHandler(Handler h) { handlers.add(h); } |
41 |
|
|
* |
42 |
|
|
* private long internalState; |
43 |
|
|
* private synchronized void changeState() { internalState = ...; } |
44 |
|
|
* |
45 |
|
|
* public void update() { |
46 |
|
|
* changeState(); |
47 |
|
|
* for (Handler handler : handlers) |
48 |
|
|
* handler.handle(); |
49 |
|
|
* } |
50 |
|
|
* }}</pre> |
51 |
|
|
* |
52 |
|
|
* <p>This class is a member of the |
53 |
|
|
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
54 |
|
|
* Java Collections Framework</a>. |
55 |
|
|
* |
56 |
|
|
* @see CopyOnWriteArrayList |
57 |
|
|
* @since 1.5 |
58 |
|
|
* @author Doug Lea |
59 |
|
|
* @param <E> the type of elements held in this collection |
60 |
|
|
*/ |
61 |
|
|
public class CopyOnWriteArraySet<E> extends AbstractSet<E> |
62 |
|
|
implements java.io.Serializable { |
63 |
|
|
private static final long serialVersionUID = 5457747651344034263L; |
64 |
|
|
|
65 |
|
|
private final CopyOnWriteArrayList<E> al; |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Creates an empty set. |
69 |
|
|
*/ |
70 |
|
|
public CopyOnWriteArraySet() { |
71 |
|
|
al = new CopyOnWriteArrayList<E>(); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* Creates a set containing all of the elements of the specified |
76 |
|
|
* collection. |
77 |
|
|
* |
78 |
|
|
* @param c the collection of elements to initially contain |
79 |
|
|
* @throws NullPointerException if the specified collection is null |
80 |
|
|
*/ |
81 |
|
|
public CopyOnWriteArraySet(Collection<? extends E> c) { |
82 |
|
|
al = new CopyOnWriteArrayList<E>(); |
83 |
|
|
al.addAllAbsent(c); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Returns the number of elements in this set. |
88 |
|
|
* |
89 |
|
|
* @return the number of elements in this set |
90 |
|
|
*/ |
91 |
|
|
public int size() { |
92 |
|
|
return al.size(); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
jsr166 |
1.2 |
* Returns {@code true} if this set contains no elements. |
97 |
dl |
1.1 |
* |
98 |
jsr166 |
1.2 |
* @return {@code true} if this set contains no elements |
99 |
dl |
1.1 |
*/ |
100 |
|
|
public boolean isEmpty() { |
101 |
|
|
return al.isEmpty(); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
jsr166 |
1.2 |
* Returns {@code true} if this set contains the specified element. |
106 |
|
|
* More formally, returns {@code true} if and only if this set |
107 |
|
|
* contains an element {@code e} such that |
108 |
dl |
1.1 |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
109 |
|
|
* |
110 |
|
|
* @param o element whose presence in this set is to be tested |
111 |
jsr166 |
1.2 |
* @return {@code true} if this set contains the specified element |
112 |
dl |
1.1 |
*/ |
113 |
|
|
public boolean contains(Object o) { |
114 |
|
|
return al.contains(o); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/** |
118 |
|
|
* Returns an array containing all of the elements in this set. |
119 |
|
|
* If this set makes any guarantees as to what order its elements |
120 |
|
|
* are returned by its iterator, this method must return the |
121 |
|
|
* elements in the same order. |
122 |
|
|
* |
123 |
|
|
* <p>The returned array will be "safe" in that no references to it |
124 |
|
|
* are maintained by this set. (In other words, this method must |
125 |
|
|
* allocate a new array even if this set is backed by an array). |
126 |
|
|
* The caller is thus free to modify the returned array. |
127 |
|
|
* |
128 |
|
|
* <p>This method acts as bridge between array-based and collection-based |
129 |
|
|
* APIs. |
130 |
|
|
* |
131 |
|
|
* @return an array containing all the elements in this set |
132 |
|
|
*/ |
133 |
|
|
public Object[] toArray() { |
134 |
|
|
return al.toArray(); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* Returns an array containing all of the elements in this set; the |
139 |
|
|
* runtime type of the returned array is that of the specified array. |
140 |
|
|
* If the set fits in the specified array, it is returned therein. |
141 |
|
|
* Otherwise, a new array is allocated with the runtime type of the |
142 |
|
|
* specified array and the size of this set. |
143 |
|
|
* |
144 |
|
|
* <p>If this set fits in the specified array with room to spare |
145 |
|
|
* (i.e., the array has more elements than this set), the element in |
146 |
|
|
* the array immediately following the end of the set is set to |
147 |
jsr166 |
1.2 |
* {@code null}. (This is useful in determining the length of this |
148 |
dl |
1.1 |
* set <i>only</i> if the caller knows that this set does not contain |
149 |
|
|
* any null elements.) |
150 |
|
|
* |
151 |
|
|
* <p>If this set makes any guarantees as to what order its elements |
152 |
|
|
* are returned by its iterator, this method must return the elements |
153 |
|
|
* in the same order. |
154 |
|
|
* |
155 |
|
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
156 |
|
|
* array-based and collection-based APIs. Further, this method allows |
157 |
|
|
* precise control over the runtime type of the output array, and may, |
158 |
|
|
* under certain circumstances, be used to save allocation costs. |
159 |
|
|
* |
160 |
jsr166 |
1.2 |
* <p>Suppose {@code x} is a set known to contain only strings. |
161 |
dl |
1.1 |
* The following code can be used to dump the set into a newly allocated |
162 |
jsr166 |
1.2 |
* array of {@code String}: |
163 |
dl |
1.1 |
* |
164 |
|
|
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
165 |
|
|
* |
166 |
jsr166 |
1.2 |
* Note that {@code toArray(new Object[0])} is identical in function to |
167 |
|
|
* {@code toArray()}. |
168 |
dl |
1.1 |
* |
169 |
|
|
* @param a the array into which the elements of this set are to be |
170 |
|
|
* stored, if it is big enough; otherwise, a new array of the same |
171 |
|
|
* runtime type is allocated for this purpose. |
172 |
|
|
* @return an array containing all the elements in this set |
173 |
|
|
* @throws ArrayStoreException if the runtime type of the specified array |
174 |
|
|
* is not a supertype of the runtime type of every element in this |
175 |
|
|
* set |
176 |
|
|
* @throws NullPointerException if the specified array is null |
177 |
|
|
*/ |
178 |
|
|
public <T> T[] toArray(T[] a) { |
179 |
|
|
return al.toArray(a); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/** |
183 |
|
|
* Removes all of the elements from this set. |
184 |
|
|
* The set will be empty after this call returns. |
185 |
|
|
*/ |
186 |
|
|
public void clear() { |
187 |
|
|
al.clear(); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
/** |
191 |
|
|
* Removes the specified element from this set if it is present. |
192 |
jsr166 |
1.2 |
* More formally, removes an element {@code e} such that |
193 |
dl |
1.1 |
* <tt>(o==null ? e==null : o.equals(e))</tt>, |
194 |
jsr166 |
1.2 |
* if this set contains such an element. Returns {@code true} if |
195 |
dl |
1.1 |
* this set contained the element (or equivalently, if this set |
196 |
|
|
* changed as a result of the call). (This set will not contain the |
197 |
|
|
* element once the call returns.) |
198 |
|
|
* |
199 |
|
|
* @param o object to be removed from this set, if present |
200 |
jsr166 |
1.2 |
* @return {@code true} if this set contained the specified element |
201 |
dl |
1.1 |
*/ |
202 |
|
|
public boolean remove(Object o) { |
203 |
|
|
return al.remove(o); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
/** |
207 |
|
|
* Adds the specified element to this set if it is not already present. |
208 |
jsr166 |
1.2 |
* More formally, adds the specified element {@code e} to this set if |
209 |
|
|
* the set contains no element {@code e2} such that |
210 |
dl |
1.1 |
* <tt>(e==null ? e2==null : e.equals(e2))</tt>. |
211 |
|
|
* If this set already contains the element, the call leaves the set |
212 |
jsr166 |
1.2 |
* unchanged and returns {@code false}. |
213 |
dl |
1.1 |
* |
214 |
|
|
* @param e element to be added to this set |
215 |
jsr166 |
1.2 |
* @return {@code true} if this set did not already contain the specified |
216 |
dl |
1.1 |
* element |
217 |
|
|
*/ |
218 |
|
|
public boolean add(E e) { |
219 |
|
|
return al.addIfAbsent(e); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
/** |
223 |
jsr166 |
1.2 |
* Returns {@code true} if this set contains all of the elements of the |
224 |
dl |
1.1 |
* specified collection. If the specified collection is also a set, this |
225 |
jsr166 |
1.2 |
* method returns {@code true} if it is a <i>subset</i> of this set. |
226 |
dl |
1.1 |
* |
227 |
|
|
* @param c collection to be checked for containment in this set |
228 |
jsr166 |
1.2 |
* @return {@code true} if this set contains all of the elements of the |
229 |
dl |
1.1 |
* specified collection |
230 |
|
|
* @throws NullPointerException if the specified collection is null |
231 |
|
|
* @see #contains(Object) |
232 |
|
|
*/ |
233 |
|
|
public boolean containsAll(Collection<?> c) { |
234 |
|
|
return al.containsAll(c); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
/** |
238 |
|
|
* Adds all of the elements in the specified collection to this set if |
239 |
|
|
* they're not already present. If the specified collection is also a |
240 |
jsr166 |
1.2 |
* set, the {@code addAll} operation effectively modifies this set so |
241 |
dl |
1.1 |
* that its value is the <i>union</i> of the two sets. The behavior of |
242 |
|
|
* this operation is undefined if the specified collection is modified |
243 |
|
|
* while the operation is in progress. |
244 |
|
|
* |
245 |
|
|
* @param c collection containing elements to be added to this set |
246 |
jsr166 |
1.2 |
* @return {@code true} if this set changed as a result of the call |
247 |
dl |
1.1 |
* @throws NullPointerException if the specified collection is null |
248 |
|
|
* @see #add(Object) |
249 |
|
|
*/ |
250 |
|
|
public boolean addAll(Collection<? extends E> c) { |
251 |
|
|
return al.addAllAbsent(c) > 0; |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
/** |
255 |
|
|
* Removes from this set all of its elements that are contained in the |
256 |
|
|
* specified collection. If the specified collection is also a set, |
257 |
|
|
* this operation effectively modifies this set so that its value is the |
258 |
|
|
* <i>asymmetric set difference</i> of the two sets. |
259 |
|
|
* |
260 |
|
|
* @param c collection containing elements to be removed from this set |
261 |
jsr166 |
1.2 |
* @return {@code true} if this set changed as a result of the call |
262 |
dl |
1.1 |
* @throws ClassCastException if the class of an element of this set |
263 |
|
|
* is incompatible with the specified collection (optional) |
264 |
|
|
* @throws NullPointerException if this set contains a null element and the |
265 |
|
|
* specified collection does not permit null elements (optional), |
266 |
|
|
* or if the specified collection is null |
267 |
|
|
* @see #remove(Object) |
268 |
|
|
*/ |
269 |
|
|
public boolean removeAll(Collection<?> c) { |
270 |
|
|
return al.removeAll(c); |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
/** |
274 |
|
|
* Retains only the elements in this set that are contained in the |
275 |
|
|
* specified collection. In other words, removes from this set all of |
276 |
|
|
* its elements that are not contained in the specified collection. If |
277 |
|
|
* the specified collection is also a set, this operation effectively |
278 |
|
|
* modifies this set so that its value is the <i>intersection</i> of the |
279 |
|
|
* two sets. |
280 |
|
|
* |
281 |
|
|
* @param c collection containing elements to be retained in this set |
282 |
jsr166 |
1.2 |
* @return {@code true} if this set changed as a result of the call |
283 |
dl |
1.1 |
* @throws ClassCastException if the class of an element of this set |
284 |
|
|
* is incompatible with the specified collection (optional) |
285 |
|
|
* @throws NullPointerException if this set contains a null element and the |
286 |
|
|
* specified collection does not permit null elements (optional), |
287 |
|
|
* or if the specified collection is null |
288 |
|
|
* @see #remove(Object) |
289 |
|
|
*/ |
290 |
|
|
public boolean retainAll(Collection<?> c) { |
291 |
|
|
return al.retainAll(c); |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
/** |
295 |
|
|
* Returns an iterator over the elements contained in this set |
296 |
|
|
* in the order in which these elements were added. |
297 |
|
|
* |
298 |
|
|
* <p>The returned iterator provides a snapshot of the state of the set |
299 |
|
|
* when the iterator was constructed. No synchronization is needed while |
300 |
|
|
* traversing the iterator. The iterator does <em>NOT</em> support the |
301 |
jsr166 |
1.2 |
* {@code remove} method. |
302 |
dl |
1.1 |
* |
303 |
|
|
* @return an iterator over the elements in this set |
304 |
|
|
*/ |
305 |
|
|
public Iterator<E> iterator() { |
306 |
|
|
return al.iterator(); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
/** |
310 |
|
|
* Compares the specified object with this set for equality. |
311 |
|
|
* Returns {@code true} if the specified object is the same object |
312 |
|
|
* as this object, or if it is also a {@link Set} and the elements |
313 |
|
|
* returned by an {@linkplain List#iterator() iterator} over the |
314 |
|
|
* specified set are the same as the elements returned by an |
315 |
|
|
* iterator over this set. More formally, the two iterators are |
316 |
|
|
* considered to return the same elements if they return the same |
317 |
|
|
* number of elements and for every element {@code e1} returned by |
318 |
|
|
* the iterator over the specified set, there is an element |
319 |
|
|
* {@code e2} returned by the iterator over this set such that |
320 |
|
|
* {@code (e1==null ? e2==null : e1.equals(e2))}. |
321 |
|
|
* |
322 |
|
|
* @param o object to be compared for equality with this set |
323 |
|
|
* @return {@code true} if the specified object is equal to this set |
324 |
|
|
*/ |
325 |
|
|
public boolean equals(Object o) { |
326 |
|
|
if (o == this) |
327 |
|
|
return true; |
328 |
|
|
if (!(o instanceof Set)) |
329 |
|
|
return false; |
330 |
|
|
Set<?> set = (Set<?>)(o); |
331 |
|
|
Iterator<?> it = set.iterator(); |
332 |
|
|
|
333 |
|
|
// Uses O(n^2) algorithm that is only appropriate |
334 |
|
|
// for small sets, which CopyOnWriteArraySets should be. |
335 |
|
|
|
336 |
|
|
// Use a single snapshot of underlying array |
337 |
|
|
Object[] elements = al.getArray(); |
338 |
|
|
int len = elements.length; |
339 |
|
|
// Mark matched elements to avoid re-checking |
340 |
|
|
boolean[] matched = new boolean[len]; |
341 |
|
|
int k = 0; |
342 |
|
|
outer: while (it.hasNext()) { |
343 |
|
|
if (++k > len) |
344 |
|
|
return false; |
345 |
|
|
Object x = it.next(); |
346 |
|
|
for (int i = 0; i < len; ++i) { |
347 |
|
|
if (!matched[i] && eq(x, elements[i])) { |
348 |
|
|
matched[i] = true; |
349 |
|
|
continue outer; |
350 |
|
|
} |
351 |
|
|
} |
352 |
|
|
return false; |
353 |
|
|
} |
354 |
|
|
return k == len; |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/** |
358 |
|
|
* Tests for equality, coping with nulls. |
359 |
|
|
*/ |
360 |
|
|
private static boolean eq(Object o1, Object o2) { |
361 |
|
|
return (o1 == null) ? o2 == null : o1.equals(o2); |
362 |
|
|
} |
363 |
|
|
} |