ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RWMap.java
Revision: 1.12
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +0 -1 lines
Log Message:
delete extraneous blank lines

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