ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SCollection.java
Revision: 1.3
Committed: Sat Oct 16 16:22:57 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +58 -18 lines
Log Message:
coding style

File Contents

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