ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentMap.java
Revision: 1.62
Committed: Thu Apr 30 18:53:51 2015 UTC (9 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.61: +5 -3 lines
Log Message:
fix sample code compilation error

File Contents

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