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 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
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 |
|
|
public class RWMap implements Map { |
17 |
|
|
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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 |
dl |
1.4 |
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.1 |
public Object remove(Object key) { |
107 |
dl |
1.4 |
ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
108 |
jsr166 |
1.6 |
l.lock(); |
109 |
|
|
try { return m.remove(key); } |
110 |
|
|
finally { l.unlock(); } |
111 |
dl |
1.1 |
} |
112 |
jsr166 |
1.6 |
|
113 |
dl |
1.1 |
public void putAll(Map map) { |
114 |
dl |
1.4 |
ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
115 |
jsr166 |
1.6 |
l.lock(); |
116 |
|
|
try { m.putAll(map); } |
117 |
|
|
finally { l.unlock(); } |
118 |
dl |
1.1 |
} |
119 |
jsr166 |
1.6 |
|
120 |
dl |
1.1 |
public void clear() { |
121 |
dl |
1.4 |
ReentrantReadWriteLock.WriteLock l = rwl.writeLock(); |
122 |
jsr166 |
1.6 |
l.lock(); |
123 |
|
|
try { m.clear(); } |
124 |
|
|
finally { l.unlock(); } |
125 |
dl |
1.1 |
} |
126 |
jsr166 |
1.5 |
|
127 |
dl |
1.1 |
} |