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