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 |
jsr166 |
1.4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.2 |
|
21 |
dl |
1.1 |
public RWCollection(Collection<E> c) { |
22 |
|
|
if (c == null) |
23 |
|
|
throw new NullPointerException(); |
24 |
|
|
this.c = c; |
25 |
|
|
} |
26 |
|
|
|
27 |
jsr166 |
1.2 |
public RWCollection() { |
28 |
|
|
this(new ArrayList<E>()); |
29 |
dl |
1.1 |
} |
30 |
|
|
|
31 |
|
|
public final int size() { |
32 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
33 |
jsr166 |
1.3 |
l.lock(); |
34 |
|
|
try { return c.size(); } |
35 |
|
|
finally { l.unlock(); } |
36 |
dl |
1.1 |
} |
37 |
jsr166 |
1.3 |
|
38 |
|
|
public final boolean isEmpty() { |
39 |
dl |
1.1 |
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
40 |
jsr166 |
1.3 |
l.lock(); |
41 |
|
|
try { return c.isEmpty(); } |
42 |
|
|
finally { l.unlock(); } |
43 |
dl |
1.1 |
} |
44 |
|
|
|
45 |
|
|
public final boolean contains(Object o) { |
46 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
47 |
jsr166 |
1.3 |
l.lock(); |
48 |
|
|
try { return c.contains(o); } |
49 |
|
|
finally { l.unlock(); } |
50 |
dl |
1.1 |
} |
51 |
|
|
|
52 |
|
|
public final boolean equals(Object o) { |
53 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
54 |
jsr166 |
1.3 |
l.lock(); |
55 |
|
|
try { return c.equals(o); } |
56 |
|
|
finally { l.unlock(); } |
57 |
dl |
1.1 |
} |
58 |
jsr166 |
1.3 |
|
59 |
dl |
1.1 |
public final int hashCode() { |
60 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
61 |
jsr166 |
1.3 |
l.lock(); |
62 |
|
|
try { return c.hashCode(); } |
63 |
|
|
finally { l.unlock(); } |
64 |
dl |
1.1 |
} |
65 |
jsr166 |
1.3 |
|
66 |
dl |
1.1 |
public final String toString() { |
67 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
68 |
jsr166 |
1.3 |
l.lock(); |
69 |
|
|
try { return c.toString(); } |
70 |
|
|
finally { l.unlock(); } |
71 |
dl |
1.1 |
} |
72 |
|
|
|
73 |
|
|
public final Iterator<E> iterator() { |
74 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
75 |
jsr166 |
1.3 |
l.lock(); |
76 |
|
|
try { return c.iterator(); } |
77 |
|
|
finally { l.unlock(); } |
78 |
dl |
1.1 |
} |
79 |
|
|
|
80 |
|
|
public final Object[] toArray() { |
81 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
82 |
jsr166 |
1.3 |
l.lock(); |
83 |
|
|
try { return c.toArray(); } |
84 |
|
|
finally { l.unlock(); } |
85 |
dl |
1.1 |
} |
86 |
jsr166 |
1.3 |
|
87 |
dl |
1.1 |
public final <T> T[] toArray(T[] a) { |
88 |
|
|
final ReentrantReadWriteLock.ReadLock l = rwl.readLock(); |
89 |
jsr166 |
1.3 |
l.lock(); |
90 |
|
|
try { return (T[])c.toArray(a); } |
91 |
|
|
finally { l.unlock(); } |
92 |
dl |
1.1 |
} |
93 |
|
|
|
94 |
|
|
public final boolean add(E e) { |
95 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
96 |
jsr166 |
1.3 |
l.lock(); |
97 |
|
|
try { return c.add(e); } |
98 |
|
|
finally { l.unlock(); } |
99 |
dl |
1.1 |
} |
100 |
jsr166 |
1.3 |
|
101 |
dl |
1.1 |
public final boolean remove(Object o) { |
102 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
103 |
jsr166 |
1.3 |
l.lock(); |
104 |
|
|
try { return c.remove(o); } |
105 |
|
|
finally { l.unlock(); } |
106 |
dl |
1.1 |
} |
107 |
|
|
|
108 |
|
|
public final boolean containsAll(Collection<?> coll) { |
109 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
110 |
jsr166 |
1.3 |
l.lock(); |
111 |
|
|
try { return c.containsAll(coll); } |
112 |
|
|
finally { l.unlock(); } |
113 |
dl |
1.1 |
} |
114 |
jsr166 |
1.3 |
|
115 |
dl |
1.1 |
public final boolean addAll(Collection<? extends E> coll) { |
116 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
117 |
jsr166 |
1.3 |
l.lock(); |
118 |
|
|
try { return c.addAll(coll); } |
119 |
|
|
finally { l.unlock(); } |
120 |
dl |
1.1 |
} |
121 |
jsr166 |
1.3 |
|
122 |
dl |
1.1 |
public final boolean removeAll(Collection<?> coll) { |
123 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
124 |
jsr166 |
1.3 |
l.lock(); |
125 |
|
|
try { return c.removeAll(coll); } |
126 |
|
|
finally { l.unlock(); } |
127 |
dl |
1.1 |
} |
128 |
jsr166 |
1.3 |
|
129 |
dl |
1.1 |
public final boolean retainAll(Collection<?> coll) { |
130 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
131 |
jsr166 |
1.3 |
l.lock(); |
132 |
|
|
try { return c.retainAll(coll); } |
133 |
|
|
finally { l.unlock(); } |
134 |
dl |
1.1 |
} |
135 |
jsr166 |
1.3 |
|
136 |
dl |
1.1 |
public final void clear() { |
137 |
|
|
final ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
138 |
jsr166 |
1.3 |
l.lock(); |
139 |
|
|
try { c.clear(); } |
140 |
|
|
finally { l.unlock(); } |
141 |
dl |
1.1 |
} |
142 |
|
|
|
143 |
|
|
} |