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

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