ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentMap.java
Revision: 1.30
Committed: Sun May 28 23:36:29 2006 UTC (18 years ago) by jsr166
Branch: MAIN
Changes since 1.29: +1 -1 lines
Log Message:
Location of Collections Guide has changed

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.17 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.2 */
6    
7 tim 1.1 package java.util.concurrent;
8     import java.util.Map;
9    
10     /**
11 dl 1.11 * A {@link java.util.Map} providing additional atomic
12 dl 1.14 * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods.
13 dl 1.19 *
14 jsr166 1.29 * <p>Memory consistency effects: As with other concurrent
15     * collections, actions in a thread prior to placing an object into a
16     * {@code ConcurrentMap} as a key or value
17     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
18     * actions subsequent to the access or removal of that object from
19     * the {@code ConcurrentMap} in another thread.
20     *
21 dl 1.19 * <p>This interface is a member of the
22 jsr166 1.30 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
23 dl 1.19 * Java Collections Framework</a>.
24 jsr166 1.21 *
25 dl 1.4 * @since 1.5
26     * @author Doug Lea
27 dl 1.13 * @param <K> the type of keys maintained by this map
28 jsr166 1.21 * @param <V> the type of mapped values
29 dl 1.4 */
30 tim 1.1 public interface ConcurrentMap<K, V> extends Map<K, V> {
31     /**
32     * If the specified key is not already associated
33     * with a value, associate it with the given value.
34     * This is equivalent to
35     * <pre>
36 jsr166 1.21 * if (!map.containsKey(key))
37     * return map.put(key, value);
38 dl 1.5 * else
39 jsr166 1.23 * return map.get(key);</pre>
40 jsr166 1.21 * except that the action is performed atomically.
41     *
42 jsr166 1.24 * @param key key with which the specified value is to be associated
43     * @param value value to be associated with the specified key
44 jsr166 1.25 * @return the previous value associated with the specified key, or
45     * <tt>null</tt> if there was no mapping for the key.
46 jsr166 1.24 * (A <tt>null</tt> return can also indicate that the map
47 jsr166 1.25 * previously associated <tt>null</tt> with the key,
48     * if the implementation supports null values.)
49 jsr166 1.22 * @throws UnsupportedOperationException if the <tt>put</tt> operation
50 jsr166 1.24 * is not supported by this map
51 tim 1.1 * @throws ClassCastException if the class of the specified key or value
52 jsr166 1.24 * prevents it from being stored in this map
53     * @throws NullPointerException if the specified key or value is null,
54     * and this map does not permit null keys or values
55     * @throws IllegalArgumentException if some property of the specified key
56     * or value prevents it from being stored in this map
57 tim 1.1 *
58 dl 1.18 */
59 dl 1.4 V putIfAbsent(K key, V value);
60 dl 1.6
61     /**
62 jsr166 1.21 * Removes the entry for a key only if currently mapped to a given value.
63     * This is equivalent to
64     * <pre>
65 jsr166 1.24 * if (map.containsKey(key) &amp;&amp; map.get(key).equals(value)) {
66 jsr166 1.21 * map.remove(key);
67     * return true;
68 jsr166 1.23 * } else return false;</pre>
69 dl 1.6 * except that the action is performed atomically.
70 jsr166 1.21 *
71 jsr166 1.24 * @param key key with which the specified value is associated
72 jsr166 1.25 * @param value value expected to be associated with the specified key
73     * @return <tt>true</tt> if the value was removed
74 jsr166 1.22 * @throws UnsupportedOperationException if the <tt>remove</tt> operation
75 jsr166 1.24 * is not supported by this map
76 jsr166 1.25 * @throws ClassCastException if the key or value is of an inappropriate
77     * type for this map (optional)
78 jsr166 1.24 * @throws NullPointerException if the specified key or value is null,
79 jsr166 1.25 * and this map does not permit null keys or values (optional)
80 dl 1.6 */
81 dl 1.7 boolean remove(Object key, Object value);
82 dl 1.14
83     /**
84 jsr166 1.21 * Replaces the entry for a key only if currently mapped to a given value.
85     * This is equivalent to
86     * <pre>
87 jsr166 1.24 * if (map.containsKey(key) &amp;&amp; map.get(key).equals(oldValue)) {
88 jsr166 1.21 * map.put(key, newValue);
89     * return true;
90 jsr166 1.23 * } else return false;</pre>
91 dl 1.14 * except that the action is performed atomically.
92 jsr166 1.21 *
93 jsr166 1.24 * @param key key with which the specified value is associated
94     * @param oldValue value expected to be associated with the specified key
95     * @param newValue value to be associated with the specified key
96 jsr166 1.25 * @return <tt>true</tt> if the value was replaced
97 jsr166 1.22 * @throws UnsupportedOperationException if the <tt>put</tt> operation
98 jsr166 1.24 * is not supported by this map
99 jsr166 1.25 * @throws ClassCastException if the class of a specified key or value
100     * prevents it from being stored in this map
101 jsr166 1.24 * @throws NullPointerException if a specified key or value is null,
102     * and this map does not permit null keys or values
103 jsr166 1.25 * @throws IllegalArgumentException if some property of a specified key
104     * or value prevents it from being stored in this map
105 dl 1.14 */
106     boolean replace(K key, V oldValue, V newValue);
107 dl 1.6
108 dl 1.15 /**
109 jsr166 1.21 * Replaces the entry for a key only if currently mapped to some value.
110     * This is equivalent to
111     * <pre>
112     * if (map.containsKey(key)) {
113     * return map.put(key, value);
114 jsr166 1.23 * } else return null;</pre>
115 dl 1.15 * except that the action is performed atomically.
116 jsr166 1.21 *
117 jsr166 1.24 * @param key key with which the specified value is associated
118     * @param value value to be associated with the specified key
119 jsr166 1.25 * @return the previous value associated with the specified key, or
120     * <tt>null</tt> if there was no mapping for the key.
121 jsr166 1.24 * (A <tt>null</tt> return can also indicate that the map
122 jsr166 1.25 * previously associated <tt>null</tt> with the key,
123     * if the implementation supports null values.)
124 jsr166 1.22 * @throws UnsupportedOperationException if the <tt>put</tt> operation
125 jsr166 1.24 * is not supported by this map
126 jsr166 1.25 * @throws ClassCastException if the class of the specified key or value
127     * prevents it from being stored in this map
128 jsr166 1.24 * @throws NullPointerException if the specified key or value is null,
129     * and this map does not permit null keys or values
130 jsr166 1.25 * @throws IllegalArgumentException if some property of the specified key
131     * or value prevents it from being stored in this map
132 dl 1.15 */
133 dl 1.16 V replace(K key, V value);
134 tim 1.1 }