ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentMap.java
Revision: 1.28
Committed: Thu Sep 8 21:58:22 2005 UTC (18 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.27: +2 -2 lines
Log Message:
whitespace

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