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 |
} |
30 |
|
31 |
public int size() { |
32 |
rwl.readLock().lock(); try {return m.size();} finally { rwl.readLock().unlock(); } |
33 |
} |
34 |
public boolean isEmpty(){ |
35 |
rwl.readLock().lock(); try {return m.isEmpty();} finally { rwl.readLock().unlock(); } |
36 |
} |
37 |
|
38 |
public Object get(Object key) { |
39 |
rwl.readLock().lock(); try {return m.get(key);} finally { rwl.readLock().unlock(); } |
40 |
} |
41 |
|
42 |
public boolean containsKey(Object key) { |
43 |
rwl.readLock().lock(); try {return m.containsKey(key);} finally { rwl.readLock().unlock(); } |
44 |
} |
45 |
public boolean containsValue(Object value){ |
46 |
rwl.readLock().lock(); try {return m.containsValue(value);} finally { rwl.readLock().unlock(); } |
47 |
} |
48 |
|
49 |
|
50 |
public Set keySet() { // Not implemented |
51 |
return m.keySet(); |
52 |
} |
53 |
|
54 |
public Set entrySet() { // Not implemented |
55 |
return m.entrySet(); |
56 |
} |
57 |
|
58 |
public Collection values() { // Not implemented |
59 |
return m.values(); |
60 |
} |
61 |
|
62 |
public boolean equals(Object o) { |
63 |
rwl.readLock().lock(); try {return m.equals(o);} finally { rwl.readLock().unlock(); } |
64 |
} |
65 |
public int hashCode() { |
66 |
rwl.readLock().lock(); try {return m.hashCode();} finally { rwl.readLock().unlock(); } |
67 |
} |
68 |
public String toString() { |
69 |
rwl.readLock().lock(); try {return m.toString();} finally { rwl.readLock().unlock(); } |
70 |
} |
71 |
|
72 |
|
73 |
|
74 |
public Object put(Object key, Object value) { |
75 |
rwl.writeLock().lock(); try {return m.put(key, value);} finally { rwl.writeLock().unlock(); } |
76 |
} |
77 |
public Object remove(Object key) { |
78 |
rwl.writeLock().lock(); try {return m.remove(key);} finally { rwl.writeLock().unlock(); } |
79 |
} |
80 |
public void putAll(Map map) { |
81 |
rwl.writeLock().lock(); try {m.putAll(map);} finally { rwl.writeLock().unlock(); } |
82 |
} |
83 |
public void clear() { |
84 |
rwl.writeLock().lock(); try {m.clear();} finally { rwl.writeLock().unlock(); } |
85 |
} |
86 |
|
87 |
} |