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 thread safety and atomicity |
12 |
* guarantees. |
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 |
/** |
33 |
* {@inheritDoc} |
34 |
* |
35 |
* @implNote This implementation assumes that the ConcurrentMap cannot |
36 |
* contain null values and get() returning null unambiguously means the key |
37 |
* is absent. Implementations which support null values must override this |
38 |
* default implementation. |
39 |
*/ |
40 |
@Override |
41 |
default V getOrDefault(Object key, V defaultValue) { |
42 |
V v; |
43 |
return ((v = get(key)) != null) ? v : defaultValue; |
44 |
} |
45 |
|
46 |
/** |
47 |
* If the specified key is not already associated |
48 |
* with a value, associate it with the given value. |
49 |
* This is equivalent to |
50 |
* <pre> {@code |
51 |
* if (!map.containsKey(key)) |
52 |
* return map.put(key, value); |
53 |
* else |
54 |
* return map.get(key);}</pre> |
55 |
* |
56 |
* except that the action is performed atomically. |
57 |
* |
58 |
* @param key key with which the specified value is to be associated |
59 |
* @param value value to be associated with the specified key |
60 |
* @return the previous value associated with the specified key, or |
61 |
* {@code null} if there was no mapping for the key. |
62 |
* (A {@code null} return can also indicate that the map |
63 |
* previously associated {@code null} with the key, |
64 |
* if the implementation supports null values.) |
65 |
* @throws UnsupportedOperationException if the {@code put} operation |
66 |
* is not supported by this map |
67 |
* @throws ClassCastException if the class of the specified key or value |
68 |
* prevents it from being stored in this map |
69 |
* @throws NullPointerException if the specified key or value is null, |
70 |
* and this map does not permit null keys or values |
71 |
* @throws IllegalArgumentException if some property of the specified key |
72 |
* or value prevents it from being stored in this map |
73 |
*/ |
74 |
V putIfAbsent(K key, V value); |
75 |
|
76 |
/** |
77 |
* Removes the entry for a key only if currently mapped to a given value. |
78 |
* This is equivalent to |
79 |
* <pre> {@code |
80 |
* if (map.containsKey(key) && Objects.equals(map.get(key), value)) { |
81 |
* map.remove(key); |
82 |
* return true; |
83 |
* } else |
84 |
* return false;}</pre> |
85 |
* |
86 |
* except that the action is performed atomically. |
87 |
* |
88 |
* @param key key with which the specified value is associated |
89 |
* @param value value expected to be associated with the specified key |
90 |
* @return {@code true} if the value was removed |
91 |
* @throws UnsupportedOperationException if the {@code remove} operation |
92 |
* is not supported by this map |
93 |
* @throws ClassCastException if the key or value is of an inappropriate |
94 |
* type for this map |
95 |
* (<a href="../Collection.html#optional-restrictions">optional</a>) |
96 |
* @throws NullPointerException if the specified key or value is null, |
97 |
* and this map does not permit null keys or values |
98 |
* (<a href="../Collection.html#optional-restrictions">optional</a>) |
99 |
*/ |
100 |
boolean remove(Object key, Object value); |
101 |
|
102 |
/** |
103 |
* Replaces the entry for a key only if currently mapped to a given value. |
104 |
* This is equivalent to |
105 |
* <pre> {@code |
106 |
* if (map.containsKey(key) && Objects.equals(map.get(key), oldValue)) { |
107 |
* map.put(key, newValue); |
108 |
* return true; |
109 |
* } else |
110 |
* return false;}</pre> |
111 |
* |
112 |
* except that the action is performed atomically. |
113 |
* |
114 |
* @param key key with which the specified value is associated |
115 |
* @param oldValue value expected to be associated with the specified key |
116 |
* @param newValue value to be associated with the specified key |
117 |
* @return {@code true} if the value was replaced |
118 |
* @throws UnsupportedOperationException if the {@code put} operation |
119 |
* is not supported by this map |
120 |
* @throws ClassCastException if the class of a specified key or value |
121 |
* prevents it from being stored in this map |
122 |
* @throws NullPointerException if a specified key or value is null, |
123 |
* and this map does not permit null keys or values |
124 |
* @throws IllegalArgumentException if some property of a specified key |
125 |
* or value prevents it from being stored in this map |
126 |
*/ |
127 |
boolean replace(K key, V oldValue, V newValue); |
128 |
|
129 |
/** |
130 |
* Replaces the entry for a key only if currently mapped to some value. |
131 |
* This is equivalent to |
132 |
* <pre> {@code |
133 |
* if (map.containsKey(key)) { |
134 |
* return map.put(key, value); |
135 |
* } else |
136 |
* return null;}</pre> |
137 |
* |
138 |
* except that the action is performed atomically. |
139 |
* |
140 |
* @param key key with which the specified value is associated |
141 |
* @param value value to be associated with the specified key |
142 |
* @return the previous value associated with the specified key, or |
143 |
* {@code null} if there was no mapping for the key. |
144 |
* (A {@code null} return can also indicate that the map |
145 |
* previously associated {@code null} with the key, |
146 |
* if the implementation supports null values.) |
147 |
* @throws UnsupportedOperationException if the {@code put} operation |
148 |
* is not supported by this map |
149 |
* @throws ClassCastException if the class of the specified key or value |
150 |
* prevents it from being stored in this map |
151 |
* @throws NullPointerException if the specified key or value is null, |
152 |
* and this map does not permit null keys or values |
153 |
* @throws IllegalArgumentException if some property of the specified key |
154 |
* or value prevents it from being stored in this map |
155 |
*/ |
156 |
V replace(K key, V value); |
157 |
} |