001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file:
030 *
031 * Written by Doug Lea with assistance from members of JCP JSR-166
032 * Expert Group and released to the public domain, as explained at
033 * http://creativecommons.org/licenses/publicdomain
034 */
035
036 package java.util.concurrent;
037
038 import java.util.Map;
039
040 /**
041 * A {@link java.util.Map} providing additional atomic
042 * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods.
043 *
044 * <p>Memory consistency effects: As with other concurrent
045 * collections, actions in a thread prior to placing an object into a
046 * {@code ConcurrentMap} as a key or value
047 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
048 * actions subsequent to the access or removal of that object from
049 * the {@code ConcurrentMap} in another thread.
050 *
051 * <p>This interface is a member of the
052 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
053 * Java Collections Framework</a>.
054 *
055 * @since 1.5
056 * @author Doug Lea
057 * @param <K> the type of keys maintained by this map
058 * @param <V> the type of mapped values
059 */
060 public interface ConcurrentMap<K, V> extends Map<K, V> {
061 /**
062 * If the specified key is not already associated
063 * with a value, associate it with the given value.
064 * This is equivalent to
065 * <pre>
066 * if (!map.containsKey(key))
067 * return map.put(key, value);
068 * else
069 * return map.get(key);</pre>
070 * except that the action is performed atomically.
071 *
072 * @param key key with which the specified value is to be associated
073 * @param value value to be associated with the specified key
074 * @return the previous value associated with the specified key, or
075 * <tt>null</tt> if there was no mapping for the key.
076 * (A <tt>null</tt> return can also indicate that the map
077 * previously associated <tt>null</tt> with the key,
078 * if the implementation supports null values.)
079 * @throws UnsupportedOperationException if the <tt>put</tt> operation
080 * is not supported by this map
081 * @throws ClassCastException if the class of the specified key or value
082 * prevents it from being stored in this map
083 * @throws NullPointerException if the specified key or value is null,
084 * and this map does not permit null keys or values
085 * @throws IllegalArgumentException if some property of the specified key
086 * or value prevents it from being stored in this map
087 *
088 */
089 V putIfAbsent(K key, V value);
090
091 /**
092 * Removes the entry for a key only if currently mapped to a given value.
093 * This is equivalent to
094 * <pre>
095 * if (map.containsKey(key) && map.get(key).equals(value)) {
096 * map.remove(key);
097 * return true;
098 * } else return false;</pre>
099 * except that the action is performed atomically.
100 *
101 * @param key key with which the specified value is associated
102 * @param value value expected to be associated with the specified key
103 * @return <tt>true</tt> if the value was removed
104 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
105 * is not supported by this map
106 * @throws ClassCastException if the key or value is of an inappropriate
107 * type for this map (optional)
108 * @throws NullPointerException if the specified key or value is null,
109 * and this map does not permit null keys or values (optional)
110 */
111 boolean remove(Object key, Object value);
112
113 /**
114 * Replaces the entry for a key only if currently mapped to a given value.
115 * This is equivalent to
116 * <pre>
117 * if (map.containsKey(key) && map.get(key).equals(oldValue)) {
118 * map.put(key, newValue);
119 * return true;
120 * } else return false;</pre>
121 * except that the action is performed atomically.
122 *
123 * @param key key with which the specified value is associated
124 * @param oldValue value expected to be associated with the specified key
125 * @param newValue value to be associated with the specified key
126 * @return <tt>true</tt> if the value was replaced
127 * @throws UnsupportedOperationException if the <tt>put</tt> operation
128 * is not supported by this map
129 * @throws ClassCastException if the class of a specified key or value
130 * prevents it from being stored in this map
131 * @throws NullPointerException if a specified key or value is null,
132 * and this map does not permit null keys or values
133 * @throws IllegalArgumentException if some property of a specified key
134 * or value prevents it from being stored in this map
135 */
136 boolean replace(K key, V oldValue, V newValue);
137
138 /**
139 * Replaces the entry for a key only if currently mapped to some value.
140 * This is equivalent to
141 * <pre>
142 * if (map.containsKey(key)) {
143 * return map.put(key, value);
144 * } else return null;</pre>
145 * except that the action is performed atomically.
146 *
147 * @param key key with which the specified value is associated
148 * @param value value to be associated with the specified key
149 * @return the previous value associated with the specified key, or
150 * <tt>null</tt> if there was no mapping for the key.
151 * (A <tt>null</tt> return can also indicate that the map
152 * previously associated <tt>null</tt> with the key,
153 * if the implementation supports null values.)
154 * @throws UnsupportedOperationException if the <tt>put</tt> operation
155 * is not supported by this map
156 * @throws ClassCastException if the class of the specified key or value
157 * prevents it from being stored in this map
158 * @throws NullPointerException if the specified key or value is null,
159 * and this map does not permit null keys or values
160 * @throws IllegalArgumentException if some property of the specified key
161 * or value prevents it from being stored in this map
162 */
163 V replace(K key, V value);
164 }
|