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