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/licenses/publicdomain |
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>This interface is a member of the |
15 |
* <a href="{@docRoot}/../guide/collections/index.html"> |
16 |
* Java Collections Framework</a>. |
17 |
* |
18 |
* @since 1.5 |
19 |
* @author Doug Lea |
20 |
* @param <K> the type of keys maintained by this map |
21 |
* @param <V> the type of mapped values |
22 |
*/ |
23 |
public interface ConcurrentMap<K, V> extends Map<K, V> { |
24 |
/** |
25 |
* If the specified key is not already associated |
26 |
* with a value, associate it with the given value. |
27 |
* This is equivalent to |
28 |
* <pre> |
29 |
* if (!map.containsKey(key)) |
30 |
* return map.put(key, value); |
31 |
* else |
32 |
* return map.get(key);</pre> |
33 |
* except that the action is performed atomically. |
34 |
* |
35 |
* @param key key with which the specified value is to be associated |
36 |
* @param value value to be associated with the specified key |
37 |
* @return the previous value associated with <tt>key</tt>, or |
38 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
39 |
* (A <tt>null</tt> return can also indicate that the map |
40 |
* previously associated <tt>null</tt> with <tt>key</tt>, |
41 |
* if the implementation supports <tt>null</tt> values.) |
42 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
43 |
* is not supported by this map |
44 |
* @throws ClassCastException if the class of the specified key or value |
45 |
* prevents it from being stored in this map |
46 |
* @throws NullPointerException if the specified key or value is null, |
47 |
* and this map does not permit null keys or values |
48 |
* @throws IllegalArgumentException if some property of the specified key |
49 |
* or value prevents it from being stored in this map |
50 |
* |
51 |
*/ |
52 |
V putIfAbsent(K key, V value); |
53 |
|
54 |
/** |
55 |
* Removes the entry for a key only if currently mapped to a given value. |
56 |
* This is equivalent to |
57 |
* <pre> |
58 |
* if (map.containsKey(key) && map.get(key).equals(value)) { |
59 |
* map.remove(key); |
60 |
* return true; |
61 |
* } else return false;</pre> |
62 |
* except that the action is performed atomically. |
63 |
* |
64 |
* @param key key with which the specified value is associated |
65 |
* @param value value associated with the specified key |
66 |
* @return true if the value was removed, false otherwise |
67 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation |
68 |
* is not supported by 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 |
*/ |
72 |
boolean remove(Object key, Object value); |
73 |
|
74 |
/** |
75 |
* Replaces the entry for a key only if currently mapped to a given value. |
76 |
* This is equivalent to |
77 |
* <pre> |
78 |
* if (map.containsKey(key) && map.get(key).equals(oldValue)) { |
79 |
* map.put(key, newValue); |
80 |
* return true; |
81 |
* } else return false;</pre> |
82 |
* except that the action is performed atomically. |
83 |
* |
84 |
* @param key key with which the specified value is associated |
85 |
* @param oldValue value expected to be associated with the specified key |
86 |
* @param newValue value to be associated with the specified key |
87 |
* @return true if the value was replaced |
88 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
89 |
* is not supported by this map |
90 |
* @throws NullPointerException if a specified key or value is null, |
91 |
* and this map does not permit null keys or values |
92 |
*/ |
93 |
boolean replace(K key, V oldValue, V newValue); |
94 |
|
95 |
/** |
96 |
* Replaces the entry for a key only if currently mapped to some value. |
97 |
* This is equivalent to |
98 |
* <pre> |
99 |
* if (map.containsKey(key)) { |
100 |
* return map.put(key, value); |
101 |
* } else return null;</pre> |
102 |
* except that the action is performed atomically. |
103 |
* |
104 |
* @param key key with which the specified value is associated |
105 |
* @param value value to be associated with the specified key |
106 |
* @return the previous value associated with <tt>key</tt>, or |
107 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
108 |
* (A <tt>null</tt> return can also indicate that the map |
109 |
* previously associated <tt>null</tt> with <tt>key</tt>, |
110 |
* if the implementation supports <tt>null</tt> values.) |
111 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation |
112 |
* is not supported by this map |
113 |
* @throws NullPointerException if the specified key or value is null, |
114 |
* and this map does not permit null keys or values |
115 |
*/ |
116 |
V replace(K key, V value); |
117 |
|
118 |
} |