ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArraySet.java
Revision: 1.21
Committed: Tue Apr 26 01:17:18 2005 UTC (19 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.20: +2 -2 lines
Log Message:
doc fixes

File Contents

# User Rev Content
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&lt;Handler&gt; handlers = new CopyOnWriteArraySet&lt;Handler&gt;();
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 tim 1.1 * Collection.
76 dl 1.4 * @param c the collection
77 tim 1.1 */
78 tim 1.12 public CopyOnWriteArraySet(Collection<? extends E> c) {
79 tim 1.1 al = new CopyOnWriteArrayList<E>();
80     al.addAllAbsent(c);
81     }
82    
83    
84     public int size() { return al.size(); }
85     public boolean isEmpty() { return al.isEmpty(); }
86     public boolean contains(Object o) { return al.contains(o); }
87     public Object[] toArray() { return al.toArray(); }
88     public <T> T[] toArray(T[] a) { return al.toArray(a); }
89     public void clear() { al.clear(); }
90     public boolean remove(Object o) { return al.remove(o); }
91     public boolean add(E o) { return al.addIfAbsent(o); }
92 tim 1.5 public boolean containsAll(Collection<?> c) { return al.containsAll(c); }
93     public boolean addAll(Collection<? extends E> c) { return al.addAllAbsent(c) > 0; }
94     public boolean removeAll(Collection<?> c) { return al.removeAll(c); }
95     public boolean retainAll(Collection<?> c) { return al.retainAll(c); }
96 tim 1.1
97 dl 1.20 /**
98     * Returns an iterator over the elements contained in this collection
99     * in the order in which these elements were added.
100     */
101     public Iterator<E> iterator() { return al.iterator(); }
102    
103 tim 1.1 }