ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RWMap.java
Revision: 1.10
Committed: Sat May 5 17:09:23 2012 UTC (12 years ago) by jsr166
Branch: MAIN
Changes since 1.9: +4 -4 lines
Log Message:
whitespace

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 Maps.
14 * Exists as a sample input for MapLoops test.
15 */
16 public class RWMap implements ConcurrentMap {
17 private final Map m;
18 private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
19
20 public RWMap(Map m) {
21 if (m == null)
22 throw new NullPointerException();
23 this.m = m;
24 }
25
26 public RWMap() {
27 this(new TreeMap()); // use TreeMap by default
28 // this(new IdentityHashMap());
29 }
30
31 public int size() {
32 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
33 l.lock();
34 try { return m.size(); }
35 finally { l.unlock(); }
36 }
37
38 public boolean isEmpty() {
39 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
40 l.lock();
41 try { return m.isEmpty(); }
42 finally { l.unlock(); }
43 }
44
45 public Object get(Object key) {
46 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
47 l.lock();
48 try { return m.get(key); }
49 finally { l.unlock(); }
50 }
51
52 public boolean containsKey(Object key) {
53 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
54 l.lock();
55 try { return m.containsKey(key); }
56 finally { l.unlock(); }
57 }
58
59 public boolean containsValue(Object value) {
60 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
61 l.lock();
62 try { return m.containsValue(value); }
63 finally { l.unlock(); }
64 }
65
66 public Set keySet() { // Not implemented
67 return m.keySet();
68 }
69
70 public Set entrySet() { // Not implemented
71 return m.entrySet();
72 }
73
74 public Collection values() { // Not implemented
75 return m.values();
76 }
77
78 public boolean equals(Object o) {
79 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
80 l.lock();
81 try { return m.equals(o); }
82 finally { l.unlock(); }
83 }
84
85 public int hashCode() {
86 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
87 l.lock();
88 try { return m.hashCode(); }
89 finally { l.unlock(); }
90 }
91
92 public String toString() {
93 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
94 l.lock();
95 try { return m.toString(); }
96 finally { l.unlock(); }
97 }
98
99 public Object put(Object key, Object value) {
100 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
101 l.lock();
102 try { return m.put(key, value); }
103 finally { l.unlock(); }
104 }
105
106 public Object putIfAbsent(Object key, Object value) {
107 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
108 l.lock();
109 try {
110 Object v = m.get(key);
111 return v == null? m.put(key, value) : v;
112 }
113 finally { l.unlock(); }
114 }
115
116 public boolean replace(Object key, Object oldValue, Object newValue) {
117 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
118 l.lock();
119 try {
120 if (m.get(key).equals(oldValue)) {
121 m.put(key, newValue);
122 return true;
123 }
124 return false;
125 }
126 finally { l.unlock(); }
127 }
128
129 public Object replace(Object key, Object newValue) {
130 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
131 l.lock();
132 try {
133 if (m.containsKey(key))
134 return m.put(key, newValue);
135 return null;
136 }
137 finally { l.unlock(); }
138 }
139
140 public Object remove(Object key) {
141 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
142 l.lock();
143 try { return m.remove(key); }
144 finally { l.unlock(); }
145 }
146
147 public boolean remove(Object key, Object value) {
148 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
149 l.lock();
150 try {
151 if (m.get(key).equals(value)) {
152 m.remove(key);
153 return true;
154 }
155 return false;
156 }
157 finally { l.unlock(); }
158 }
159
160 public void putAll(Map map) {
161 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
162 l.lock();
163 try { m.putAll(map); }
164 finally { l.unlock(); }
165 }
166
167 public void clear() {
168 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
169 l.lock();
170 try { m.clear(); }
171 finally { l.unlock(); }
172 }
173
174 }