jdk/src/share/classes/java/util/concurrent/ConcurrentMap.java
author smarks
Tue, 06 Dec 2011 10:14:02 -0800
changeset 11139 db0c2ff5e1ea
parent 9504 a61398ab96e0
child 14325 622c473a21aa
permissions -rw-r--r--
7116997: fix warnings in java.util.PropertyPermission Reviewed-by: smarks Contributed-by: Brandon Passanisi <brandon.passanisi@oracle.com>
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A {@link java.util.Map} providing additional atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Memory consistency effects: As with other concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * collections, actions in a thread prior to placing an object into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * {@code ConcurrentMap} as a key or value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * actions subsequent to the access or removal of that object from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the {@code ConcurrentMap} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>This interface is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public interface ConcurrentMap<K, V> extends Map<K, V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * If the specified key is not already associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * with a value, associate it with the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *   if (!map.containsKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *       return map.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *       return map.get(key);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * except that the action is performed atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return the previous value associated with the specified key, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *         <tt>null</tt> if there was no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *         previously associated <tt>null</tt> with the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *         if the implementation supports null values.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @throws UnsupportedOperationException if the <tt>put</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @throws ClassCastException if the class of the specified key or value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *         prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @throws NullPointerException if the specified key or value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *         and this map does not permit null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @throws IllegalArgumentException if some property of the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *         or value prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    V putIfAbsent(K key, V value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Removes the entry for a key only if currently mapped to a given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *   if (map.containsKey(key) &amp;&amp; map.get(key).equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *       map.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *       return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *   } else return false;</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * except that the action is performed atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param key key with which the specified value is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param value value expected to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @return <tt>true</tt> if the value was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @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
   106
     *         type for this map
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   107
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws NullPointerException if the specified key or value is null,
9504
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   109
     *         and this map does not permit null keys or values
a61398ab96e0 7038501: Clarify meaning of "(optional)" in javadoc
dl
parents: 9242
diff changeset
   110
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    boolean remove(Object key, Object value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Replaces the entry for a key only if currently mapped to a given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *   if (map.containsKey(key) &amp;&amp; map.get(key).equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *       map.put(key, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *       return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *   } else return false;</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * except that the action is performed atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param key key with which the specified value is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param oldValue value expected to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param newValue value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return <tt>true</tt> if the value was replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @throws UnsupportedOperationException if the <tt>put</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws ClassCastException if the class of a specified key or value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *         prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws NullPointerException if a specified key or value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         and this map does not permit null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws IllegalArgumentException if some property of a specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         or value prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    boolean replace(K key, V oldValue, V newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Replaces the entry for a key only if currently mapped to some value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *   if (map.containsKey(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *       return map.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *   } else return null;</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * except that the action is performed atomically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param key key with which the specified value is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the previous value associated with the specified key, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *         <tt>null</tt> if there was no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         previously associated <tt>null</tt> with the key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         if the implementation supports null values.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws UnsupportedOperationException if the <tt>put</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         is not supported by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws ClassCastException if the class of the specified key or value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws NullPointerException if the specified key or value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *         and this map does not permit null keys or values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws IllegalArgumentException if some property of the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *         or value prevents it from being stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    V replace(K key, V value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}