1 |
dl |
1.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 |
|
|
import java.util.*; |
7 |
|
|
import java.util.concurrent.*; |
8 |
|
|
import java.util.concurrent.locks.*; |
9 |
|
|
|
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* This is an incomplete implementation of a wrapper class |
13 |
|
|
* that places read-write locks around unsynchronized Collections. |
14 |
|
|
* Exists as a sample input for CollectionLoops test. |
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
public final class RWCollection<E> implements Collection<E> { |
18 |
|
|
private final Collection c; |
19 |
|
|
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); |
20 |
|
|
|
21 |
|
|
public RWCollection(Collection<E> c) { |
22 |
|
|
if (c == null) |
23 |
|
|
throw new NullPointerException(); |
24 |
|
|
this.c = c; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
public RWCollection() { |
28 |
|
|
this(new ArrayList<E>()); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
public final int size() { |
32 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
33 |
|
|
l.lock(); try {return c.size();} finally { l.unlock(); } |
34 |
|
|
} |
35 |
|
|
public final boolean isEmpty(){ |
36 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
37 |
|
|
l.lock(); try {return c.isEmpty();} finally { l.unlock(); } |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
public final boolean contains(Object o) { |
41 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
42 |
|
|
l.lock(); try {return c.contains(o);} finally { l.unlock(); } |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
public final boolean equals(Object o) { |
46 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
47 |
|
|
l.lock(); try {return c.equals(o);} finally { l.unlock(); } |
48 |
|
|
} |
49 |
|
|
public final int hashCode() { |
50 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
51 |
|
|
l.lock(); try {return c.hashCode();} finally { l.unlock(); } |
52 |
|
|
} |
53 |
|
|
public final String toString() { |
54 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
55 |
|
|
l.lock(); try {return c.toString();} finally { l.unlock(); } |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
public final Iterator<E> iterator() { |
59 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
60 |
|
|
l.lock(); try {return c.iterator();} finally { l.unlock(); } |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
public final Object[] toArray() { |
64 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
65 |
|
|
l.lock(); try {return c.toArray();} finally { l.unlock(); } |
66 |
|
|
} |
67 |
|
|
public final <T> T[] toArray(T[] a) { |
68 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
69 |
|
|
l.lock(); try {return (T[])c.toArray(a);} finally { l.unlock(); } |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
public final boolean add(E e) { |
73 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
74 |
|
|
l.lock(); try {return c.add(e);} finally { l.unlock(); } |
75 |
|
|
} |
76 |
|
|
public final boolean remove(Object o) { |
77 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
78 |
|
|
l.lock(); try {return c.remove(o);} finally { l.unlock(); } |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
public final boolean containsAll(Collection<?> coll) { |
82 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
83 |
|
|
l.lock(); try {return c.containsAll(coll);} finally { l.unlock(); } |
84 |
|
|
} |
85 |
|
|
public final boolean addAll(Collection<? extends E> coll) { |
86 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
87 |
|
|
l.lock(); try {return c.addAll(coll);} finally { l.unlock(); } |
88 |
|
|
} |
89 |
|
|
public final boolean removeAll(Collection<?> coll) { |
90 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
91 |
|
|
l.lock(); try {return c.removeAll(coll);} finally { l.unlock(); } |
92 |
|
|
} |
93 |
|
|
public final boolean retainAll(Collection<?> coll) { |
94 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
95 |
|
|
l.lock(); try {return c.retainAll(coll);} finally { l.unlock(); } |
96 |
|
|
} |
97 |
|
|
public final void clear() { |
98 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
99 |
|
|
l.lock(); try {c.clear();} finally { l.unlock(); } |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
} |