ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RWMap.java
(Generate patch)

Comparing jsr166/src/test/loops/RWMap.java (file contents):
Revision 1.7 by jsr166, Tue Mar 15 19:47:05 2011 UTC vs.
Revision 1.10 by jsr166, Sat May 5 17:09:23 2012 UTC

# Line 13 | Line 13 | import java.util.concurrent.locks.*;
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 {
16 > public class RWMap implements ConcurrentMap {
17      private final Map m;
18      private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
19  
# Line 29 | Line 29 | public class RWMap implements Map {
29      }
30  
31      public int size() {
32 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
32 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
33          l.lock();
34          try { return m.size(); }
35          finally { l.unlock(); }
36      }
37  
38      public boolean isEmpty() {
39 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
39 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
40          l.lock();
41          try { return m.isEmpty(); }
42          finally { l.unlock(); }
43      }
44  
45      public Object get(Object key) {
46 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
46 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
47          l.lock();
48          try { return m.get(key); }
49          finally { l.unlock(); }
50      }
51  
52      public boolean containsKey(Object key) {
53 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
53 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
54          l.lock();
55          try { return m.containsKey(key); }
56          finally { l.unlock(); }
57      }
58  
59      public boolean containsValue(Object value) {
60 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
60 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
61          l.lock();
62          try { return m.containsValue(value); }
63          finally { l.unlock(); }
# Line 76 | Line 76 | public class RWMap implements Map {
76      }
77  
78      public boolean equals(Object o) {
79 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
79 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
80          l.lock();
81          try { return m.equals(o); }
82          finally { l.unlock(); }
83      }
84  
85      public int hashCode() {
86 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
86 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
87          l.lock();
88          try { return m.hashCode(); }
89          finally { l.unlock(); }
90      }
91  
92      public String toString() {
93 <        ReentrantReadWriteLock.ReadLock l =  rwl.readLock();
93 >        ReentrantReadWriteLock.ReadLock l = rwl.readLock();
94          l.lock();
95          try { return m.toString(); }
96          finally { l.unlock(); }
97      }
98  
99      public Object put(Object key, Object value) {
100 <        ReentrantReadWriteLock.WriteLock l =  rwl.writeLock();
100 >        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
101          l.lock();
102          try { return m.put(key, value); }
103          finally { l.unlock(); }
104      }
105  
106 +    public Object putIfAbsent(Object key, Object value) {
107 +        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
108 +        l.lock();
109 +        try {
110 +            Object v = m.get(key);
111 +            return v == null?  m.put(key, value) : v;
112 +        }
113 +        finally { l.unlock(); }
114 +    }
115 +
116 +    public boolean replace(Object key, Object oldValue, Object newValue) {
117 +        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
118 +        l.lock();
119 +        try {
120 +            if (m.get(key).equals(oldValue)) {
121 +                m.put(key, newValue);
122 +                return true;
123 +            }
124 +            return false;
125 +        }
126 +        finally { l.unlock(); }
127 +    }
128 +
129 +    public Object replace(Object key, Object newValue) {
130 +        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
131 +        l.lock();
132 +        try {
133 +            if (m.containsKey(key))
134 +                return m.put(key, newValue);
135 +            return null;
136 +        }
137 +        finally { l.unlock(); }
138 +    }
139 +
140      public Object remove(Object key) {
141 <        ReentrantReadWriteLock.WriteLock l =  rwl.writeLock();
141 >        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
142          l.lock();
143          try { return m.remove(key); }
144          finally { l.unlock(); }
145      }
146  
147 +    public boolean remove(Object key, Object value) {
148 +        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
149 +        l.lock();
150 +        try {
151 +            if (m.get(key).equals(value)) {
152 +                m.remove(key);
153 +                return true;
154 +            }
155 +            return false;
156 +        }
157 +        finally { l.unlock(); }
158 +    }
159 +
160      public void putAll(Map map) {
161 <        ReentrantReadWriteLock.WriteLock l =  rwl.writeLock();
161 >        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
162          l.lock();
163          try { m.putAll(map); }
164          finally { l.unlock(); }
165      }
166  
167      public void clear() {
168 <        ReentrantReadWriteLock.WriteLock l =  rwl.writeLock();
168 >        ReentrantReadWriteLock.WriteLock l = rwl.writeLock();
169          l.lock();
170          try { m.clear(); }
171          finally { l.unlock(); }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines