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 SCollection<E> implements Collection<E> { |
18 |
private final Collection c; |
19 |
private final ReentrantLock l = new ReentrantLock(); |
20 |
|
21 |
public SCollection(Collection<E> c) { |
22 |
if (c == null) |
23 |
throw new NullPointerException(); |
24 |
this.c = c; |
25 |
} |
26 |
|
27 |
public SCollection() { |
28 |
this(new ArrayList<E>()); |
29 |
} |
30 |
|
31 |
public final int size() { |
32 |
l.lock(); try {return c.size();} finally { l.unlock(); } |
33 |
} |
34 |
public final boolean isEmpty(){ |
35 |
l.lock(); try {return c.isEmpty();} finally { l.unlock(); } |
36 |
} |
37 |
|
38 |
public final boolean contains(Object o) { |
39 |
l.lock(); try {return c.contains(o);} finally { l.unlock(); } |
40 |
} |
41 |
|
42 |
public final boolean equals(Object o) { |
43 |
l.lock(); try {return c.equals(o);} finally { l.unlock(); } |
44 |
} |
45 |
public final int hashCode() { |
46 |
l.lock(); try {return c.hashCode();} finally { l.unlock(); } |
47 |
} |
48 |
public final String toString() { |
49 |
l.lock(); try {return c.toString();} finally { l.unlock(); } |
50 |
} |
51 |
|
52 |
public final Iterator<E> iterator() { |
53 |
l.lock(); try {return c.iterator();} finally { l.unlock(); } |
54 |
} |
55 |
|
56 |
public final Object[] toArray() { |
57 |
l.lock(); try {return c.toArray();} finally { l.unlock(); } |
58 |
} |
59 |
public final <T> T[] toArray(T[] a) { |
60 |
l.lock(); try {return (T[])c.toArray(a);} finally { l.unlock(); } |
61 |
} |
62 |
|
63 |
public final boolean add(E e) { |
64 |
l.lock(); try {return c.add(e);} finally { l.unlock(); } |
65 |
} |
66 |
public final boolean remove(Object o) { |
67 |
l.lock(); try {return c.remove(o);} finally { l.unlock(); } |
68 |
} |
69 |
|
70 |
public final boolean containsAll(Collection<?> coll) { |
71 |
l.lock(); try {return c.containsAll(coll);} finally { l.unlock(); } |
72 |
} |
73 |
public final boolean addAll(Collection<? extends E> coll) { |
74 |
l.lock(); try {return c.addAll(coll);} finally { l.unlock(); } |
75 |
} |
76 |
public final boolean removeAll(Collection<?> coll) { |
77 |
l.lock(); try {return c.removeAll(coll);} finally { l.unlock(); } |
78 |
} |
79 |
public final boolean retainAll(Collection<?> coll) { |
80 |
l.lock(); try {return c.retainAll(coll);} finally { l.unlock(); } |
81 |
} |
82 |
public final void clear() { |
83 |
l.lock(); try {c.clear();} finally { l.unlock(); } |
84 |
} |
85 |
|
86 |
} |