1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
import java.util.Map; |
9 |
|
10 |
/** |
11 |
* A {@link java.util.Map} providing additional atomic |
12 |
* <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods. |
13 |
* |
14 |
* <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 |
* <p>This interface is a member of the |
22 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
23 |
* Java Collections Framework</a>. |
24 |
* |
25 |
* @since 1.5 |
26 |
* @author Doug Lea |
27 |
* @param <K> the type of keys maintained by this map |
28 |
* @param <V> the type of mapped values |
29 |
*/ |
30 |
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 |
* if (!map.containsKey(key)) |
37 |
* return map.put(key, value); |
38 |
* else |
39 |
* return map.get(key);</pre> |
40 |
* except that the action is performed atomically. |
41 |
* |
42 |
* @param key key with which the specified value is to be associated |
43 |
* @param value value to be associated with the specified key |
44 |
* @return the previous value associated with the specified key, or |
45 |
* <tt>null</tt> if there was no mapping for the key. |
46 |
* (A <tt>null</tt> return can also indicate that the map |
47 |
* previously associated <tt>null</tt> with the key, |
48 |
* if the implementation supports null values.) |
49 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
50 |
* is not supported by this map |
51 |
* @throws ClassCastException if the class of the specified key or value |
52 |
* 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 |
* |
58 |
*/ |
59 |
V putIfAbsent(K key, V value); |
60 |
|
61 |
/** |
62 |
* Removes the entry for a key only if currently mapped to a given value. |
63 |
* This is equivalent to |
64 |
* <pre> |
65 |
* if (map.containsKey(key) && map.get(key).equals(value)) { |
66 |
* map.remove(key); |
67 |
* return true; |
68 |
* } else return false;</pre> |
69 |
* except that the action is performed atomically. |
70 |
* |
71 |
* @param key key with which the specified value is associated |
72 |
* @param value value expected to be associated with the specified key |
73 |
* @return <tt>true</tt> if the value was removed |
74 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
75 |
* is not supported by this map |
76 |
* @throws ClassCastException if the key or value is of an inappropriate |
77 |
* type for this map |
78 |
* (<a href="../Collection.html#optional-restrictions">optional</a>) |
79 |
* @throws NullPointerException if the specified key or value is null, |
80 |
* and this map does not permit null keys or values |
81 |
* (<a href="../Collection.html#optional-restrictions">optional</a>) |
82 |
*/ |
83 |
boolean remove(Object key, Object value); |
84 |
|
85 |
/** |
86 |
* Replaces the entry for a key only if currently mapped to a given value. |
87 |
* This is equivalent to |
88 |
* <pre> |
89 |
* if (map.containsKey(key) && map.get(key).equals(oldValue)) { |
90 |
* map.put(key, newValue); |
91 |
* return true; |
92 |
* } else return false;</pre> |
93 |
* except that the action is performed atomically. |
94 |
* |
95 |
* @param key key with which the specified value is associated |
96 |
* @param oldValue value expected to be associated with the specified key |
97 |
* @param newValue value to be associated with the specified key |
98 |
* @return <tt>true</tt> if the value was replaced |
99 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
100 |
* is not supported by this map |
101 |
* @throws ClassCastException if the class of a specified key or value |
102 |
* prevents it from being stored in this map |
103 |
* @throws NullPointerException if a specified key or value is null, |
104 |
* and this map does not permit null keys or values |
105 |
* @throws IllegalArgumentException if some property of a specified key |
106 |
* or value prevents it from being stored in this map |
107 |
*/ |
108 |
boolean replace(K key, V oldValue, V newValue); |
109 |
|
110 |
/** |
111 |
* Replaces the entry for a key only if currently mapped to some value. |
112 |
* This is equivalent to |
113 |
* <pre> |
114 |
* if (map.containsKey(key)) { |
115 |
* return map.put(key, value); |
116 |
* } else return null;</pre> |
117 |
* except that the action is performed atomically. |
118 |
* |
119 |
* @param key key with which the specified value is associated |
120 |
* @param value value to be associated with the specified key |
121 |
* @return the previous value associated with the specified key, or |
122 |
* <tt>null</tt> if there was no mapping for the key. |
123 |
* (A <tt>null</tt> return can also indicate that the map |
124 |
* previously associated <tt>null</tt> with the key, |
125 |
* if the implementation supports null values.) |
126 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
127 |
* is not supported by this map |
128 |
* @throws ClassCastException if the class of the specified key or value |
129 |
* prevents it from being stored in this map |
130 |
* @throws NullPointerException if the specified key or value is null, |
131 |
* and this map does not permit null keys or values |
132 |
* @throws IllegalArgumentException if some property of the specified key |
133 |
* or value prevents it from being stored in this map |
134 |
*/ |
135 |
V replace(K key, V value); |
136 |
} |