ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RWMap.java
Revision: 1.5
Committed: Thu Oct 29 23:09:08 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +7 -7 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/licenses/publicdomain
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
17 public class RWMap implements Map {
18 private final Map m;
19 private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
20
21 public RWMap(Map m) {
22 if (m == null)
23 throw new NullPointerException();
24 this.m = m;
25 }
26
27 public RWMap() {
28 this(new TreeMap()); // use TreeMap by default
29 // this(new IdentityHashMap());
30 }
31
32 public int size() {
33 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
34 l.lock(); try {return m.size();} finally { l.unlock(); }
35 }
36 public boolean isEmpty(){
37 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
38 l.lock(); try {return m.isEmpty();} finally { l.unlock(); }
39 }
40
41 public Object get(Object key) {
42 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
43 l.lock(); try {return m.get(key);} finally { l.unlock(); }
44 }
45
46 public boolean containsKey(Object key) {
47 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
48 l.lock(); try {return m.containsKey(key);} finally { l.unlock(); }
49 }
50 public boolean containsValue(Object value){
51 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
52 l.lock(); try {return m.containsValue(value);} finally { l.unlock(); }
53 }
54
55
56 public Set keySet() { // Not implemented
57 return m.keySet();
58 }
59
60 public Set entrySet() { // Not implemented
61 return m.entrySet();
62 }
63
64 public Collection values() { // Not implemented
65 return m.values();
66 }
67
68 public boolean equals(Object o) {
69 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
70 l.lock(); try {return m.equals(o);} finally { l.unlock(); }
71 }
72 public int hashCode() {
73 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
74 l.lock(); try {return m.hashCode();} finally { l.unlock(); }
75 }
76 public String toString() {
77 ReentrantReadWriteLock.ReadLock l = rwl.readLock();
78 l.lock(); try {return m.toString();} finally { l.unlock(); }
79 }
80
81
82
83 public Object put(Object key, Object value) {
84 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
85 l.lock(); try {return m.put(key, value);} finally { l.unlock(); }
86 }
87 public Object remove(Object key) {
88 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
89 l.lock(); try {return m.remove(key);} finally { l.unlock(); }
90 }
91 public void putAll(Map map) {
92 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
93 l.lock(); try {m.putAll(map);} finally { l.unlock(); }
94 }
95 public void clear() {
96 ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
97 l.lock(); try {m.clear();} finally { l.unlock(); }
98 }
99
100 }