jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
3 |
*
|
jtulach@1334
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
9 |
*
|
jtulach@1334
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
14 |
* accompanied this code).
|
jtulach@1334
|
15 |
*
|
jtulach@1334
|
16 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
19 |
*
|
jtulach@1334
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
22 |
* questions.
|
jtulach@1334
|
23 |
*/
|
jtulach@1334
|
24 |
|
jtulach@1334
|
25 |
/*
|
jtulach@1334
|
26 |
* This file is available under and governed by the GNU General Public
|
jtulach@1334
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
jtulach@1334
|
28 |
* However, the following notice accompanied the original version of this
|
jtulach@1334
|
29 |
* file:
|
jtulach@1334
|
30 |
*
|
jtulach@1334
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
jtulach@1334
|
32 |
* Expert Group and released to the public domain, as explained at
|
jtulach@1334
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
jtulach@1334
|
34 |
*/
|
jtulach@1334
|
35 |
|
jtulach@1334
|
36 |
package java.util.concurrent;
|
jtulach@1334
|
37 |
import java.util.Map;
|
jtulach@1334
|
38 |
|
jtulach@1334
|
39 |
/**
|
jtulach@1334
|
40 |
* A {@link java.util.Map} providing additional atomic
|
jtulach@1334
|
41 |
* <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods.
|
jtulach@1334
|
42 |
*
|
jtulach@1334
|
43 |
* <p>Memory consistency effects: As with other concurrent
|
jtulach@1334
|
44 |
* collections, actions in a thread prior to placing an object into a
|
jtulach@1334
|
45 |
* {@code ConcurrentMap} as a key or value
|
jtulach@1334
|
46 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
|
jtulach@1334
|
47 |
* actions subsequent to the access or removal of that object from
|
jtulach@1334
|
48 |
* the {@code ConcurrentMap} in another thread.
|
jtulach@1334
|
49 |
*
|
jtulach@1334
|
50 |
* <p>This interface is a member of the
|
jtulach@1334
|
51 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
jtulach@1334
|
52 |
* Java Collections Framework</a>.
|
jtulach@1334
|
53 |
*
|
jtulach@1334
|
54 |
* @since 1.5
|
jtulach@1334
|
55 |
* @author Doug Lea
|
jtulach@1334
|
56 |
* @param <K> the type of keys maintained by this map
|
jtulach@1334
|
57 |
* @param <V> the type of mapped values
|
jtulach@1334
|
58 |
*/
|
jtulach@1334
|
59 |
public interface ConcurrentMap<K, V> extends Map<K, V> {
|
jtulach@1334
|
60 |
/**
|
jtulach@1334
|
61 |
* If the specified key is not already associated
|
jtulach@1334
|
62 |
* with a value, associate it with the given value.
|
jtulach@1334
|
63 |
* This is equivalent to
|
jtulach@1334
|
64 |
* <pre>
|
jtulach@1334
|
65 |
* if (!map.containsKey(key))
|
jtulach@1334
|
66 |
* return map.put(key, value);
|
jtulach@1334
|
67 |
* else
|
jtulach@1334
|
68 |
* return map.get(key);</pre>
|
jtulach@1334
|
69 |
* except that the action is performed atomically.
|
jtulach@1334
|
70 |
*
|
jtulach@1334
|
71 |
* @param key key with which the specified value is to be associated
|
jtulach@1334
|
72 |
* @param value value to be associated with the specified key
|
jtulach@1334
|
73 |
* @return the previous value associated with the specified key, or
|
jtulach@1334
|
74 |
* <tt>null</tt> if there was no mapping for the key.
|
jtulach@1334
|
75 |
* (A <tt>null</tt> return can also indicate that the map
|
jtulach@1334
|
76 |
* previously associated <tt>null</tt> with the key,
|
jtulach@1334
|
77 |
* if the implementation supports null values.)
|
jtulach@1334
|
78 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation
|
jtulach@1334
|
79 |
* is not supported by this map
|
jtulach@1334
|
80 |
* @throws ClassCastException if the class of the specified key or value
|
jtulach@1334
|
81 |
* prevents it from being stored in this map
|
jtulach@1334
|
82 |
* @throws NullPointerException if the specified key or value is null,
|
jtulach@1334
|
83 |
* and this map does not permit null keys or values
|
jtulach@1334
|
84 |
* @throws IllegalArgumentException if some property of the specified key
|
jtulach@1334
|
85 |
* or value prevents it from being stored in this map
|
jtulach@1334
|
86 |
*
|
jtulach@1334
|
87 |
*/
|
jtulach@1334
|
88 |
V putIfAbsent(K key, V value);
|
jtulach@1334
|
89 |
|
jtulach@1334
|
90 |
/**
|
jtulach@1334
|
91 |
* Removes the entry for a key only if currently mapped to a given value.
|
jtulach@1334
|
92 |
* This is equivalent to
|
jtulach@1334
|
93 |
* <pre>
|
jtulach@1334
|
94 |
* if (map.containsKey(key) && map.get(key).equals(value)) {
|
jtulach@1334
|
95 |
* map.remove(key);
|
jtulach@1334
|
96 |
* return true;
|
jtulach@1334
|
97 |
* } else return false;</pre>
|
jtulach@1334
|
98 |
* except that the action is performed atomically.
|
jtulach@1334
|
99 |
*
|
jtulach@1334
|
100 |
* @param key key with which the specified value is associated
|
jtulach@1334
|
101 |
* @param value value expected to be associated with the specified key
|
jtulach@1334
|
102 |
* @return <tt>true</tt> if the value was removed
|
jtulach@1334
|
103 |
* @throws UnsupportedOperationException if the <tt>remove</tt> operation
|
jtulach@1334
|
104 |
* is not supported by this map
|
jtulach@1334
|
105 |
* @throws ClassCastException if the key or value is of an inappropriate
|
jtulach@1334
|
106 |
* type for this map
|
jtulach@1334
|
107 |
* (<a href="../Collection.html#optional-restrictions">optional</a>)
|
jtulach@1334
|
108 |
* @throws NullPointerException if the specified key or value is null,
|
jtulach@1334
|
109 |
* and this map does not permit null keys or values
|
jtulach@1334
|
110 |
* (<a href="../Collection.html#optional-restrictions">optional</a>)
|
jtulach@1334
|
111 |
*/
|
jtulach@1334
|
112 |
boolean remove(Object key, Object value);
|
jtulach@1334
|
113 |
|
jtulach@1334
|
114 |
/**
|
jtulach@1334
|
115 |
* Replaces the entry for a key only if currently mapped to a given value.
|
jtulach@1334
|
116 |
* This is equivalent to
|
jtulach@1334
|
117 |
* <pre>
|
jtulach@1334
|
118 |
* if (map.containsKey(key) && map.get(key).equals(oldValue)) {
|
jtulach@1334
|
119 |
* map.put(key, newValue);
|
jtulach@1334
|
120 |
* return true;
|
jtulach@1334
|
121 |
* } else return false;</pre>
|
jtulach@1334
|
122 |
* except that the action is performed atomically.
|
jtulach@1334
|
123 |
*
|
jtulach@1334
|
124 |
* @param key key with which the specified value is associated
|
jtulach@1334
|
125 |
* @param oldValue value expected to be associated with the specified key
|
jtulach@1334
|
126 |
* @param newValue value to be associated with the specified key
|
jtulach@1334
|
127 |
* @return <tt>true</tt> if the value was replaced
|
jtulach@1334
|
128 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation
|
jtulach@1334
|
129 |
* is not supported by this map
|
jtulach@1334
|
130 |
* @throws ClassCastException if the class of a specified key or value
|
jtulach@1334
|
131 |
* prevents it from being stored in this map
|
jtulach@1334
|
132 |
* @throws NullPointerException if a specified key or value is null,
|
jtulach@1334
|
133 |
* and this map does not permit null keys or values
|
jtulach@1334
|
134 |
* @throws IllegalArgumentException if some property of a specified key
|
jtulach@1334
|
135 |
* or value prevents it from being stored in this map
|
jtulach@1334
|
136 |
*/
|
jtulach@1334
|
137 |
boolean replace(K key, V oldValue, V newValue);
|
jtulach@1334
|
138 |
|
jtulach@1334
|
139 |
/**
|
jtulach@1334
|
140 |
* Replaces the entry for a key only if currently mapped to some value.
|
jtulach@1334
|
141 |
* This is equivalent to
|
jtulach@1334
|
142 |
* <pre>
|
jtulach@1334
|
143 |
* if (map.containsKey(key)) {
|
jtulach@1334
|
144 |
* return map.put(key, value);
|
jtulach@1334
|
145 |
* } else return null;</pre>
|
jtulach@1334
|
146 |
* except that the action is performed atomically.
|
jtulach@1334
|
147 |
*
|
jtulach@1334
|
148 |
* @param key key with which the specified value is associated
|
jtulach@1334
|
149 |
* @param value value to be associated with the specified key
|
jtulach@1334
|
150 |
* @return the previous value associated with the specified key, or
|
jtulach@1334
|
151 |
* <tt>null</tt> if there was no mapping for the key.
|
jtulach@1334
|
152 |
* (A <tt>null</tt> return can also indicate that the map
|
jtulach@1334
|
153 |
* previously associated <tt>null</tt> with the key,
|
jtulach@1334
|
154 |
* if the implementation supports null values.)
|
jtulach@1334
|
155 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation
|
jtulach@1334
|
156 |
* is not supported by this map
|
jtulach@1334
|
157 |
* @throws ClassCastException if the class of the specified key or value
|
jtulach@1334
|
158 |
* prevents it from being stored in this map
|
jtulach@1334
|
159 |
* @throws NullPointerException if the specified key or value is null,
|
jtulach@1334
|
160 |
* and this map does not permit null keys or values
|
jtulach@1334
|
161 |
* @throws IllegalArgumentException if some property of the specified key
|
jtulach@1334
|
162 |
* or value prevents it from being stored in this map
|
jtulach@1334
|
163 |
*/
|
jtulach@1334
|
164 |
V replace(K key, V value);
|
jtulach@1334
|
165 |
}
|