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.12 by jsr166, Thu Jan 15 18:34:19 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines