ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RWCollection.java
Revision: 1.4
Committed: Tue Mar 15 19:47:05 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.3: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

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