1 |
dl |
1.2 |
/* |
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 |
tim |
1.1 |
package java.util.concurrent; |
8 |
|
|
import java.util.*; |
9 |
|
|
|
10 |
|
|
/** |
11 |
dl |
1.3 |
* 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 |
tim |
1.1 |
* <ul> |
15 |
dholmes |
1.7 |
* <li>It is best suited for applications in which set sizes generally |
16 |
tim |
1.1 |
* stay small, read-only operations |
17 |
|
|
* vastly outnumber mutative operations, and you need |
18 |
|
|
* to prevent interference among threads during traversal. |
19 |
dl |
1.16 |
* <li>It is thread-safe. |
20 |
dl |
1.18 |
* <li>Mutative operations (add, set, remove, etc) are expensive |
21 |
tim |
1.1 |
* since they usually entail copying the entire underlying array. |
22 |
dl |
1.18 |
* <li>Iterators do not support the mutative remove operation. |
23 |
|
|
* <li>Traversal via iterators is fast and cannot encounter |
24 |
tim |
1.1 |
* interference from other threads. Iterators rely on |
25 |
|
|
* unchanging snapshots of the array at the time the iterators were |
26 |
|
|
* constructed. |
27 |
|
|
* </ul> |
28 |
dl |
1.18 |
* |
29 |
|
|
* <p> <b>Sample Usage.</b> The following code sketch uses a |
30 |
jsr166 |
1.21 |
* copy-on-write set to maintain a set of Handler objects that |
31 |
|
|
* perform some action upon state updates. |
32 |
dl |
1.18 |
* |
33 |
tim |
1.1 |
* <pre> |
34 |
tim |
1.6 |
* class Handler { void handle(); ... } |
35 |
tim |
1.1 |
* |
36 |
|
|
* class X { |
37 |
dl |
1.13 |
* private final CopyOnWriteArraySet<Handler> handlers = new CopyOnWriteArraySet<Handler>(); |
38 |
tim |
1.1 |
* public void addHandler(Handler h) { handlers.add(h); } |
39 |
|
|
* |
40 |
|
|
* private long internalState; |
41 |
|
|
* private synchronized void changeState() { internalState = ...; } |
42 |
|
|
* |
43 |
|
|
* public void update() { |
44 |
|
|
* changeState(); |
45 |
dl |
1.18 |
* for (Handler handler : handlers) |
46 |
dl |
1.19 |
* handler.handle(); |
47 |
tim |
1.1 |
* } |
48 |
|
|
* } |
49 |
tim |
1.6 |
* </pre> |
50 |
dl |
1.14 |
* |
51 |
|
|
* <p>This class is a member of the |
52 |
|
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
53 |
|
|
* Java Collections Framework</a>. |
54 |
|
|
* |
55 |
dl |
1.16 |
* @see CopyOnWriteArrayList |
56 |
dl |
1.4 |
* @since 1.5 |
57 |
|
|
* @author Doug Lea |
58 |
dl |
1.11 |
* @param <E> the type of elements held in this collection |
59 |
dl |
1.4 |
*/ |
60 |
tim |
1.1 |
public class CopyOnWriteArraySet<E> extends AbstractSet<E> |
61 |
dl |
1.18 |
implements java.io.Serializable { |
62 |
dl |
1.8 |
private static final long serialVersionUID = 5457747651344034263L; |
63 |
tim |
1.1 |
|
64 |
|
|
private final CopyOnWriteArrayList<E> al; |
65 |
|
|
|
66 |
|
|
/** |
67 |
dl |
1.15 |
* Creates an empty set. |
68 |
tim |
1.1 |
*/ |
69 |
|
|
public CopyOnWriteArraySet() { |
70 |
|
|
al = new CopyOnWriteArrayList<E>(); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
dl |
1.15 |
* Creates a set containing all of the elements of the specified |
75 |
jsr166 |
1.23 |
* collection. |
76 |
jsr166 |
1.25 |
* |
77 |
jsr166 |
1.23 |
* @param c the collection of elements to initially contain |
78 |
|
|
* @throws NullPointerException if the specified collection is null |
79 |
tim |
1.1 |
*/ |
80 |
tim |
1.12 |
public CopyOnWriteArraySet(Collection<? extends E> c) { |
81 |
tim |
1.1 |
al = new CopyOnWriteArrayList<E>(); |
82 |
|
|
al.addAllAbsent(c); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
public int size() { return al.size(); } |
87 |
|
|
public boolean isEmpty() { return al.isEmpty(); } |
88 |
|
|
public boolean contains(Object o) { return al.contains(o); } |
89 |
|
|
public Object[] toArray() { return al.toArray(); } |
90 |
|
|
public <T> T[] toArray(T[] a) { return al.toArray(a); } |
91 |
|
|
public void clear() { al.clear(); } |
92 |
|
|
public boolean remove(Object o) { return al.remove(o); } |
93 |
jsr166 |
1.22 |
public boolean add(E e) { return al.addIfAbsent(e); } |
94 |
tim |
1.5 |
public boolean containsAll(Collection<?> c) { return al.containsAll(c); } |
95 |
|
|
public boolean addAll(Collection<? extends E> c) { return al.addAllAbsent(c) > 0; } |
96 |
|
|
public boolean removeAll(Collection<?> c) { return al.removeAll(c); } |
97 |
|
|
public boolean retainAll(Collection<?> c) { return al.retainAll(c); } |
98 |
tim |
1.1 |
|
99 |
dl |
1.20 |
/** |
100 |
jsr166 |
1.24 |
* Returns an iterator over the elements contained in this set |
101 |
dl |
1.20 |
* in the order in which these elements were added. |
102 |
|
|
*/ |
103 |
|
|
public Iterator<E> iterator() { return al.iterator(); } |
104 |
|
|
|
105 |
tim |
1.1 |
} |