1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /*
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
29 * file:
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/
34 */
35
36 package java.util.concurrent;
37
38 import java.util.AbstractSet;
39 import java.util.Collection;
40 import java.util.Iterator;
41 import java.util.Objects;
42 import java.util.Set;
43 import java.util.Spliterator;
44 import java.util.Spliterators;
45 import java.util.function.Consumer;
46 import java.util.function.Predicate;
47
48 /**
49 * A {@link Set} that uses an internal {@link CopyOnWriteArrayList}
50 * for all of its operations. Thus, it shares the same basic properties:
51 * <ul>
52 * <li>It is best suited for applications in which set sizes generally
53 * stay small, read-only operations
54 * vastly outnumber mutative operations, and you need
55 * to prevent interference among threads during traversal.
56 * <li>It is thread-safe.
57 * <li>Mutative operations ({@code add}, {@code set}, {@code remove}, etc.)
58 * are expensive since they usually entail copying the entire underlying
59 * array.
60 * <li>Iterators do not support the mutative {@code remove} operation.
61 * <li>Traversal via iterators is fast and cannot encounter
62 * interference from other threads. Iterators rely on
63 * unchanging snapshots of the array at the time the iterators were
64 * constructed.
65 * </ul>
66 *
67 * <p><b>Sample Usage.</b> The following code sketch uses a
68 * copy-on-write set to maintain a set of Handler objects that
69 * perform some action upon state updates.
70 *
71 * <pre> {@code
72 * class Handler { void handle(); ... }
73 *
74 * class X {
75 * private final CopyOnWriteArraySet<Handler> handlers
76 * = new CopyOnWriteArraySet<>();
77 * public void addHandler(Handler h) { handlers.add(h); }
78 *
79 * private long internalState;
80 * private synchronized void changeState() { internalState = ...; }
81 *
82 * public void update() {
83 * changeState();
84 * for (Handler handler : handlers)
85 * handler.handle();
86 * }
87 * }}</pre>
88 *
89 * <p>This class is a member of the
90 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
91 * Java Collections Framework</a>.
92 *
93 * @see CopyOnWriteArrayList
94 * @since 1.5
95 * @author Doug Lea
96 * @param <E> the type of elements held in this set
97 */
98 public class CopyOnWriteArraySet<E> extends AbstractSet<E>
99 implements java.io.Serializable {
100 private static final long serialVersionUID = 5457747651344034263L;
101
102 private final CopyOnWriteArrayList<E> al;
103
104 /**
105 * Creates an empty set.
106 */
107 public CopyOnWriteArraySet() {
108 al = new CopyOnWriteArrayList<E>();
109 }
110
111 /**
112 * Creates a set containing all of the elements of the specified
113 * collection.
114 *
115 * @param c the collection of elements to initially contain
116 * @throws NullPointerException if the specified collection is null
117 */
118 public CopyOnWriteArraySet(Collection<? extends E> c) {
119 if (c.getClass() == CopyOnWriteArraySet.class) {
120 @SuppressWarnings("unchecked") CopyOnWriteArraySet<E> cc =
121 (CopyOnWriteArraySet<E>)c;
122 al = new CopyOnWriteArrayList<E>(cc.al);
123 }
124 else {
125 al = new CopyOnWriteArrayList<E>();
126 al.addAllAbsent(c);
127 }
128 }
129
130 /**
131 * Returns the number of elements in this set.
132 *
133 * @return the number of elements in this set
134 */
135 public int size() {
136 return al.size();
137 }
138
139 /**
140 * Returns {@code true} if this set contains no elements.
141 *
142 * @return {@code true} if this set contains no elements
143 */
144 public boolean isEmpty() {
145 return al.isEmpty();
146 }
147
148 /**
149 * Returns {@code true} if this set contains the specified element.
150 * More formally, returns {@code true} if and only if this set
151 * contains an element {@code e} such that {@code Objects.equals(o, e)}.
152 *
153 * @param o element whose presence in this set is to be tested
154 * @return {@code true} if this set contains the specified element
155 */
156 public boolean contains(Object o) {
157 return al.contains(o);
158 }
159
160 /**
161 * Returns an array containing all of the elements in this set.
162 * If this set makes any guarantees as to what order its elements
163 * are returned by its iterator, this method must return the
164 * elements in the same order.
165 *
166 * <p>The returned array will be "safe" in that no references to it
167 * are maintained by this set. (In other words, this method must
168 * allocate a new array even if this set is backed by an array).
169 * The caller is thus free to modify the returned array.
170 *
171 * <p>This method acts as bridge between array-based and collection-based
172 * APIs.
173 *
174 * @return an array containing all the elements in this set
175 */
176 public Object[] toArray() {
177 return al.toArray();
178 }
179
180 /**
181 * Returns an array containing all of the elements in this set; the
182 * runtime type of the returned array is that of the specified array.
183 * If the set fits in the specified array, it is returned therein.
184 * Otherwise, a new array is allocated with the runtime type of the
185 * specified array and the size of this set.
186 *
187 * <p>If this set fits in the specified array with room to spare
188 * (i.e., the array has more elements than this set), the element in
189 * the array immediately following the end of the set is set to
190 * {@code null}. (This is useful in determining the length of this
191 * set <i>only</i> if the caller knows that this set does not contain
192 * any null elements.)
193 *
194 * <p>If this set makes any guarantees as to what order its elements
195 * are returned by its iterator, this method must return the elements
196 * in the same order.
197 *
198 * <p>Like the {@link #toArray()} method, this method acts as bridge between
199 * array-based and collection-based APIs. Further, this method allows
200 * precise control over the runtime type of the output array, and may,
201 * under certain circumstances, be used to save allocation costs.
202 *
203 * <p>Suppose {@code x} is a set known to contain only strings.
204 * The following code can be used to dump the set into a newly allocated
205 * array of {@code String}:
206 *
207 * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
208 *
209 * Note that {@code toArray(new Object[0])} is identical in function to
210 * {@code toArray()}.
211 *
212 * @param a the array into which the elements of this set are to be
213 * stored, if it is big enough; otherwise, a new array of the same
214 * runtime type is allocated for this purpose.
215 * @return an array containing all the elements in this set
216 * @throws ArrayStoreException if the runtime type of the specified array
217 * is not a supertype of the runtime type of every element in this
218 * set
219 * @throws NullPointerException if the specified array is null
220 */
221 public <T> T[] toArray(T[] a) {
222 return al.toArray(a);
223 }
224
225 /**
226 * Removes all of the elements from this set.
227 * The set will be empty after this call returns.
228 */
229 public void clear() {
230 al.clear();
231 }
232
233 /**
234 * Removes the specified element from this set if it is present.
235 * More formally, removes an element {@code e} such that
236 * {@code Objects.equals(o, e)}, if this set contains such an element.
237 * Returns {@code true} if this set contained the element (or
238 * equivalently, if this set changed as a result of the call).
239 * (This set will not contain the element once the call returns.)
240 *
241 * @param o object to be removed from this set, if present
242 * @return {@code true} if this set contained the specified element
243 */
244 public boolean remove(Object o) {
245 return al.remove(o);
246 }
247
248 /**
249 * Adds the specified element to this set if it is not already present.
250 * More formally, adds the specified element {@code e} to this set if
251 * the set contains no element {@code e2} such that
252 * {@code Objects.equals(e, e2)}.
253 * If this set already contains the element, the call leaves the set
254 * unchanged and returns {@code false}.
255 *
256 * @param e element to be added to this set
257 * @return {@code true} if this set did not already contain the specified
258 * element
259 */
260 public boolean add(E e) {
261 return al.addIfAbsent(e);
262 }
263
264 /**
265 * Returns {@code true} if this set contains all of the elements of the
266 * specified collection. If the specified collection is also a set, this
267 * method returns {@code true} if it is a <i>subset</i> of this set.
268 *
269 * @param c collection to be checked for containment in this set
270 * @return {@code true} if this set contains all of the elements of the
271 * specified collection
272 * @throws NullPointerException if the specified collection is null
273 * @see #contains(Object)
274 */
275 public boolean containsAll(Collection<?> c) {
276 return (c instanceof Set)
277 ? compareSets(al.getArray(), (Set<?>) c) >= 0
278 : al.containsAll(c);
279 }
280
281 /**
282 * Tells whether the objects in snapshot (regarded as a set) are a
283 * superset of the given set.
284 *
285 * @return -1 if snapshot is not a superset, 0 if the two sets
286 * contain precisely the same elements, and 1 if snapshot is a
287 * proper superset of the given set
288 */
289 private static int compareSets(Object[] snapshot, Set<?> set) {
290 // Uses O(n^2) algorithm, that is only appropriate for small
291 // sets, which CopyOnWriteArraySets should be.
292 //
293 // Optimize up to O(n) if the two sets share a long common prefix,
294 // as might happen if one set was created as a copy of the other set.
295
296 final int len = snapshot.length;
297 // Mark matched elements to avoid re-checking
298 final boolean[] matched = new boolean[len];
299
300 // j is the largest int with matched[i] true for { i | 0 <= i < j }
301 int j = 0;
302 outer: for (Object x : set) {
303 for (int i = j; i < len; i++) {
304 if (!matched[i] && Objects.equals(x, snapshot[i])) {
305 matched[i] = true;
306 if (i == j)
307 do { j++; } while (j < len && matched[j]);
308 continue outer;
309 }
310 }
311 return -1;
312 }
313 return (j == len) ? 0 : 1;
314 }
315
316 /**
317 * Adds all of the elements in the specified collection to this set if
318 * they're not already present. If the specified collection is also a
319 * set, the {@code addAll} operation effectively modifies this set so
320 * that its value is the <i>union</i> of the two sets. The behavior of
321 * this operation is undefined if the specified collection is modified
322 * while the operation is in progress.
323 *
324 * @param c collection containing elements to be added to this set
325 * @return {@code true} if this set changed as a result of the call
326 * @throws NullPointerException if the specified collection is null
327 * @see #add(Object)
328 */
329 public boolean addAll(Collection<? extends E> c) {
330 return al.addAllAbsent(c) > 0;
331 }
332
333 /**
334 * Removes from this set all of its elements that are contained in the
335 * specified collection. If the specified collection is also a set,
336 * this operation effectively modifies this set so that its value is the
337 * <i>asymmetric set difference</i> of the two sets.
338 *
339 * @param c collection containing elements to be removed from this set
340 * @return {@code true} if this set changed as a result of the call
341 * @throws ClassCastException if the class of an element of this set
342 * is incompatible with the specified collection
343 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
344 * @throws NullPointerException if this set contains a null element and the
345 * specified collection does not permit null elements
346 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
347 * or if the specified collection is null
348 * @see #remove(Object)
349 */
350 public boolean removeAll(Collection<?> c) {
351 return al.removeAll(c);
352 }
353
354 /**
355 * Retains only the elements in this set that are contained in the
356 * specified collection. In other words, removes from this set all of
357 * its elements that are not contained in the specified collection. If
358 * the specified collection is also a set, this operation effectively
359 * modifies this set so that its value is the <i>intersection</i> of the
360 * two sets.
361 *
362 * @param c collection containing elements to be retained in this set
363 * @return {@code true} if this set changed as a result of the call
364 * @throws ClassCastException if the class of an element of this set
365 * is incompatible with the specified collection
366 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
367 * @throws NullPointerException if this set contains a null element and the
368 * specified collection does not permit null elements
369 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>),
370 * or if the specified collection is null
371 * @see #remove(Object)
372 */
373 public boolean retainAll(Collection<?> c) {
374 return al.retainAll(c);
375 }
376
377 /**
378 * Returns an iterator over the elements contained in this set
379 * in the order in which these elements were added.
380 *
381 * <p>The returned iterator provides a snapshot of the state of the set
382 * when the iterator was constructed. No synchronization is needed while
383 * traversing the iterator. The iterator does <em>NOT</em> support the
384 * {@code remove} method.
385 *
386 * @return an iterator over the elements in this set
387 */
388 public Iterator<E> iterator() {
389 return al.iterator();
390 }
391
392 /**
393 * Compares the specified object with this set for equality.
394 * Returns {@code true} if the specified object is the same object
395 * as this object, or if it is also a {@link Set} and the elements
396 * returned by an {@linkplain Set#iterator() iterator} over the
397 * specified set are the same as the elements returned by an
398 * iterator over this set. More formally, the two iterators are
399 * considered to return the same elements if they return the same
400 * number of elements and for every element {@code e1} returned by
401 * the iterator over the specified set, there is an element
402 * {@code e2} returned by the iterator over this set such that
403 * {@code Objects.equals(e1, e2)}.
404 *
405 * @param o object to be compared for equality with this set
406 * @return {@code true} if the specified object is equal to this set
407 */
408 public boolean equals(Object o) {
409 return (o == this)
410 || ((o instanceof Set)
411 && compareSets(al.getArray(), (Set<?>) o) == 0);
412 }
413
414 /**
415 * @throws NullPointerException {@inheritDoc}
416 */
417 public boolean removeIf(Predicate<? super E> filter) {
418 return al.removeIf(filter);
419 }
420
421 /**
422 * @throws NullPointerException {@inheritDoc}
423 */
424 public void forEach(Consumer<? super E> action) {
425 al.forEach(action);
426 }
427
428 /**
429 * Returns a {@link Spliterator} over the elements in this set in the order
430 * in which these elements were added.
431 *
432 * <p>The {@code Spliterator} reports {@link Spliterator#IMMUTABLE},
433 * {@link Spliterator#DISTINCT}, {@link Spliterator#SIZED}, and
434 * {@link Spliterator#SUBSIZED}.
435 *
436 * <p>The spliterator provides a snapshot of the state of the set
437 * when the spliterator was constructed. No synchronization is needed while
438 * operating on the spliterator.
439 *
440 * @return a {@code Spliterator} over the elements in this set
441 * @since 1.8
442 */
443 public Spliterator<E> spliterator() {
444 return Spliterators.spliterator
445 (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT);
446 }
447 }
448