rt/emul/compact/src/main/java/java/util/concurrent/ConcurrentMap.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
permissions -rw-r--r--
Set of JDK classes needed to run javac
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  *
     4  * This code is free software; you can redistribute it and/or modify it
     5  * under the terms of the GNU General Public License version 2 only, as
     6  * published by the Free Software Foundation.  Oracle designates this
     7  * particular file as subject to the "Classpath" exception as provided
     8  * by Oracle in the LICENSE file that accompanied this code.
     9  *
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    13  * version 2 for more details (a copy is included in the LICENSE file that
    14  * accompanied this code).
    15  *
    16  * You should have received a copy of the GNU General Public License version
    17  * 2 along with this work; if not, write to the Free Software Foundation,
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    19  *
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * or visit www.oracle.com if you need additional information or have any
    22  * questions.
    23  */
    24 
    25 /*
    26  * This file is available under and governed by the GNU General Public
    27  * License version 2 only, as published by the Free Software Foundation.
    28  * However, the following notice accompanied the original version of this
    29  * file:
    30  *
    31  * Written by Doug Lea with assistance from members of JCP JSR-166
    32  * Expert Group and released to the public domain, as explained at
    33  * http://creativecommons.org/publicdomain/zero/1.0/
    34  */
    35 
    36 package java.util.concurrent;
    37 import java.util.Map;
    38 
    39 /**
    40  * A {@link java.util.Map} providing additional atomic
    41  * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods.
    42  *
    43  * <p>Memory consistency effects: As with other concurrent
    44  * collections, actions in a thread prior to placing an object into a
    45  * {@code ConcurrentMap} as a key or value
    46  * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
    47  * actions subsequent to the access or removal of that object from
    48  * the {@code ConcurrentMap} in another thread.
    49  *
    50  * <p>This interface is a member of the
    51  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    52  * Java Collections Framework</a>.
    53  *
    54  * @since 1.5
    55  * @author Doug Lea
    56  * @param <K> the type of keys maintained by this map
    57  * @param <V> the type of mapped values
    58  */
    59 public interface ConcurrentMap<K, V> extends Map<K, V> {
    60     /**
    61      * If the specified key is not already associated
    62      * with a value, associate it with the given value.
    63      * This is equivalent to
    64      * <pre>
    65      *   if (!map.containsKey(key))
    66      *       return map.put(key, value);
    67      *   else
    68      *       return map.get(key);</pre>
    69      * except that the action is performed atomically.
    70      *
    71      * @param key key with which the specified value is to be associated
    72      * @param value value to be associated with the specified key
    73      * @return the previous value associated with the specified key, or
    74      *         <tt>null</tt> if there was no mapping for the key.
    75      *         (A <tt>null</tt> return can also indicate that the map
    76      *         previously associated <tt>null</tt> with the key,
    77      *         if the implementation supports null values.)
    78      * @throws UnsupportedOperationException if the <tt>put</tt> operation
    79      *         is not supported by this map
    80      * @throws ClassCastException if the class of the specified key or value
    81      *         prevents it from being stored in this map
    82      * @throws NullPointerException if the specified key or value is null,
    83      *         and this map does not permit null keys or values
    84      * @throws IllegalArgumentException if some property of the specified key
    85      *         or value prevents it from being stored in this map
    86      *
    87      */
    88     V putIfAbsent(K key, V value);
    89 
    90     /**
    91      * Removes the entry for a key only if currently mapped to a given value.
    92      * This is equivalent to
    93      * <pre>
    94      *   if (map.containsKey(key) &amp;&amp; map.get(key).equals(value)) {
    95      *       map.remove(key);
    96      *       return true;
    97      *   } else return false;</pre>
    98      * except that the action is performed atomically.
    99      *
   100      * @param key key with which the specified value is associated
   101      * @param value value expected to be associated with the specified key
   102      * @return <tt>true</tt> if the value was removed
   103      * @throws UnsupportedOperationException if the <tt>remove</tt> operation
   104      *         is not supported by this map
   105      * @throws ClassCastException if the key or value is of an inappropriate
   106      *         type for this map
   107      *         (<a href="../Collection.html#optional-restrictions">optional</a>)
   108      * @throws NullPointerException if the specified key or value is null,
   109      *         and this map does not permit null keys or values
   110      *         (<a href="../Collection.html#optional-restrictions">optional</a>)
   111      */
   112     boolean remove(Object key, Object value);
   113 
   114     /**
   115      * Replaces the entry for a key only if currently mapped to a given value.
   116      * This is equivalent to
   117      * <pre>
   118      *   if (map.containsKey(key) &amp;&amp; map.get(key).equals(oldValue)) {
   119      *       map.put(key, newValue);
   120      *       return true;
   121      *   } else return false;</pre>
   122      * except that the action is performed atomically.
   123      *
   124      * @param key key with which the specified value is associated
   125      * @param oldValue value expected to be associated with the specified key
   126      * @param newValue value to be associated with the specified key
   127      * @return <tt>true</tt> if the value was replaced
   128      * @throws UnsupportedOperationException if the <tt>put</tt> operation
   129      *         is not supported by this map
   130      * @throws ClassCastException if the class of a specified key or value
   131      *         prevents it from being stored in this map
   132      * @throws NullPointerException if a specified key or value is null,
   133      *         and this map does not permit null keys or values
   134      * @throws IllegalArgumentException if some property of a specified key
   135      *         or value prevents it from being stored in this map
   136      */
   137     boolean replace(K key, V oldValue, V newValue);
   138 
   139     /**
   140      * Replaces the entry for a key only if currently mapped to some value.
   141      * This is equivalent to
   142      * <pre>
   143      *   if (map.containsKey(key)) {
   144      *       return map.put(key, value);
   145      *   } else return null;</pre>
   146      * except that the action is performed atomically.
   147      *
   148      * @param key key with which the specified value is associated
   149      * @param value value to be associated with the specified key
   150      * @return the previous value associated with the specified key, or
   151      *         <tt>null</tt> if there was no mapping for the key.
   152      *         (A <tt>null</tt> return can also indicate that the map
   153      *         previously associated <tt>null</tt> with the key,
   154      *         if the implementation supports null values.)
   155      * @throws UnsupportedOperationException if the <tt>put</tt> operation
   156      *         is not supported by this map
   157      * @throws ClassCastException if the class of the specified key or value
   158      *         prevents it from being stored in this map
   159      * @throws NullPointerException if the specified key or value is null,
   160      *         and this map does not permit null keys or values
   161      * @throws IllegalArgumentException if some property of the specified key
   162      *         or value prevents it from being stored in this map
   163      */
   164     V replace(K key, V value);
   165 }