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

File Contents

# User Rev Content
1 dl 1.2 /*
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 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6 dl 1.1 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 dl 1.9 public class RWMap implements ConcurrentMap {
17 dl 1.1 private final Map m;
18     private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
19 jsr166 1.5
20 dl 1.1 public RWMap(Map m) {
21     if (m == null)
22     throw new NullPointerException();
23     this.m = m;
24     }
25    
26 jsr166 1.5 public RWMap() {
27 dl 1.1 this(new TreeMap()); // use TreeMap by default
28 jsr166 1.5 // this(new IdentityHashMap());
29 dl 1.1 }
30    
31     public int size() {
32 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
33 jsr166 1.6 l.lock();
34     try { return m.size(); }
35     finally { l.unlock(); }
36 dl 1.1 }
37 jsr166 1.6
38     public boolean isEmpty() {
39 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
40 jsr166 1.6 l.lock();
41     try { return m.isEmpty(); }
42     finally { l.unlock(); }
43 dl 1.1 }
44    
45     public Object get(Object key) {
46 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
47 jsr166 1.6 l.lock();
48     try { return m.get(key); }
49     finally { l.unlock(); }
50 dl 1.1 }
51    
52     public boolean containsKey(Object key) {
53 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
54 jsr166 1.6 l.lock();
55     try { return m.containsKey(key); }
56     finally { l.unlock(); }
57 dl 1.1 }
58 jsr166 1.6
59     public boolean containsValue(Object value) {
60 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
61 jsr166 1.6 l.lock();
62     try { return m.containsValue(value); }
63     finally { l.unlock(); }
64 dl 1.1 }
65    
66     public Set keySet() { // Not implemented
67     return m.keySet();
68     }
69 jsr166 1.5
70 dl 1.1 public Set entrySet() { // Not implemented
71     return m.entrySet();
72     }
73 jsr166 1.5
74 dl 1.1 public Collection values() { // Not implemented
75     return m.values();
76     }
77 jsr166 1.5
78 dl 1.1 public boolean equals(Object o) {
79 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
80 jsr166 1.6 l.lock();
81     try { return m.equals(o); }
82     finally { l.unlock(); }
83 dl 1.1 }
84 jsr166 1.6
85 dl 1.1 public int hashCode() {
86 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
87 jsr166 1.6 l.lock();
88     try { return m.hashCode(); }
89     finally { l.unlock(); }
90 dl 1.1 }
91 jsr166 1.6
92 dl 1.1 public String toString() {
93 jsr166 1.8 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
94 jsr166 1.6 l.lock();
95     try { return m.toString(); }
96     finally { l.unlock(); }
97 dl 1.1 }
98    
99     public Object put(Object key, Object value) {
100 jsr166 1.8 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
101 jsr166 1.6 l.lock();
102     try { return m.put(key, value); }
103     finally { l.unlock(); }
104 dl 1.1 }
105 jsr166 1.6
106 dl 1.9 public Object putIfAbsent(Object key, Object value) {
107     ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
108     l.lock();
109 jsr166 1.10 try {
110 dl 1.9 Object v = m.get(key);
111 jsr166 1.11 return (v == null) ? m.put(key, value) : v;
112 dl 1.9 }
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 jsr166 1.10 try {
120 dl 1.9 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 jsr166 1.10 try {
133 dl 1.9 if (m.containsKey(key))
134     return m.put(key, newValue);
135     return null;
136     }
137     finally { l.unlock(); }
138     }
139    
140 dl 1.1 public Object remove(Object key) {
141 jsr166 1.8 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
142 jsr166 1.6 l.lock();
143     try { return m.remove(key); }
144     finally { l.unlock(); }
145 dl 1.1 }
146 jsr166 1.6
147 dl 1.9 public boolean remove(Object key, Object value) {
148     ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
149     l.lock();
150 jsr166 1.10 try {
151 dl 1.9 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 dl 1.1 public void putAll(Map map) {
161 jsr166 1.8 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
162 jsr166 1.6 l.lock();
163     try { m.putAll(map); }
164     finally { l.unlock(); }
165 dl 1.1 }
166 jsr166 1.6
167 dl 1.1 public void clear() {
168 jsr166 1.8 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
169 jsr166 1.6 l.lock();
170     try { m.clear(); }
171     finally { l.unlock(); }
172 dl 1.1 }
173 jsr166 1.5
174 dl 1.1 }