KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > CopyOnWriteArraySet


1 /*
2  * @(#)CopyOnWriteArraySet.java 1.7 04/06/11
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.concurrent;
9 import java.util.*;
10
11 /**
12  * A {@link java.util.Set} that uses {@link
13  * java.util.concurrent.CopyOnWriteArrayList} for all of its
14  * operations. Thus, it shares the same basic properties:
15  * <ul>
16  * <li>It is best suited for applications in which set sizes generally
17  * stay small, read-only operations
18  * vastly outnumber mutative operations, and you need
19  * to prevent interference among threads during traversal.
20  * <li>It is thread-safe.
21  * <li>Mutative operations(add, set, remove, etc) are expensive
22  * since they usually entail copying the entire underlying array.
23  * <li>Iterators do not support the mutative remove operation.
24  * <li>Traversal via iterators is fast and cannot 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  *
30  * <p> <b>Sample Usage.</b> The following code sketch uses a
31  * copy-on-write set to maintain a set of Handler objects that
32  * perform some action upon state updates.
33  *
34  * <pre>
35  * class Handler { void handle(); ... }
36  *
37  * class X {
38  * private final CopyOnWriteArraySet&lt;Handler&gt; handlers = new CopyOnWriteArraySet&lt;Handler&gt;();
39  * public void addHandler(Handler h) { handlers.add(h); }
40  *
41  * private long internalState;
42  * private synchronized void changeState() { internalState = ...; }
43  *
44  * public void update() {
45  * changeState();
46  * for (Handler handler : handlers)
47  * handler.handle();
48  * }
49  * }
50  * </pre>
51  *
52  * <p>This class is a member of the
53  * <a HREF="{@docRoot}/../guide/collections/index.html">
54  * Java Collections Framework</a>.
55  *
56  * @see CopyOnWriteArrayList
57  * @since 1.5
58  * @author Doug Lea
59  * @param <E> the type of elements held in this collection
60  */

61 public class CopyOnWriteArraySet<E> extends AbstractSet<E>
62         implements java.io.Serializable JavaDoc {
63     private static final long serialVersionUID = 5457747651344034263L;
64
65     private final CopyOnWriteArrayList JavaDoc<E> al;
66
67     /**
68      * Creates an empty set.
69      */

70     public CopyOnWriteArraySet() {
71         al = new CopyOnWriteArrayList JavaDoc<E>();
72     }
73
74     /**
75      * Creates a set containing all of the elements of the specified
76      * Collection.
77      * @param c the collection
78      */

79     public CopyOnWriteArraySet(Collection<? extends E> c) {
80         al = new CopyOnWriteArrayList JavaDoc<E>();
81         al.addAllAbsent(c);
82     }
83
84
85     public int size() { return al.size(); }
86     public boolean isEmpty() { return al.isEmpty(); }
87     public boolean contains(Object JavaDoc o) { return al.contains(o); }
88     public Object JavaDoc[] toArray() { return al.toArray(); }
89     public <T> T[] toArray(T[] a) { return al.toArray(a); }
90     public void clear() { al.clear(); }
91     public Iterator<E> iterator() { return al.iterator(); }
92     public boolean remove(Object JavaDoc o) { return al.remove(o); }
93     public boolean add(E o) { return al.addIfAbsent(o); }
94     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
99 }
100
Popular Tags