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