ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RWCollection.java
Revision: 1.6
Committed: Sun Oct 21 06:40:21 2012 UTC (11 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +0 -2 lines
Log Message:
no blank line between javadoc and corresponding code

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