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.5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
dl |
1.1 |
import java.util.*; |
8 |
|
|
import java.util.concurrent.*; |
9 |
|
|
import java.util.concurrent.locks.*; |
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 RLMap implements Map { |
17 |
|
|
private final Map m; |
18 |
|
|
private final ReentrantLock rl = new ReentrantLock(); |
19 |
jsr166 |
1.3 |
|
20 |
dl |
1.1 |
public RLMap(Map m) { |
21 |
|
|
if (m == null) |
22 |
|
|
throw new NullPointerException(); |
23 |
|
|
this.m = m; |
24 |
|
|
} |
25 |
|
|
|
26 |
jsr166 |
1.3 |
public RLMap() { |
27 |
dl |
1.1 |
this(new TreeMap()); // use TreeMap by default |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
public int size() { |
31 |
|
|
rl.lock(); try {return m.size();} finally { rl.unlock(); } |
32 |
|
|
} |
33 |
jsr166 |
1.4 |
public boolean isEmpty() { |
34 |
dl |
1.1 |
rl.lock(); try {return m.isEmpty();} finally { rl.unlock(); } |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
public Object get(Object key) { |
38 |
|
|
rl.lock(); try {return m.get(key);} finally { rl.unlock(); } |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
public boolean containsKey(Object key) { |
42 |
|
|
rl.lock(); try {return m.containsKey(key);} finally { rl.unlock(); } |
43 |
|
|
} |
44 |
jsr166 |
1.4 |
public boolean containsValue(Object value) { |
45 |
dl |
1.1 |
rl.lock(); try {return m.containsValue(value);} finally { rl.unlock(); } |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
public Set keySet() { // Not implemented |
49 |
|
|
return m.keySet(); |
50 |
|
|
} |
51 |
jsr166 |
1.3 |
|
52 |
dl |
1.1 |
public Set entrySet() { // Not implemented |
53 |
|
|
return m.entrySet(); |
54 |
|
|
} |
55 |
jsr166 |
1.3 |
|
56 |
dl |
1.1 |
public Collection values() { // Not implemented |
57 |
|
|
return m.values(); |
58 |
|
|
} |
59 |
jsr166 |
1.3 |
|
60 |
dl |
1.1 |
public boolean equals(Object o) { |
61 |
|
|
rl.lock(); try {return m.equals(o);} finally { rl.unlock(); } |
62 |
|
|
} |
63 |
|
|
public int hashCode() { |
64 |
|
|
rl.lock(); try {return m.hashCode();} finally { rl.unlock(); } |
65 |
|
|
} |
66 |
|
|
public String toString() { |
67 |
|
|
rl.lock(); try {return m.toString();} finally { rl.unlock(); } |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
public Object put(Object key, Object value) { |
71 |
|
|
rl.lock(); try {return m.put(key, value);} finally { rl.unlock(); } |
72 |
|
|
} |
73 |
|
|
public Object remove(Object key) { |
74 |
|
|
rl.lock(); try {return m.remove(key);} finally { rl.unlock(); } |
75 |
|
|
} |
76 |
|
|
public void putAll(Map map) { |
77 |
|
|
rl.lock(); try {m.putAll(map);} finally { rl.unlock(); } |
78 |
|
|
} |
79 |
|
|
public void clear() { |
80 |
|
|
rl.lock(); try {m.clear();} finally { rl.unlock(); } |
81 |
|
|
} |
82 |
jsr166 |
1.3 |
|
83 |
dl |
1.1 |
} |