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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
import java.util.Map; |
10 |
import java.util.Objects; |
11 |
import java.util.function.BiConsumer; |
12 |
import java.util.function.BiFunction; |
13 |
import java.util.function.Function; |
14 |
|
15 |
/** |
16 |
* A {@link Map} providing thread safety and atomicity guarantees. |
17 |
* |
18 |
* <p>To maintain the specified guarantees, default implementations of |
19 |
* methods including {@link #putIfAbsent} inherited from {@link Map} |
20 |
* must be overridden by implementations of this interface. Similarly, |
21 |
* implementations of the collections returned by methods {@link |
22 |
* #keySet}, {@link #values}, and {@link #entrySet} must override |
23 |
* methods such as {@code removeIf} when necessary to |
24 |
* preserve atomicity guarantees. |
25 |
* |
26 |
* <p>Memory consistency effects: As with other concurrent |
27 |
* collections, actions in a thread prior to placing an object into a |
28 |
* {@code ConcurrentMap} as a key or value |
29 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
30 |
* actions subsequent to the access or removal of that object from |
31 |
* the {@code ConcurrentMap} in another thread. |
32 |
* |
33 |
* <p>This interface is a member of the |
34 |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
35 |
* Java Collections Framework</a>. |
36 |
* |
37 |
* @since 1.5 |
38 |
* @author Doug Lea |
39 |
* @param <K> the type of keys maintained by this map |
40 |
* @param <V> the type of mapped values |
41 |
*/ |
42 |
public interface ConcurrentMap<K,V> extends Map<K,V> { |
43 |
|
44 |
/** |
45 |
* {@inheritDoc} |
46 |
* |
47 |
* @implNote This implementation assumes that the ConcurrentMap cannot |
48 |
* contain null values and {@code get()} returning null unambiguously means |
49 |
* the key is absent. Implementations which support null values |
50 |
* <strong>must</strong> override this default implementation. |
51 |
* |
52 |
* @throws ClassCastException {@inheritDoc} |
53 |
* @throws NullPointerException {@inheritDoc} |
54 |
* @since 1.8 |
55 |
*/ |
56 |
@Override |
57 |
default V getOrDefault(Object key, V defaultValue) { |
58 |
V v; |
59 |
return ((v = get(key)) != null) ? v : defaultValue; |
60 |
} |
61 |
|
62 |
/** |
63 |
* {@inheritDoc} |
64 |
* |
65 |
* @implSpec The default implementation is equivalent to, for this |
66 |
* {@code map}: |
67 |
* <pre> {@code |
68 |
* for (Map.Entry<K,V> entry : map.entrySet()) { |
69 |
* action.accept(entry.getKey(), entry.getValue()); |
70 |
* }}</pre> |
71 |
* |
72 |
* @implNote The default implementation assumes that |
73 |
* {@code IllegalStateException} thrown by {@code getKey()} or |
74 |
* {@code getValue()} indicates that the entry has been removed and cannot |
75 |
* be processed. Operation continues for subsequent entries. |
76 |
* |
77 |
* @throws NullPointerException {@inheritDoc} |
78 |
* @since 1.8 |
79 |
*/ |
80 |
@Override |
81 |
default void forEach(BiConsumer<? super K, ? super V> action) { |
82 |
Objects.requireNonNull(action); |
83 |
for (Map.Entry<K,V> entry : entrySet()) { |
84 |
K k; |
85 |
V v; |
86 |
try { |
87 |
k = entry.getKey(); |
88 |
v = entry.getValue(); |
89 |
} catch (IllegalStateException ise) { |
90 |
// this usually means the entry is no longer in the map. |
91 |
continue; |
92 |
} |
93 |
action.accept(k, v); |
94 |
} |
95 |
} |
96 |
|
97 |
/** |
98 |
* If the specified key is not already associated |
99 |
* with a value, associates it with the given value. |
100 |
* This is equivalent to, for this {@code map}: |
101 |
* <pre> {@code |
102 |
* if (!map.containsKey(key)) |
103 |
* return map.put(key, value); |
104 |
* else |
105 |
* return map.get(key);}</pre> |
106 |
* |
107 |
* except that the action is performed atomically. |
108 |
* |
109 |
* @implNote This implementation intentionally re-abstracts the |
110 |
* inappropriate default provided in {@code Map}. |
111 |
* |
112 |
* @param key key with which the specified value is to be associated |
113 |
* @param value value to be associated with the specified key |
114 |
* @return the previous value associated with the specified key, or |
115 |
* {@code null} if there was no mapping for the key. |
116 |
* (A {@code null} return can also indicate that the map |
117 |
* previously associated {@code null} with the key, |
118 |
* if the implementation supports null values.) |
119 |
* @throws UnsupportedOperationException if the {@code put} operation |
120 |
* is not supported by this map |
121 |
* @throws ClassCastException if the class of the specified key or value |
122 |
* prevents it from being stored in this map |
123 |
* @throws NullPointerException if the specified key or value is null, |
124 |
* and this map does not permit null keys or values |
125 |
* @throws IllegalArgumentException if some property of the specified key |
126 |
* or value prevents it from being stored in this map |
127 |
*/ |
128 |
V putIfAbsent(K key, V value); |
129 |
|
130 |
/** |
131 |
* Removes the entry for a key only if currently mapped to a given value. |
132 |
* This is equivalent to, for this {@code map}: |
133 |
* <pre> {@code |
134 |
* if (map.containsKey(key) |
135 |
* && Objects.equals(map.get(key), value)) { |
136 |
* map.remove(key); |
137 |
* return true; |
138 |
* } else { |
139 |
* return false; |
140 |
* }}</pre> |
141 |
* |
142 |
* except that the action is performed atomically. |
143 |
* |
144 |
* @implNote This implementation intentionally re-abstracts the |
145 |
* inappropriate default provided in {@code Map}. |
146 |
* |
147 |
* @param key key with which the specified value is associated |
148 |
* @param value value expected to be associated with the specified key |
149 |
* @return {@code true} if the value was removed |
150 |
* @throws UnsupportedOperationException if the {@code remove} operation |
151 |
* is not supported by this map |
152 |
* @throws ClassCastException if the key or value is of an inappropriate |
153 |
* type for this map |
154 |
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>) |
155 |
* @throws NullPointerException if the specified key or value is null, |
156 |
* and this map does not permit null keys or values |
157 |
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>) |
158 |
*/ |
159 |
boolean remove(Object key, Object value); |
160 |
|
161 |
/** |
162 |
* Replaces the entry for a key only if currently mapped to a given value. |
163 |
* This is equivalent to, for this {@code map}: |
164 |
* <pre> {@code |
165 |
* if (map.containsKey(key) |
166 |
* && Objects.equals(map.get(key), oldValue)) { |
167 |
* map.put(key, newValue); |
168 |
* return true; |
169 |
* } else { |
170 |
* return false; |
171 |
* }}</pre> |
172 |
* |
173 |
* except that the action is performed atomically. |
174 |
* |
175 |
* @implNote This implementation intentionally re-abstracts the |
176 |
* inappropriate default provided in {@code Map}. |
177 |
* |
178 |
* @param key key with which the specified value is associated |
179 |
* @param oldValue value expected to be associated with the specified key |
180 |
* @param newValue value to be associated with the specified key |
181 |
* @return {@code true} if the value was replaced |
182 |
* @throws UnsupportedOperationException if the {@code put} operation |
183 |
* is not supported by this map |
184 |
* @throws ClassCastException if the class of a specified key or value |
185 |
* prevents it from being stored in this map |
186 |
* @throws NullPointerException if a specified key or value is null, |
187 |
* and this map does not permit null keys or values |
188 |
* @throws IllegalArgumentException if some property of a specified key |
189 |
* or value prevents it from being stored in this map |
190 |
*/ |
191 |
boolean replace(K key, V oldValue, V newValue); |
192 |
|
193 |
/** |
194 |
* Replaces the entry for a key only if currently mapped to some value. |
195 |
* This is equivalent to, for this {@code map}: |
196 |
* <pre> {@code |
197 |
* if (map.containsKey(key)) |
198 |
* return map.put(key, value); |
199 |
* else |
200 |
* return null;}</pre> |
201 |
* |
202 |
* except that the action is performed atomically. |
203 |
* |
204 |
* @implNote This implementation intentionally re-abstracts the |
205 |
* inappropriate default provided in {@code Map}. |
206 |
* |
207 |
* @param key key with which the specified value is associated |
208 |
* @param value value to be associated with the specified key |
209 |
* @return the previous value associated with the specified key, or |
210 |
* {@code null} if there was no mapping for the key. |
211 |
* (A {@code null} return can also indicate that the map |
212 |
* previously associated {@code null} with the key, |
213 |
* if the implementation supports null values.) |
214 |
* @throws UnsupportedOperationException if the {@code put} operation |
215 |
* is not supported by this map |
216 |
* @throws ClassCastException if the class of the specified key or value |
217 |
* prevents it from being stored in this map |
218 |
* @throws NullPointerException if the specified key or value is null, |
219 |
* and this map does not permit null keys or values |
220 |
* @throws IllegalArgumentException if some property of the specified key |
221 |
* or value prevents it from being stored in this map |
222 |
*/ |
223 |
V replace(K key, V value); |
224 |
|
225 |
/** |
226 |
* {@inheritDoc} |
227 |
* |
228 |
* @implSpec |
229 |
* <p>The default implementation is equivalent to, for this {@code map}: |
230 |
* <pre> {@code |
231 |
* for (Map.Entry<K,V> entry : map.entrySet()) { |
232 |
* K k; |
233 |
* V v; |
234 |
* do { |
235 |
* k = entry.getKey(); |
236 |
* v = entry.getValue(); |
237 |
* } while (!map.replace(k, v, function.apply(k, v))); |
238 |
* }}</pre> |
239 |
* |
240 |
* The default implementation may retry these steps when multiple |
241 |
* threads attempt updates including potentially calling the function |
242 |
* repeatedly for a given key. |
243 |
* |
244 |
* <p>This implementation assumes that the ConcurrentMap cannot contain null |
245 |
* values and {@code get()} returning null unambiguously means the key is |
246 |
* absent. Implementations which support null values <strong>must</strong> |
247 |
* override this default implementation. |
248 |
* |
249 |
* @throws UnsupportedOperationException {@inheritDoc} |
250 |
* @throws NullPointerException {@inheritDoc} |
251 |
* @throws ClassCastException {@inheritDoc} |
252 |
* @throws IllegalArgumentException {@inheritDoc} |
253 |
* @since 1.8 |
254 |
*/ |
255 |
@Override |
256 |
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
257 |
Objects.requireNonNull(function); |
258 |
forEach((k,v) -> { |
259 |
while (!replace(k, v, function.apply(k, v))) { |
260 |
// v changed or k is gone |
261 |
if ( (v = get(k)) == null) { |
262 |
// k is no longer in the map. |
263 |
break; |
264 |
} |
265 |
} |
266 |
}); |
267 |
} |
268 |
|
269 |
/** |
270 |
* {@inheritDoc} |
271 |
* |
272 |
* @implSpec |
273 |
* The default implementation is equivalent to the following steps for this |
274 |
* {@code map}: |
275 |
* |
276 |
* <pre> {@code |
277 |
* V oldValue, newValue; |
278 |
* return ((oldValue = map.get(key)) == null |
279 |
* && (newValue = mappingFunction.apply(key)) != null |
280 |
* && (oldValue = map.putIfAbsent(key, newValue)) == null) |
281 |
* ? newValue |
282 |
* : oldValue;}</pre> |
283 |
* |
284 |
* <p>This implementation assumes that the ConcurrentMap cannot contain null |
285 |
* values and {@code get()} returning null unambiguously means the key is |
286 |
* absent. Implementations which support null values <strong>must</strong> |
287 |
* override this default implementation. |
288 |
* |
289 |
* @throws UnsupportedOperationException {@inheritDoc} |
290 |
* @throws ClassCastException {@inheritDoc} |
291 |
* @throws NullPointerException {@inheritDoc} |
292 |
* @throws IllegalArgumentException {@inheritDoc} |
293 |
* @since 1.8 |
294 |
*/ |
295 |
@Override |
296 |
default V computeIfAbsent(K key, |
297 |
Function<? super K, ? extends V> mappingFunction) { |
298 |
Objects.requireNonNull(mappingFunction); |
299 |
V oldValue, newValue; |
300 |
return ((oldValue = get(key)) == null |
301 |
&& (newValue = mappingFunction.apply(key)) != null |
302 |
&& (oldValue = putIfAbsent(key, newValue)) == null) |
303 |
? newValue |
304 |
: oldValue; |
305 |
} |
306 |
|
307 |
/** |
308 |
* {@inheritDoc} |
309 |
* |
310 |
* @implSpec |
311 |
* The default implementation is equivalent to performing the following |
312 |
* steps for this {@code map}: |
313 |
* |
314 |
* <pre> {@code |
315 |
* for (V oldValue; (oldValue = map.get(key)) != null; ) { |
316 |
* V newValue = remappingFunction.apply(key, oldValue); |
317 |
* if ((newValue == null) |
318 |
* ? map.remove(key, oldValue) |
319 |
* : map.replace(key, oldValue, newValue)) |
320 |
* return newValue; |
321 |
* } |
322 |
* return null;}</pre> |
323 |
* When multiple threads attempt updates, map operations and the |
324 |
* remapping function may be called multiple times. |
325 |
* |
326 |
* <p>This implementation assumes that the ConcurrentMap cannot contain null |
327 |
* values and {@code get()} returning null unambiguously means the key is |
328 |
* absent. Implementations which support null values <strong>must</strong> |
329 |
* override this default implementation. |
330 |
* |
331 |
* @throws UnsupportedOperationException {@inheritDoc} |
332 |
* @throws ClassCastException {@inheritDoc} |
333 |
* @throws NullPointerException {@inheritDoc} |
334 |
* @throws IllegalArgumentException {@inheritDoc} |
335 |
* @since 1.8 |
336 |
*/ |
337 |
@Override |
338 |
default V computeIfPresent(K key, |
339 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
340 |
Objects.requireNonNull(remappingFunction); |
341 |
for (V oldValue; (oldValue = get(key)) != null; ) { |
342 |
V newValue = remappingFunction.apply(key, oldValue); |
343 |
if ((newValue == null) |
344 |
? remove(key, oldValue) |
345 |
: replace(key, oldValue, newValue)) |
346 |
return newValue; |
347 |
} |
348 |
return null; |
349 |
} |
350 |
|
351 |
/** |
352 |
* {@inheritDoc} |
353 |
* |
354 |
* @implSpec |
355 |
* The default implementation is equivalent to performing the following |
356 |
* steps for this {@code map}: |
357 |
* |
358 |
* <pre> {@code |
359 |
* for (;;) { |
360 |
* V oldValue = map.get(key); |
361 |
* V newValue = remappingFunction.apply(key, oldValue); |
362 |
* if (newValue != null) { |
363 |
* if ((oldValue != null) |
364 |
* ? map.replace(key, oldValue, newValue) |
365 |
* : map.putIfAbsent(key, newValue) == null) |
366 |
* return newValue; |
367 |
* } else if (oldValue == null || map.remove(key, oldValue)) { |
368 |
* return null; |
369 |
* } |
370 |
* }}</pre> |
371 |
* When multiple threads attempt updates, map operations and the |
372 |
* remapping function may be called multiple times. |
373 |
* |
374 |
* <p>This implementation assumes that the ConcurrentMap cannot contain null |
375 |
* values and {@code get()} returning null unambiguously means the key is |
376 |
* absent. Implementations which support null values <strong>must</strong> |
377 |
* override this default implementation. |
378 |
* |
379 |
* @throws UnsupportedOperationException {@inheritDoc} |
380 |
* @throws ClassCastException {@inheritDoc} |
381 |
* @throws NullPointerException {@inheritDoc} |
382 |
* @throws IllegalArgumentException {@inheritDoc} |
383 |
* @since 1.8 |
384 |
*/ |
385 |
@Override |
386 |
default V compute(K key, |
387 |
BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
388 |
retry: for (;;) { |
389 |
V oldValue = get(key); |
390 |
// if putIfAbsent fails, opportunistically use its return value |
391 |
haveOldValue: for (;;) { |
392 |
V newValue = remappingFunction.apply(key, oldValue); |
393 |
if (newValue != null) { |
394 |
if (oldValue != null) { |
395 |
if (replace(key, oldValue, newValue)) |
396 |
return newValue; |
397 |
} |
398 |
else if ((oldValue = putIfAbsent(key, newValue)) == null) |
399 |
return newValue; |
400 |
else continue haveOldValue; |
401 |
} else if (oldValue == null || remove(key, oldValue)) { |
402 |
return null; |
403 |
} |
404 |
continue retry; |
405 |
} |
406 |
} |
407 |
} |
408 |
|
409 |
/** |
410 |
* {@inheritDoc} |
411 |
* |
412 |
* @implSpec |
413 |
* The default implementation is equivalent to performing the following |
414 |
* steps for this {@code map}: |
415 |
* |
416 |
* <pre> {@code |
417 |
* for (;;) { |
418 |
* V oldValue = map.get(key); |
419 |
* if (oldValue != null) { |
420 |
* V newValue = remappingFunction.apply(oldValue, value); |
421 |
* if (newValue != null) { |
422 |
* if (map.replace(key, oldValue, newValue)) |
423 |
* return newValue; |
424 |
* } else if (map.remove(key, oldValue)) { |
425 |
* return null; |
426 |
* } |
427 |
* } else if (map.putIfAbsent(key, value) == null) { |
428 |
* return value; |
429 |
* } |
430 |
* }}</pre> |
431 |
* When multiple threads attempt updates, map operations and the |
432 |
* remapping function may be called multiple times. |
433 |
* |
434 |
* <p>This implementation assumes that the ConcurrentMap cannot contain null |
435 |
* values and {@code get()} returning null unambiguously means the key is |
436 |
* absent. Implementations which support null values <strong>must</strong> |
437 |
* override this default implementation. |
438 |
* |
439 |
* @throws UnsupportedOperationException {@inheritDoc} |
440 |
* @throws ClassCastException {@inheritDoc} |
441 |
* @throws NullPointerException {@inheritDoc} |
442 |
* @throws IllegalArgumentException {@inheritDoc} |
443 |
* @since 1.8 |
444 |
*/ |
445 |
@Override |
446 |
default V merge(K key, V value, |
447 |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
448 |
Objects.requireNonNull(remappingFunction); |
449 |
Objects.requireNonNull(value); |
450 |
retry: for (;;) { |
451 |
V oldValue = get(key); |
452 |
// if putIfAbsent fails, opportunistically use its return value |
453 |
haveOldValue: for (;;) { |
454 |
if (oldValue != null) { |
455 |
V newValue = remappingFunction.apply(oldValue, value); |
456 |
if (newValue != null) { |
457 |
if (replace(key, oldValue, newValue)) |
458 |
return newValue; |
459 |
} else if (remove(key, oldValue)) { |
460 |
return null; |
461 |
} |
462 |
continue retry; |
463 |
} else { |
464 |
if ((oldValue = putIfAbsent(key, value)) == null) |
465 |
return value; |
466 |
continue haveOldValue; |
467 |
} |
468 |
} |
469 |
} |
470 |
} |
471 |
} |