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 |
|
|
public final class SCollection<E> implements Collection<E> { |
17 |
|
|
private final Collection c; |
18 |
|
|
private final ReentrantLock l = new ReentrantLock(); |
19 |
jsr166 |
1.2 |
|
20 |
dl |
1.1 |
public SCollection(Collection<E> c) { |
21 |
|
|
if (c == null) |
22 |
|
|
throw new NullPointerException(); |
23 |
|
|
this.c = c; |
24 |
|
|
} |
25 |
|
|
|
26 |
jsr166 |
1.2 |
public SCollection() { |
27 |
|
|
this(new ArrayList<E>()); |
28 |
dl |
1.1 |
} |
29 |
|
|
|
30 |
|
|
public final int size() { |
31 |
jsr166 |
1.3 |
l.lock(); |
32 |
|
|
try { return c.size(); } |
33 |
|
|
finally { l.unlock(); } |
34 |
dl |
1.1 |
} |
35 |
jsr166 |
1.3 |
|
36 |
|
|
public final boolean isEmpty() { |
37 |
|
|
l.lock(); |
38 |
|
|
try { return c.isEmpty(); } |
39 |
|
|
finally { l.unlock(); } |
40 |
dl |
1.1 |
} |
41 |
|
|
|
42 |
|
|
public final boolean contains(Object o) { |
43 |
jsr166 |
1.3 |
l.lock(); |
44 |
|
|
try { return c.contains(o); } |
45 |
|
|
finally { l.unlock(); } |
46 |
dl |
1.1 |
} |
47 |
|
|
|
48 |
|
|
public final boolean equals(Object o) { |
49 |
jsr166 |
1.3 |
l.lock(); |
50 |
|
|
try { return c.equals(o); } |
51 |
|
|
finally { l.unlock(); } |
52 |
dl |
1.1 |
} |
53 |
jsr166 |
1.3 |
|
54 |
dl |
1.1 |
public final int hashCode() { |
55 |
jsr166 |
1.3 |
l.lock(); |
56 |
|
|
try { return c.hashCode(); } |
57 |
|
|
finally { l.unlock(); } |
58 |
dl |
1.1 |
} |
59 |
jsr166 |
1.3 |
|
60 |
dl |
1.1 |
public final String toString() { |
61 |
jsr166 |
1.3 |
l.lock(); |
62 |
|
|
try { return c.toString(); } |
63 |
|
|
finally { l.unlock(); } |
64 |
dl |
1.1 |
} |
65 |
|
|
|
66 |
|
|
public final Iterator<E> iterator() { |
67 |
jsr166 |
1.3 |
l.lock(); |
68 |
|
|
try { return c.iterator(); } |
69 |
|
|
finally { l.unlock(); } |
70 |
dl |
1.1 |
} |
71 |
|
|
|
72 |
|
|
public final Object[] toArray() { |
73 |
jsr166 |
1.3 |
l.lock(); |
74 |
|
|
try { return c.toArray(); } |
75 |
|
|
finally { l.unlock(); } |
76 |
dl |
1.1 |
} |
77 |
jsr166 |
1.3 |
|
78 |
dl |
1.1 |
public final <T> T[] toArray(T[] a) { |
79 |
jsr166 |
1.3 |
l.lock(); |
80 |
|
|
try { return (T[])c.toArray(a); } |
81 |
|
|
finally { l.unlock(); } |
82 |
dl |
1.1 |
} |
83 |
|
|
|
84 |
|
|
public final boolean add(E e) { |
85 |
jsr166 |
1.3 |
l.lock(); |
86 |
|
|
try { return c.add(e); } |
87 |
|
|
finally { l.unlock(); } |
88 |
dl |
1.1 |
} |
89 |
jsr166 |
1.3 |
|
90 |
dl |
1.1 |
public final boolean remove(Object o) { |
91 |
jsr166 |
1.3 |
l.lock(); |
92 |
|
|
try { return c.remove(o); } |
93 |
|
|
finally { l.unlock(); } |
94 |
dl |
1.1 |
} |
95 |
|
|
|
96 |
|
|
public final boolean containsAll(Collection<?> coll) { |
97 |
jsr166 |
1.3 |
l.lock(); |
98 |
|
|
try { return c.containsAll(coll); } |
99 |
|
|
finally { l.unlock(); } |
100 |
dl |
1.1 |
} |
101 |
jsr166 |
1.3 |
|
102 |
dl |
1.1 |
public final boolean addAll(Collection<? extends E> coll) { |
103 |
jsr166 |
1.3 |
l.lock(); |
104 |
|
|
try { return c.addAll(coll); } |
105 |
|
|
finally { l.unlock(); } |
106 |
dl |
1.1 |
} |
107 |
jsr166 |
1.3 |
|
108 |
dl |
1.1 |
public final boolean removeAll(Collection<?> coll) { |
109 |
jsr166 |
1.3 |
l.lock(); |
110 |
|
|
try { return c.removeAll(coll); } |
111 |
|
|
finally { l.unlock(); } |
112 |
dl |
1.1 |
} |
113 |
jsr166 |
1.3 |
|
114 |
dl |
1.1 |
public final boolean retainAll(Collection<?> coll) { |
115 |
jsr166 |
1.3 |
l.lock(); |
116 |
|
|
try { return c.retainAll(coll); } |
117 |
|
|
finally { l.unlock(); } |
118 |
dl |
1.1 |
} |
119 |
jsr166 |
1.3 |
|
120 |
dl |
1.1 |
public final void clear() { |
121 |
jsr166 |
1.3 |
l.lock(); |
122 |
|
|
try { c.clear(); } |
123 |
|
|
finally { l.unlock(); } |
124 |
dl |
1.1 |
} |
125 |
|
|
|
126 |
|
|
} |