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