ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentMap.java
Revision: 1.61
Committed: Thu Apr 30 18:50:22 2015 UTC (9 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.60: +4 -2 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.17 * Expert Group and released to the public domain, as explained at
4 jsr166 1.31 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6    
7 tim 1.1 package java.util.concurrent;
8 jsr166 1.53
9 tim 1.1 import java.util.Map;
10 dl 1.46 import java.util.Objects;
11     import java.util.function.BiConsumer;
12 dl 1.44 import java.util.function.BiFunction;
13 dl 1.46 import java.util.function.Function;
14 tim 1.1
15     /**
16 jsr166 1.43 * A {@link java.util.Map} providing thread safety and atomicity
17 dl 1.42 * guarantees.
18 dl 1.19 *
19 dl 1.55 * <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 dl 1.46 *
27 jsr166 1.29 * <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 dl 1.19 * <p>This interface is a member of the
35 jsr166 1.30 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
36 jsr166 1.47 * Java Collections Framework</a>.
37 jsr166 1.21 *
38 dl 1.4 * @since 1.5
39     * @author Doug Lea
40 dl 1.13 * @param <K> the type of keys maintained by this map
41 jsr166 1.21 * @param <V> the type of mapped values
42 dl 1.4 */
43 jsr166 1.56 public interface ConcurrentMap<K,V> extends Map<K,V> {
44 dl 1.41
45     /**
46     * {@inheritDoc}
47     *
48 dl 1.55 * @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 dl 1.46 *
53     * @throws ClassCastException {@inheritDoc}
54     * @throws NullPointerException {@inheritDoc}
55     * @since 1.8
56 dl 1.41 */
57     @Override
58     default V getOrDefault(Object key, V defaultValue) {
59     V v;
60     return ((v = get(key)) != null) ? v : defaultValue;
61     }
62    
63 dl 1.46 /**
64     * {@inheritDoc}
65     *
66     * @implSpec The default implementation is equivalent to, for this
67     * {@code map}:
68 jsr166 1.52 * <pre> {@code
69 jsr166 1.57 * for (Map.Entry<K,V> entry : map.entrySet()) {
70 jsr166 1.56 * action.accept(entry.getKey(), entry.getValue());
71 jsr166 1.57 * }}</pre>
72 dl 1.55 *
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 dl 1.46 *
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 jsr166 1.56 for (Map.Entry<K,V> entry : entrySet()) {
85 dl 1.46 K k;
86     V v;
87     try {
88     k = entry.getKey();
89     v = entry.getValue();
90 jsr166 1.56 } catch (IllegalStateException ise) {
91 dl 1.46 // this usually means the entry is no longer in the map.
92     continue;
93     }
94     action.accept(k, v);
95     }
96     }
97    
98 tim 1.1 /**
99     * If the specified key is not already associated
100 jsr166 1.60 * with a value, associates it with the given value.
101     * This is equivalent to, for this {@code map}:
102 jsr166 1.59 * <pre> {@code
103 dl 1.55 * if (!map.containsKey(key))
104     * return map.put(key, value);
105     * else
106 jsr166 1.59 * return map.get(key);}</pre>
107 jsr166 1.36 *
108 jsr166 1.21 * except that the action is performed atomically.
109     *
110 dl 1.55 * @implNote This implementation intentionally re-abstracts the
111     * inappropriate default provided in {@code Map}.
112 dl 1.46 *
113 jsr166 1.24 * @param key key with which the specified value is to be associated
114     * @param value value to be associated with the specified key
115 jsr166 1.25 * @return the previous value associated with the specified key, or
116 jsr166 1.38 * {@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 jsr166 1.25 * if the implementation supports null values.)
120 jsr166 1.38 * @throws UnsupportedOperationException if the {@code put} operation
121 jsr166 1.24 * is not supported by this map
122 tim 1.1 * @throws ClassCastException if the class of the specified key or value
123 jsr166 1.24 * 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 dl 1.18 */
129 jsr166 1.57 V putIfAbsent(K key, V value);
130 dl 1.6
131     /**
132 jsr166 1.21 * Removes the entry for a key only if currently mapped to a given value.
133 jsr166 1.60 * This is equivalent to, for this {@code map}:
134 jsr166 1.59 * <pre> {@code
135 jsr166 1.61 * if (map.containsKey(key)
136     * && Objects.equals(map.get(key), value)) {
137 jsr166 1.36 * map.remove(key);
138     * return true;
139 jsr166 1.59 * } else {
140 dl 1.55 * return false;
141 jsr166 1.59 * }}</pre>
142 jsr166 1.36 *
143 dl 1.6 * except that the action is performed atomically.
144 jsr166 1.21 *
145 dl 1.55 * @implNote This implementation intentionally re-abstracts the
146     * inappropriate default provided in {@code Map}.
147 dl 1.46 *
148 jsr166 1.24 * @param key key with which the specified value is associated
149 jsr166 1.25 * @param value value expected to be associated with the specified key
150 jsr166 1.38 * @return {@code true} if the value was removed
151     * @throws UnsupportedOperationException if the {@code remove} operation
152 jsr166 1.24 * is not supported by this map
153 jsr166 1.25 * @throws ClassCastException if the key or value is of an inappropriate
154 jsr166 1.35 * type for this map
155 dl 1.33 * (<a href="../Collection.html#optional-restrictions">optional</a>)
156 jsr166 1.24 * @throws NullPointerException if the specified key or value is null,
157 dl 1.32 * and this map does not permit null keys or values
158 dl 1.33 * (<a href="../Collection.html#optional-restrictions">optional</a>)
159 dl 1.6 */
160 dl 1.7 boolean remove(Object key, Object value);
161 dl 1.14
162     /**
163 jsr166 1.21 * Replaces the entry for a key only if currently mapped to a given value.
164 jsr166 1.60 * This is equivalent to, for this {@code map}:
165 jsr166 1.59 * <pre> {@code
166 jsr166 1.61 * if (map.containsKey(key)
167     * && Objects.equals(map.get(key), oldValue)) {
168 jsr166 1.36 * map.put(key, newValue);
169     * return true;
170 jsr166 1.59 * } else {
171 dl 1.55 * return false;
172 jsr166 1.59 * }}</pre>
173 jsr166 1.36 *
174 dl 1.14 * except that the action is performed atomically.
175 jsr166 1.21 *
176 dl 1.55 * @implNote This implementation intentionally re-abstracts the
177     * inappropriate default provided in {@code Map}.
178 dl 1.46 *
179 jsr166 1.24 * @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 jsr166 1.38 * @return {@code true} if the value was replaced
183     * @throws UnsupportedOperationException if the {@code put} operation
184 jsr166 1.24 * is not supported by this map
185 jsr166 1.25 * @throws ClassCastException if the class of a specified key or value
186     * prevents it from being stored in this map
187 jsr166 1.24 * @throws NullPointerException if a specified key or value is null,
188     * and this map does not permit null keys or values
189 jsr166 1.25 * @throws IllegalArgumentException if some property of a specified key
190     * or value prevents it from being stored in this map
191 dl 1.14 */
192     boolean replace(K key, V oldValue, V newValue);
193 dl 1.6
194 dl 1.15 /**
195 jsr166 1.21 * Replaces the entry for a key only if currently mapped to some value.
196 jsr166 1.60 * This is equivalent to, for this {@code map}:
197 jsr166 1.59 * <pre> {@code
198 dl 1.55 * if (map.containsKey(key)) {
199 jsr166 1.36 * return map.put(key, value);
200 jsr166 1.59 * } else {
201 dl 1.55 * return null;
202 jsr166 1.59 * }}</pre>
203 jsr166 1.36 *
204 dl 1.15 * except that the action is performed atomically.
205 jsr166 1.21 *
206 dl 1.55 * @implNote This implementation intentionally re-abstracts the
207     * inappropriate default provided in {@code Map}.
208 dl 1.46 *
209 jsr166 1.24 * @param key key with which the specified value is associated
210     * @param value value to be associated with the specified key
211 jsr166 1.25 * @return the previous value associated with the specified key, or
212 jsr166 1.38 * {@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 jsr166 1.25 * if the implementation supports null values.)
216 jsr166 1.38 * @throws UnsupportedOperationException if the {@code put} operation
217 jsr166 1.24 * is not supported by this map
218 jsr166 1.25 * @throws ClassCastException if the class of the specified key or value
219     * prevents it from being stored in this map
220 jsr166 1.24 * @throws NullPointerException if the specified key or value is null,
221     * and this map does not permit null keys or values
222 jsr166 1.25 * @throws IllegalArgumentException if some property of the specified key
223     * or value prevents it from being stored in this map
224 dl 1.15 */
225 dl 1.16 V replace(K key, V value);
226 dl 1.44
227     /**
228     * {@inheritDoc}
229     *
230 dl 1.46 * @implSpec
231     * <p>The default implementation is equivalent to, for this {@code map}:
232 jsr166 1.52 * <pre> {@code
233 jsr166 1.57 * for (Map.Entry<K,V> entry : map.entrySet()) {
234 jsr166 1.56 * do {
235     * K k = entry.getKey();
236     * V v = entry.getValue();
237     * } while (!replace(k, v, function.apply(k, v)));
238 jsr166 1.57 * }}</pre>
239 dl 1.46 *
240     * The default implementation may retry these steps when multiple
241 dl 1.55 * 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 dl 1.46 *
249     * @throws UnsupportedOperationException {@inheritDoc}
250     * @throws NullPointerException {@inheritDoc}
251     * @throws ClassCastException {@inheritDoc}
252     * @throws IllegalArgumentException {@inheritDoc}
253     * @since 1.8
254 dl 1.44 */
255     @Override
256     default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
257 dl 1.46 Objects.requireNonNull(function);
258     forEach((k,v) -> {
259 jsr166 1.56 while (!replace(k, v, function.apply(k, v))) {
260 dl 1.46 // v changed or k is gone
261     if ( (v = get(k)) == null) {
262     // k is no longer in the map.
263     break;
264     }
265 dl 1.44 }
266 dl 1.46 });
267     }
268    
269     /**
270     * {@inheritDoc}
271     *
272     * @implSpec
273     * The default implementation is equivalent to the following steps for this
274     * {@code map}, then returning the current value or {@code null} if now
275     * absent:
276     *
277 jsr166 1.52 * <pre> {@code
278 dl 1.46 * if (map.get(key) == null) {
279 jsr166 1.59 * V newValue = mappingFunction.apply(key);
280     * if (newValue != null)
281     * return map.putIfAbsent(key, newValue);
282     * }}</pre>
283 dl 1.46 *
284     * The default implementation may retry these steps when multiple
285 dl 1.55 * threads attempt updates including potentially calling the mapping
286     * function multiple times.
287     *
288     * <p>This implementation assumes that the ConcurrentMap cannot contain null
289     * values and {@code get()} returning null unambiguously means the key is
290     * absent. Implementations which support null values <strong>must</strong>
291     * override this default implementation.
292 dl 1.46 *
293     * @throws UnsupportedOperationException {@inheritDoc}
294     * @throws ClassCastException {@inheritDoc}
295     * @throws NullPointerException {@inheritDoc}
296     * @since 1.8
297     */
298     @Override
299     default V computeIfAbsent(K key,
300     Function<? super K, ? extends V> mappingFunction) {
301     Objects.requireNonNull(mappingFunction);
302     V v, newValue;
303     return ((v = get(key)) == null &&
304     (newValue = mappingFunction.apply(key)) != null &&
305     (v = putIfAbsent(key, newValue)) == null) ? newValue : v;
306     }
307 dl 1.44
308 dl 1.46 /**
309     * {@inheritDoc}
310     *
311     * @implSpec
312     * The default implementation is equivalent to performing the following
313     * steps for this {@code map}, then returning the current value or
314 jsr166 1.59 * {@code null} if now absent:
315 dl 1.46 *
316 jsr166 1.52 * <pre> {@code
317 dl 1.46 * if (map.get(key) != null) {
318 jsr166 1.59 * V oldValue = map.get(key);
319     * V newValue = remappingFunction.apply(key, oldValue);
320     * if (newValue != null)
321     * map.replace(key, oldValue, newValue);
322     * else
323     * map.remove(key, oldValue);
324     * }}</pre>
325 dl 1.46 *
326 dl 1.55 * The default implementation may retry these steps when multiple threads
327     * attempt updates including potentially calling the remapping function
328 dl 1.46 * multiple times.
329     *
330 dl 1.55 * <p>This implementation assumes that the ConcurrentMap cannot contain null
331     * values and {@code get()} returning null unambiguously means the key is
332     * absent. Implementations which support null values <strong>must</strong>
333     * override this default implementation.
334     *
335 dl 1.46 * @throws UnsupportedOperationException {@inheritDoc}
336     * @throws ClassCastException {@inheritDoc}
337     * @throws NullPointerException {@inheritDoc}
338     * @since 1.8
339     */
340     @Override
341     default V computeIfPresent(K key,
342     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
343     Objects.requireNonNull(remappingFunction);
344     V oldValue;
345 jsr166 1.56 while ((oldValue = get(key)) != null) {
346 dl 1.46 V newValue = remappingFunction.apply(key, oldValue);
347     if (newValue != null) {
348     if (replace(key, oldValue, newValue))
349     return newValue;
350     } else if (remove(key, oldValue))
351 jsr166 1.57 return null;
352 dl 1.46 }
353     return oldValue;
354     }
355    
356     /**
357     * {@inheritDoc}
358     *
359     * @implSpec
360     * The default implementation is equivalent to performing the following
361     * steps for this {@code map}, then returning the current value or
362     * {@code null} if absent:
363     *
364 jsr166 1.52 * <pre> {@code
365 dl 1.46 * V oldValue = map.get(key);
366     * V newValue = remappingFunction.apply(key, oldValue);
367     * if (oldValue != null ) {
368 jsr166 1.59 * if (newValue != null)
369     * map.replace(key, oldValue, newValue);
370     * else
371     * map.remove(key, oldValue);
372 dl 1.46 * } else {
373 jsr166 1.59 * if (newValue != null)
374     * map.putIfAbsent(key, newValue);
375     * else
376     * return null;
377     * }}</pre>
378 dl 1.46 *
379     * The default implementation may retry these steps when multiple
380 dl 1.55 * threads attempt updates including potentially calling the remapping
381     * function multiple times.
382     *
383     * <p>This implementation assumes that the ConcurrentMap cannot contain null
384     * values and {@code get()} returning null unambiguously means the key is
385     * absent. Implementations which support null values <strong>must</strong>
386     * override this default implementation.
387 dl 1.46 *
388     * @throws UnsupportedOperationException {@inheritDoc}
389     * @throws ClassCastException {@inheritDoc}
390     * @throws NullPointerException {@inheritDoc}
391     * @since 1.8
392     */
393     @Override
394     default V compute(K key,
395     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
396     Objects.requireNonNull(remappingFunction);
397     V oldValue = get(key);
398 jsr166 1.56 for (;;) {
399 dl 1.46 V newValue = remappingFunction.apply(key, oldValue);
400     if (newValue == null) {
401     // delete mapping
402     if (oldValue != null || containsKey(key)) {
403     // something to remove
404     if (remove(key, oldValue)) {
405     // removed the old value as expected
406     return null;
407     }
408    
409     // some other value replaced old value. try again.
410     oldValue = get(key);
411     } else {
412     // nothing to do. Leave things as they were.
413     return null;
414     }
415     } else {
416     // add or replace old mapping
417     if (oldValue != null) {
418     // replace
419     if (replace(key, oldValue, newValue)) {
420     // replaced as expected.
421     return newValue;
422     }
423    
424     // some other value replaced old value. try again.
425     oldValue = get(key);
426     } else {
427     // add (replace if oldValue was null)
428     if ((oldValue = putIfAbsent(key, newValue)) == null) {
429     // replaced
430     return newValue;
431     }
432    
433     // some other value replaced old value. try again.
434     }
435 dl 1.44 }
436     }
437     }
438    
439 dl 1.46 /**
440     * {@inheritDoc}
441     *
442     * @implSpec
443     * The default implementation is equivalent to performing the following
444     * steps for this {@code map}, then returning the current value or
445     * {@code null} if absent:
446     *
447 jsr166 1.52 * <pre> {@code
448 dl 1.46 * V oldValue = map.get(key);
449     * V newValue = (oldValue == null) ? value :
450 jsr166 1.59 * remappingFunction.apply(oldValue, value);
451 dl 1.46 * if (newValue == null)
452 jsr166 1.59 * map.remove(key);
453 dl 1.46 * else
454 jsr166 1.59 * map.put(key, newValue);}</pre>
455 dl 1.46 *
456 dl 1.55 * <p>The default implementation may retry these steps when multiple
457     * threads attempt updates including potentially calling the remapping
458     * function multiple times.
459     *
460     * <p>This implementation assumes that the ConcurrentMap cannot contain null
461     * values and {@code get()} returning null unambiguously means the key is
462     * absent. Implementations which support null values <strong>must</strong>
463     * override this default implementation.
464 dl 1.46 *
465     * @throws UnsupportedOperationException {@inheritDoc}
466     * @throws ClassCastException {@inheritDoc}
467     * @throws NullPointerException {@inheritDoc}
468     * @since 1.8
469     */
470     @Override
471     default V merge(K key, V value,
472     BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
473     Objects.requireNonNull(remappingFunction);
474     Objects.requireNonNull(value);
475     V oldValue = get(key);
476     for (;;) {
477     if (oldValue != null) {
478     V newValue = remappingFunction.apply(oldValue, value);
479     if (newValue != null) {
480     if (replace(key, oldValue, newValue))
481     return newValue;
482     } else if (remove(key, oldValue)) {
483     return null;
484     }
485     oldValue = get(key);
486     } else {
487     if ((oldValue = putIfAbsent(key, value)) == null) {
488     return value;
489     }
490     }
491     }
492     }
493 tim 1.1 }