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 |
|
|
* <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> Mutative operations(add, set, remove, etc) are expensive |
20 |
|
|
* since they usually entail copying the entire underlying array. |
21 |
|
|
* <li> Loops involving repeated element-by-element mutative operations |
22 |
|
|
* are so expensive that they should generally be avoided. |
23 |
|
|
* <li> Iterators do not support the mutative remove operation |
24 |
|
|
* <li> Traversal via iterators is very fast and cannot ever 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 |
|
|
* <p> |
30 |
|
|
* <b>Sample Usage.</b> Probably the main application |
31 |
|
|
* of copy-on-write sets are classes that maintain |
32 |
|
|
* sets of Handler objects |
33 |
|
|
* that must be multicasted to upon an update command. This |
34 |
tim |
1.5 |
* is a classic case where you do not want to be holding a |
35 |
tim |
1.1 |
* lock while sending a message, and where traversals normally |
36 |
|
|
* vastly overwhelm additions. |
37 |
|
|
* <pre> |
38 |
|
|
* class Handler { void handle(); ... } |
39 |
|
|
* |
40 |
|
|
* class X { |
41 |
|
|
* private final CopyOnWriteArraySet handlers = new CopyOnWriteArraySet(); |
42 |
|
|
* public void addHandler(Handler h) { handlers.add(h); } |
43 |
|
|
* |
44 |
|
|
* private long internalState; |
45 |
|
|
* private synchronized void changeState() { internalState = ...; } |
46 |
|
|
* |
47 |
|
|
* public void update() { |
48 |
|
|
* changeState(); |
49 |
|
|
* Iterator it = handlers.iterator(); |
50 |
|
|
* while (it.hasNext()) |
51 |
|
|
* ((Handler)(it.next()).handle(); |
52 |
|
|
* } |
53 |
|
|
* } |
54 |
|
|
* @see CopyOnWriteArrayList |
55 |
dl |
1.4 |
* @since 1.5 |
56 |
|
|
* @author Doug Lea |
57 |
|
|
*/ |
58 |
tim |
1.1 |
public class CopyOnWriteArraySet<E> extends AbstractSet<E> |
59 |
|
|
implements Cloneable, java.io.Serializable { |
60 |
|
|
|
61 |
|
|
private final CopyOnWriteArrayList<E> al; |
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* Constructs an empty set |
65 |
|
|
*/ |
66 |
|
|
public CopyOnWriteArraySet() { |
67 |
|
|
al = new CopyOnWriteArrayList<E>(); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
|
|
* Constructs a set containing all of the elements of the specified |
72 |
|
|
* Collection. |
73 |
dl |
1.4 |
* @param c the collection |
74 |
tim |
1.1 |
*/ |
75 |
|
|
public <T extends E> CopyOnWriteArraySet(Collection<T> c) { |
76 |
|
|
al = new CopyOnWriteArrayList<E>(); |
77 |
|
|
al.addAllAbsent(c); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
public int size() { return al.size(); } |
82 |
|
|
public boolean isEmpty() { return al.isEmpty(); } |
83 |
|
|
public boolean contains(Object o) { return al.contains(o); } |
84 |
|
|
public Object[] toArray() { return al.toArray(); } |
85 |
|
|
public <T> T[] toArray(T[] a) { return al.toArray(a); } |
86 |
|
|
public void clear() { al.clear(); } |
87 |
|
|
public Iterator<E> iterator() { return al.iterator(); } |
88 |
|
|
public boolean remove(Object o) { return al.remove(o); } |
89 |
|
|
public boolean add(E o) { return al.addIfAbsent(o); } |
90 |
tim |
1.5 |
public boolean containsAll(Collection<?> c) { return al.containsAll(c); } |
91 |
|
|
public boolean addAll(Collection<? extends E> c) { return al.addAllAbsent(c) > 0; } |
92 |
|
|
public boolean removeAll(Collection<?> c) { return al.removeAll(c); } |
93 |
|
|
public boolean retainAll(Collection<?> c) { return al.retainAll(c); } |
94 |
tim |
1.1 |
|
95 |
|
|
} |