1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
// A set wrapper over CHM for testing |
8 |
|
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.io.*; |
12 |
|
13 |
public class ConcurrentHashSet<E> extends AbstractSet<E> |
14 |
implements Set<E>, Serializable { |
15 |
|
16 |
private final ConcurrentHashMap<E, Boolean> m; // The backing map |
17 |
private transient Set<E> keySet; // Its keySet |
18 |
|
19 |
public ConcurrentHashSet() { |
20 |
m = new ConcurrentHashMap<E, Boolean>(); |
21 |
keySet = m.keySet(); |
22 |
} |
23 |
public ConcurrentHashSet(int initialCapacity) { |
24 |
m = new ConcurrentHashMap<E, Boolean>(initialCapacity); |
25 |
keySet = m.keySet(); |
26 |
} |
27 |
public ConcurrentHashSet(int initialCapacity, float loadFactor, |
28 |
int concurrencyLevel) { |
29 |
m = new ConcurrentHashMap<E, Boolean>(initialCapacity, loadFactor, |
30 |
concurrencyLevel); |
31 |
keySet = m.keySet(); |
32 |
} |
33 |
|
34 |
public int size() { return m.size(); } |
35 |
public boolean isEmpty() { return m.isEmpty(); } |
36 |
public boolean contains(Object o) { return m.containsKey(o); } |
37 |
public Iterator<E> iterator() { return keySet.iterator(); } |
38 |
public Object[] toArray() { return keySet.toArray(); } |
39 |
public <T> T[] toArray(T[] a) { return keySet.toArray(a); } |
40 |
public boolean add(E e) { |
41 |
return m.put(e, Boolean.TRUE) == null; |
42 |
} |
43 |
public boolean remove(Object o) { return m.remove(o) != null; } |
44 |
|
45 |
public boolean removeAll(Collection<?> c) { |
46 |
return keySet.removeAll(c); |
47 |
} |
48 |
public boolean retainAll(Collection<?> c) { |
49 |
return keySet.retainAll(c); |
50 |
} |
51 |
public void clear() { m.clear(); } |
52 |
public boolean equals(Object o) { return keySet.equals(o); } |
53 |
public int hashCode() { return keySet.hashCode(); } |
54 |
|
55 |
private static final long serialVersionUID = 2454657854757543876L; |
56 |
|
57 |
private void readObject(java.io.ObjectInputStream s) |
58 |
throws IOException, ClassNotFoundException |
59 |
{ |
60 |
s.defaultReadObject(); |
61 |
keySet = m.keySet(); |
62 |
} |
63 |
} |
64 |
|