ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArraySet.java
Revision: 1.17
Committed: Fri May 21 14:43:28 2004 UTC (20 years ago) by dl
Branch: MAIN
Changes since 1.16: +1 -0 lines
Log Message:
add clone

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain. Use, modify, and
4 * redistribute this code in any way without acknowledgement.
5 */
6
7 package java.util.concurrent;
8 import java.util.*;
9
10 /**
11 * A {@link java.util.Set} that uses {@link
12 * java.util.concurrent.CopyOnWriteArrayList} for all of its
13 * 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 * <li>Mutative operations(add, set, remove, etc) are expensive
21 * since they usually entail copying the entire underlying array.
22 * <li>Iterators do not support the mutative remove operation
23 * <li>Traversal via iterators is very fast and cannot ever encounter
24 * interference from other threads. Iterators rely on
25 * unchanging snapshots of the array at the time the iterators were
26 * constructed.
27 * </ul>
28 * <p>
29 * <b>Sample Usage.</b> Probably the main application
30 * of copy-on-write sets are classes that maintain
31 * sets of Handler objects
32 * that must be multicasted to upon an update command. This
33 * is a classic case where you do not want to be holding a
34 * lock while sending a message, and where traversals normally
35 * vastly overwhelm additions.
36 * <pre>
37 * class Handler { void handle(); ... }
38 *
39 * class X {
40 * private final CopyOnWriteArraySet&lt;Handler&gt; handlers = new CopyOnWriteArraySet&lt;Handler&gt;();
41 * public void addHandler(Handler h) { handlers.add(h); }
42 *
43 * private long internalState;
44 * private synchronized void changeState() { internalState = ...; }
45 *
46 * public void update() {
47 * changeState();
48 * Iterator&lt;Handler&gt; it = handlers.iterator();
49 * while (it.hasNext())
50 * it.next().handle();
51 * }
52 * }
53 * </pre>
54 *
55 * <p>This class is a member of the
56 * <a href="{@docRoot}/../guide/collections/index.html">
57 * Java Collections Framework</a>.
58 *
59 * @see CopyOnWriteArrayList
60 * @since 1.5
61 * @author Doug Lea
62 * @param <E> the type of elements held in this collection
63 */
64 public class CopyOnWriteArraySet<E> extends AbstractSet<E>
65 implements Cloneable, java.io.Serializable {
66 private static final long serialVersionUID = 5457747651344034263L;
67
68 private final CopyOnWriteArrayList<E> al;
69
70 /**
71 * Creates an empty set.
72 */
73 public CopyOnWriteArraySet() {
74 al = new CopyOnWriteArrayList<E>();
75 }
76
77 /**
78 * Creates a set containing all of the elements of the specified
79 * Collection.
80 * @param c the collection
81 */
82 public CopyOnWriteArraySet(Collection<? extends E> c) {
83 al = new CopyOnWriteArrayList<E>();
84 al.addAllAbsent(c);
85 }
86
87
88 public int size() { return al.size(); }
89 public boolean isEmpty() { return al.isEmpty(); }
90 public boolean contains(Object o) { return al.contains(o); }
91 public Object[] toArray() { return al.toArray(); }
92 public <T> T[] toArray(T[] a) { return al.toArray(a); }
93 public void clear() { al.clear(); }
94 public Iterator<E> iterator() { return al.iterator(); }
95 public boolean remove(Object o) { return al.remove(o); }
96 public boolean add(E o) { return al.addIfAbsent(o); }
97 public boolean containsAll(Collection<?> c) { return al.containsAll(c); }
98 public boolean addAll(Collection<? extends E> c) { return al.addAllAbsent(c) > 0; }
99 public boolean removeAll(Collection<?> c) { return al.removeAll(c); }
100 public boolean retainAll(Collection<?> c) { return al.retainAll(c); }
101
102 public Object clone() { return new CopyOnWriteArraySet(al); }
103 }