1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.36 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util.concurrent; |
8 |
dl |
1.10 |
import java.util.concurrent.locks.*; |
9 |
tim |
1.1 |
import java.util.*; |
10 |
|
|
import java.io.Serializable; |
11 |
|
|
import java.io.IOException; |
12 |
|
|
import java.io.ObjectInputStream; |
13 |
|
|
import java.io.ObjectOutputStream; |
14 |
|
|
|
15 |
|
|
/** |
16 |
dl |
1.4 |
* A hash table supporting full concurrency of retrievals and |
17 |
|
|
* adjustable expected concurrency for updates. This class obeys the |
18 |
dl |
1.22 |
* same functional specification as {@link java.util.Hashtable}, and |
19 |
dl |
1.19 |
* includes versions of methods corresponding to each method of |
20 |
dl |
1.25 |
* <tt>Hashtable</tt>. However, even though all operations are |
21 |
dl |
1.19 |
* thread-safe, retrieval operations do <em>not</em> entail locking, |
22 |
|
|
* and there is <em>not</em> any support for locking the entire table |
23 |
|
|
* in a way that prevents all access. This class is fully |
24 |
|
|
* interoperable with <tt>Hashtable</tt> in programs that rely on its |
25 |
dl |
1.4 |
* thread safety but not on its synchronization details. |
26 |
tim |
1.11 |
* |
27 |
dl |
1.25 |
* <p> Retrieval operations (including <tt>get</tt>) generally do not |
28 |
|
|
* block, so may overlap with update operations (including |
29 |
|
|
* <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results |
30 |
|
|
* of the most recently <em>completed</em> update operations holding |
31 |
|
|
* upon their onset. For aggregate operations such as <tt>putAll</tt> |
32 |
|
|
* and <tt>clear</tt>, concurrent retrievals may reflect insertion or |
33 |
dl |
1.4 |
* removal of only some entries. Similarly, Iterators and |
34 |
|
|
* Enumerations return elements reflecting the state of the hash table |
35 |
|
|
* at some point at or since the creation of the iterator/enumeration. |
36 |
dl |
1.25 |
* They do <em>not</em> throw |
37 |
dl |
1.28 |
* {@link ConcurrentModificationException}. However, iterators are |
38 |
dl |
1.25 |
* designed to be used by only one thread at a time. |
39 |
tim |
1.1 |
* |
40 |
dl |
1.19 |
* <p> The allowed concurrency among update operations is guided by |
41 |
|
|
* the optional <tt>concurrencyLevel</tt> constructor argument |
42 |
dl |
1.21 |
* (default 16), which is used as a hint for internal sizing. The |
43 |
|
|
* table is internally partitioned to try to permit the indicated |
44 |
|
|
* number of concurrent updates without contention. Because placement |
45 |
|
|
* in hash tables is essentially random, the actual concurrency will |
46 |
|
|
* vary. Ideally, you should choose a value to accommodate as many |
47 |
dl |
1.25 |
* threads as will ever concurrently modify the table. Using a |
48 |
dl |
1.21 |
* significantly higher value than you need can waste space and time, |
49 |
|
|
* and a significantly lower value can lead to thread contention. But |
50 |
|
|
* overestimates and underestimates within an order of magnitude do |
51 |
dl |
1.25 |
* not usually have much noticeable impact. A value of one is |
52 |
dl |
1.45 |
* appropriate when it is known that only one thread will modify and |
53 |
|
|
* all others will only read. Also, resizing this or any other kind of |
54 |
|
|
* hash table is a relatively slow operation, so, when possible, it is |
55 |
|
|
* a good idea to provide estimates of expected table sizes in |
56 |
|
|
* constructors. |
57 |
tim |
1.1 |
* |
58 |
dl |
1.45 |
* <p>This class and its views and iterators implement all of the |
59 |
|
|
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
60 |
|
|
* interfaces. |
61 |
dl |
1.23 |
* |
62 |
dl |
1.22 |
* <p> Like {@link java.util.Hashtable} but unlike {@link |
63 |
|
|
* java.util.HashMap}, this class does NOT allow <tt>null</tt> to be |
64 |
|
|
* used as a key or value. |
65 |
tim |
1.1 |
* |
66 |
dl |
1.42 |
* <p>This class is a member of the |
67 |
|
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
68 |
|
|
* Java Collections Framework</a>. |
69 |
|
|
* |
70 |
dl |
1.8 |
* @since 1.5 |
71 |
|
|
* @author Doug Lea |
72 |
dl |
1.27 |
* @param <K> the type of keys maintained by this map |
73 |
|
|
* @param <V> the type of mapped values |
74 |
dl |
1.8 |
*/ |
75 |
tim |
1.1 |
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> |
76 |
|
|
implements ConcurrentMap<K, V>, Cloneable, Serializable { |
77 |
dl |
1.20 |
private static final long serialVersionUID = 7249069246763182397L; |
78 |
tim |
1.1 |
|
79 |
|
|
/* |
80 |
dl |
1.4 |
* The basic strategy is to subdivide the table among Segments, |
81 |
|
|
* each of which itself is a concurrently readable hash table. |
82 |
|
|
*/ |
83 |
tim |
1.1 |
|
84 |
dl |
1.4 |
/* ---------------- Constants -------------- */ |
85 |
tim |
1.11 |
|
86 |
dl |
1.4 |
/** |
87 |
dl |
1.19 |
* The default initial number of table slots for this table. |
88 |
dl |
1.4 |
* Used when not otherwise specified in constructor. |
89 |
|
|
*/ |
90 |
dl |
1.41 |
static int DEFAULT_INITIAL_CAPACITY = 16; |
91 |
tim |
1.1 |
|
92 |
|
|
/** |
93 |
dl |
1.4 |
* The maximum capacity, used if a higher value is implicitly |
94 |
|
|
* specified by either of the constructors with arguments. MUST |
95 |
dl |
1.21 |
* be a power of two <= 1<<30 to ensure that entries are indexible |
96 |
|
|
* using ints. |
97 |
dl |
1.4 |
*/ |
98 |
dl |
1.21 |
static final int MAXIMUM_CAPACITY = 1 << 30; |
99 |
tim |
1.11 |
|
100 |
tim |
1.1 |
/** |
101 |
dl |
1.4 |
* The default load factor for this table. Used when not |
102 |
|
|
* otherwise specified in constructor. |
103 |
|
|
*/ |
104 |
tim |
1.11 |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
105 |
tim |
1.1 |
|
106 |
|
|
/** |
107 |
dl |
1.4 |
* The default number of concurrency control segments. |
108 |
tim |
1.1 |
**/ |
109 |
dl |
1.41 |
static final int DEFAULT_SEGMENTS = 16; |
110 |
tim |
1.1 |
|
111 |
dl |
1.21 |
/** |
112 |
dl |
1.37 |
* The maximum number of segments to allow; used to bound |
113 |
|
|
* constructor arguments. |
114 |
dl |
1.21 |
*/ |
115 |
dl |
1.41 |
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative |
116 |
dl |
1.21 |
|
117 |
dl |
1.46 |
/** |
118 |
|
|
* Number of unsynchronized retries in size and containsValue |
119 |
|
|
* methods before resorting to locking. This is used to avoid |
120 |
|
|
* unbounded retries if tables undergo continuous modification |
121 |
|
|
* which would make it impossible to obtain an accurate result. |
122 |
|
|
*/ |
123 |
|
|
static final int RETRIES_BEFORE_LOCK = 2; |
124 |
|
|
|
125 |
dl |
1.4 |
/* ---------------- Fields -------------- */ |
126 |
tim |
1.1 |
|
127 |
|
|
/** |
128 |
dl |
1.9 |
* Mask value for indexing into segments. The upper bits of a |
129 |
|
|
* key's hash code are used to choose the segment. |
130 |
tim |
1.1 |
**/ |
131 |
dl |
1.41 |
final int segmentMask; |
132 |
tim |
1.1 |
|
133 |
|
|
/** |
134 |
dl |
1.4 |
* Shift value for indexing within segments. |
135 |
tim |
1.1 |
**/ |
136 |
dl |
1.41 |
final int segmentShift; |
137 |
tim |
1.1 |
|
138 |
|
|
/** |
139 |
dl |
1.4 |
* The segments, each of which is a specialized hash table |
140 |
tim |
1.1 |
*/ |
141 |
dl |
1.41 |
final Segment[] segments; |
142 |
dl |
1.4 |
|
143 |
dl |
1.41 |
transient Set<K> keySet; |
144 |
|
|
transient Set<Map.Entry<K,V>> entrySet; |
145 |
|
|
transient Collection<V> values; |
146 |
dl |
1.4 |
|
147 |
|
|
/* ---------------- Small Utilities -------------- */ |
148 |
tim |
1.1 |
|
149 |
|
|
/** |
150 |
dl |
1.44 |
* Returns a hash code for non-null Object x. |
151 |
dl |
1.37 |
* Uses the same hash code spreader as most other java.util hash tables. |
152 |
dl |
1.8 |
* @param x the object serving as a key |
153 |
|
|
* @return the hash code |
154 |
tim |
1.1 |
*/ |
155 |
dl |
1.41 |
static int hash(Object x) { |
156 |
dl |
1.4 |
int h = x.hashCode(); |
157 |
|
|
h += ~(h << 9); |
158 |
|
|
h ^= (h >>> 14); |
159 |
|
|
h += (h << 4); |
160 |
|
|
h ^= (h >>> 10); |
161 |
|
|
return h; |
162 |
|
|
} |
163 |
|
|
|
164 |
tim |
1.1 |
/** |
165 |
dl |
1.44 |
* Returns the segment that should be used for key with given hash |
166 |
|
|
* @param hash the hash code for the key |
167 |
|
|
* @return the segment |
168 |
tim |
1.1 |
*/ |
169 |
dl |
1.41 |
final Segment<K,V> segmentFor(int hash) { |
170 |
tim |
1.12 |
return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask]; |
171 |
dl |
1.4 |
} |
172 |
tim |
1.1 |
|
173 |
dl |
1.4 |
/* ---------------- Inner Classes -------------- */ |
174 |
tim |
1.1 |
|
175 |
|
|
/** |
176 |
dl |
1.46 |
* ConcurrentHashMap list entry. Note that this is never exported |
177 |
|
|
* out as a user-visible Map.Entry. |
178 |
|
|
* |
179 |
|
|
* Because the value field is volatile, not final, it is legal wrt |
180 |
|
|
* the Java Memory Model for an unsynchronized reader to see null |
181 |
|
|
* instead of initial value when read via a data race. Although a |
182 |
|
|
* reordering leading to this is not likely to ever actually |
183 |
|
|
* occur, the Segment.readValueUnderLock method is used as a |
184 |
|
|
* backup in case a null (pre-initialized) value is ever seen in |
185 |
|
|
* an unsynchronized access method. |
186 |
|
|
*/ |
187 |
|
|
static final class HashEntry<K,V> { |
188 |
|
|
final K key; |
189 |
|
|
final int hash; |
190 |
|
|
volatile V value; |
191 |
|
|
final HashEntry<K,V> next; |
192 |
|
|
|
193 |
|
|
HashEntry(K key, int hash, HashEntry<K,V> next, V value) { |
194 |
|
|
this.key = key; |
195 |
|
|
this.hash = hash; |
196 |
|
|
this.next = next; |
197 |
|
|
this.value = value; |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
/** |
202 |
dl |
1.6 |
* Segments are specialized versions of hash tables. This |
203 |
dl |
1.4 |
* subclasses from ReentrantLock opportunistically, just to |
204 |
|
|
* simplify some locking and avoid separate construction. |
205 |
tim |
1.1 |
**/ |
206 |
dl |
1.41 |
static final class Segment<K,V> extends ReentrantLock implements Serializable { |
207 |
dl |
1.4 |
/* |
208 |
|
|
* Segments maintain a table of entry lists that are ALWAYS |
209 |
|
|
* kept in a consistent state, so can be read without locking. |
210 |
|
|
* Next fields of nodes are immutable (final). All list |
211 |
|
|
* additions are performed at the front of each bin. This |
212 |
|
|
* makes it easy to check changes, and also fast to traverse. |
213 |
|
|
* When nodes would otherwise be changed, new nodes are |
214 |
|
|
* created to replace them. This works well for hash tables |
215 |
|
|
* since the bin lists tend to be short. (The average length |
216 |
|
|
* is less than two for the default load factor threshold.) |
217 |
|
|
* |
218 |
|
|
* Read operations can thus proceed without locking, but rely |
219 |
dl |
1.45 |
* on selected uses of volatiles to ensure that completed |
220 |
|
|
* write operations performed by other threads are |
221 |
|
|
* noticed. For most purposes, the "count" field, tracking the |
222 |
|
|
* number of elements, serves as that volatile variable |
223 |
|
|
* ensuring visibility. This is convenient because this field |
224 |
|
|
* needs to be read in many read operations anyway: |
225 |
dl |
1.4 |
* |
226 |
dl |
1.45 |
* - All (unsynchronized) read operations must first read the |
227 |
dl |
1.4 |
* "count" field, and should not look at table entries if |
228 |
|
|
* it is 0. |
229 |
tim |
1.11 |
* |
230 |
dl |
1.45 |
* - All (synchronized) write operations should write to |
231 |
|
|
* the "count" field after structurally changing any bin. |
232 |
|
|
* The operations must not take any action that could even |
233 |
|
|
* momentarily cause a concurrent read operation to see |
234 |
|
|
* inconsistent data. This is made easier by the nature of |
235 |
|
|
* the read operations in Map. For example, no operation |
236 |
dl |
1.4 |
* can reveal that the table has grown but the threshold |
237 |
|
|
* has not yet been updated, so there are no atomicity |
238 |
|
|
* requirements for this with respect to reads. |
239 |
|
|
* |
240 |
dl |
1.45 |
* As a guide, all critical volatile reads and writes to the |
241 |
|
|
* count field are marked in code comments. |
242 |
dl |
1.4 |
*/ |
243 |
tim |
1.11 |
|
244 |
dl |
1.24 |
private static final long serialVersionUID = 2249069246763182397L; |
245 |
|
|
|
246 |
dl |
1.4 |
/** |
247 |
|
|
* The number of elements in this segment's region. |
248 |
|
|
**/ |
249 |
|
|
transient volatile int count; |
250 |
|
|
|
251 |
|
|
/** |
252 |
dl |
1.45 |
* Number of updates that alter the size of the table. This is |
253 |
|
|
* used during bulk-read methods to make sure they see a |
254 |
|
|
* consistent snapshot: If modCounts change during a traversal |
255 |
dl |
1.46 |
* of segments computing size or checking containsValue, then |
256 |
dl |
1.45 |
* we might have an inconsistent view of state so (usually) |
257 |
|
|
* must retry. |
258 |
dl |
1.21 |
*/ |
259 |
|
|
transient int modCount; |
260 |
|
|
|
261 |
|
|
/** |
262 |
dl |
1.4 |
* The table is rehashed when its size exceeds this threshold. |
263 |
|
|
* (The value of this field is always (int)(capacity * |
264 |
|
|
* loadFactor).) |
265 |
|
|
*/ |
266 |
dl |
1.41 |
transient int threshold; |
267 |
dl |
1.4 |
|
268 |
|
|
/** |
269 |
dl |
1.45 |
* The per-segment table. Declared as a raw type, casted |
270 |
|
|
* to HashEntry<K,V> on each use. |
271 |
dl |
1.4 |
*/ |
272 |
dl |
1.45 |
transient volatile HashEntry[] table; |
273 |
dl |
1.4 |
|
274 |
|
|
/** |
275 |
|
|
* The load factor for the hash table. Even though this value |
276 |
|
|
* is same for all segments, it is replicated to avoid needing |
277 |
|
|
* links to outer object. |
278 |
|
|
* @serial |
279 |
|
|
*/ |
280 |
dl |
1.41 |
final float loadFactor; |
281 |
tim |
1.1 |
|
282 |
dl |
1.4 |
Segment(int initialCapacity, float lf) { |
283 |
|
|
loadFactor = lf; |
284 |
tim |
1.11 |
setTable(new HashEntry[initialCapacity]); |
285 |
dl |
1.4 |
} |
286 |
tim |
1.1 |
|
287 |
dl |
1.4 |
/** |
288 |
tim |
1.11 |
* Set table to new HashEntry array. |
289 |
dl |
1.4 |
* Call only while holding lock or in constructor. |
290 |
|
|
**/ |
291 |
dl |
1.41 |
void setTable(HashEntry[] newTable) { |
292 |
dl |
1.45 |
threshold = (int)(newTable.length * loadFactor); |
293 |
dl |
1.4 |
table = newTable; |
294 |
dl |
1.45 |
} |
295 |
|
|
|
296 |
|
|
/** |
297 |
|
|
* Return properly casted first entry of bin for given hash |
298 |
|
|
*/ |
299 |
|
|
HashEntry<K,V> getFirst(int hash) { |
300 |
|
|
HashEntry[] tab = table; |
301 |
|
|
return (HashEntry<K,V>) tab[hash & (tab.length - 1)]; |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
/** |
305 |
|
|
* Read value field of an entry under lock. Called if value |
306 |
|
|
* field ever appears to be null. This is possible only if a |
307 |
|
|
* compiler happens to reorder a HashEntry initialization with |
308 |
|
|
* its table assignment, which is legal under memory model |
309 |
|
|
* but is not known to ever occur. |
310 |
|
|
*/ |
311 |
|
|
V readValueUnderLock(HashEntry<K,V> e) { |
312 |
|
|
lock(); |
313 |
|
|
try { |
314 |
|
|
return e.value; |
315 |
|
|
} finally { |
316 |
|
|
unlock(); |
317 |
|
|
} |
318 |
tim |
1.11 |
} |
319 |
dl |
1.4 |
|
320 |
|
|
/* Specialized implementations of map methods */ |
321 |
tim |
1.11 |
|
322 |
dl |
1.29 |
V get(Object key, int hash) { |
323 |
dl |
1.4 |
if (count != 0) { // read-volatile |
324 |
dl |
1.45 |
HashEntry<K,V> e = getFirst(hash); |
325 |
dl |
1.4 |
while (e != null) { |
326 |
dl |
1.45 |
if (e.hash == hash && key.equals(e.key)) { |
327 |
|
|
V v = e.value; |
328 |
|
|
if (v != null) |
329 |
|
|
return v; |
330 |
|
|
return readValueUnderLock(e); // recheck |
331 |
|
|
} |
332 |
dl |
1.4 |
e = e.next; |
333 |
|
|
} |
334 |
|
|
} |
335 |
|
|
return null; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
boolean containsKey(Object key, int hash) { |
339 |
|
|
if (count != 0) { // read-volatile |
340 |
dl |
1.45 |
HashEntry<K,V> e = getFirst(hash); |
341 |
dl |
1.4 |
while (e != null) { |
342 |
tim |
1.11 |
if (e.hash == hash && key.equals(e.key)) |
343 |
dl |
1.4 |
return true; |
344 |
|
|
e = e.next; |
345 |
|
|
} |
346 |
|
|
} |
347 |
|
|
return false; |
348 |
|
|
} |
349 |
tim |
1.11 |
|
350 |
dl |
1.4 |
boolean containsValue(Object value) { |
351 |
|
|
if (count != 0) { // read-volatile |
352 |
tim |
1.11 |
HashEntry[] tab = table; |
353 |
dl |
1.4 |
int len = tab.length; |
354 |
dl |
1.45 |
for (int i = 0 ; i < len; i++) { |
355 |
|
|
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; |
356 |
|
|
e != null ; |
357 |
|
|
e = e.next) { |
358 |
|
|
V v = e.value; |
359 |
|
|
if (v == null) // recheck |
360 |
|
|
v = readValueUnderLock(e); |
361 |
|
|
if (value.equals(v)) |
362 |
dl |
1.4 |
return true; |
363 |
dl |
1.45 |
} |
364 |
|
|
} |
365 |
dl |
1.4 |
} |
366 |
|
|
return false; |
367 |
|
|
} |
368 |
|
|
|
369 |
dl |
1.31 |
boolean replace(K key, int hash, V oldValue, V newValue) { |
370 |
|
|
lock(); |
371 |
|
|
try { |
372 |
dl |
1.45 |
HashEntry<K,V> e = getFirst(hash); |
373 |
|
|
while (e != null && (e.hash != hash || !key.equals(e.key))) |
374 |
dl |
1.31 |
e = e.next; |
375 |
dl |
1.45 |
|
376 |
|
|
boolean replaced = false; |
377 |
|
|
if (e != null && oldValue.equals(e.value)) { |
378 |
|
|
replaced = true; |
379 |
|
|
e.value = newValue; |
380 |
dl |
1.31 |
} |
381 |
dl |
1.45 |
return replaced; |
382 |
dl |
1.33 |
} finally { |
383 |
|
|
unlock(); |
384 |
|
|
} |
385 |
|
|
} |
386 |
|
|
|
387 |
|
|
V replace(K key, int hash, V newValue) { |
388 |
|
|
lock(); |
389 |
|
|
try { |
390 |
dl |
1.45 |
HashEntry<K,V> e = getFirst(hash); |
391 |
|
|
while (e != null && (e.hash != hash || !key.equals(e.key))) |
392 |
dl |
1.33 |
e = e.next; |
393 |
dl |
1.45 |
|
394 |
|
|
V oldValue = null; |
395 |
|
|
if (e != null) { |
396 |
|
|
oldValue = e.value; |
397 |
|
|
e.value = newValue; |
398 |
dl |
1.32 |
} |
399 |
dl |
1.45 |
return oldValue; |
400 |
dl |
1.31 |
} finally { |
401 |
|
|
unlock(); |
402 |
|
|
} |
403 |
|
|
} |
404 |
|
|
|
405 |
dl |
1.32 |
|
406 |
tim |
1.11 |
V put(K key, int hash, V value, boolean onlyIfAbsent) { |
407 |
dl |
1.4 |
lock(); |
408 |
|
|
try { |
409 |
dl |
1.9 |
int c = count; |
410 |
dl |
1.45 |
if (c++ > threshold) // ensure capacity |
411 |
|
|
rehash(); |
412 |
tim |
1.11 |
HashEntry[] tab = table; |
413 |
dl |
1.9 |
int index = hash & (tab.length - 1); |
414 |
tim |
1.11 |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
415 |
dl |
1.45 |
HashEntry<K,V> e = first; |
416 |
|
|
while (e != null && (e.hash != hash || !key.equals(e.key))) |
417 |
|
|
e = e.next; |
418 |
tim |
1.11 |
|
419 |
dl |
1.45 |
V oldValue; |
420 |
|
|
if (e != null) { |
421 |
|
|
oldValue = e.value; |
422 |
|
|
if (!onlyIfAbsent) |
423 |
|
|
e.value = value; |
424 |
|
|
} |
425 |
|
|
else { |
426 |
|
|
oldValue = null; |
427 |
|
|
++modCount; |
428 |
|
|
tab[index] = new HashEntry<K,V>(key, hash, first, value); |
429 |
|
|
count = c; // write-volatile |
430 |
dl |
1.4 |
} |
431 |
dl |
1.45 |
return oldValue; |
432 |
tim |
1.16 |
} finally { |
433 |
dl |
1.4 |
unlock(); |
434 |
|
|
} |
435 |
|
|
} |
436 |
|
|
|
437 |
dl |
1.45 |
void rehash() { |
438 |
|
|
HashEntry[] oldTable = table; |
439 |
dl |
1.4 |
int oldCapacity = oldTable.length; |
440 |
|
|
if (oldCapacity >= MAXIMUM_CAPACITY) |
441 |
dl |
1.45 |
return; |
442 |
dl |
1.4 |
|
443 |
|
|
/* |
444 |
|
|
* Reclassify nodes in each list to new Map. Because we are |
445 |
|
|
* using power-of-two expansion, the elements from each bin |
446 |
|
|
* must either stay at same index, or move with a power of two |
447 |
|
|
* offset. We eliminate unnecessary node creation by catching |
448 |
|
|
* cases where old nodes can be reused because their next |
449 |
|
|
* fields won't change. Statistically, at the default |
450 |
dl |
1.29 |
* threshold, only about one-sixth of them need cloning when |
451 |
dl |
1.4 |
* a table doubles. The nodes they replace will be garbage |
452 |
|
|
* collectable as soon as they are no longer referenced by any |
453 |
|
|
* reader thread that may be in the midst of traversing table |
454 |
|
|
* right now. |
455 |
|
|
*/ |
456 |
tim |
1.11 |
|
457 |
|
|
HashEntry[] newTable = new HashEntry[oldCapacity << 1]; |
458 |
dl |
1.45 |
threshold = (int)(newTable.length * loadFactor); |
459 |
dl |
1.4 |
int sizeMask = newTable.length - 1; |
460 |
|
|
for (int i = 0; i < oldCapacity ; i++) { |
461 |
|
|
// We need to guarantee that any existing reads of old Map can |
462 |
tim |
1.11 |
// proceed. So we cannot yet null out each bin. |
463 |
tim |
1.12 |
HashEntry<K,V> e = (HashEntry<K,V>)oldTable[i]; |
464 |
tim |
1.11 |
|
465 |
dl |
1.4 |
if (e != null) { |
466 |
|
|
HashEntry<K,V> next = e.next; |
467 |
|
|
int idx = e.hash & sizeMask; |
468 |
tim |
1.11 |
|
469 |
dl |
1.4 |
// Single node on list |
470 |
tim |
1.11 |
if (next == null) |
471 |
dl |
1.4 |
newTable[idx] = e; |
472 |
tim |
1.11 |
|
473 |
|
|
else { |
474 |
dl |
1.4 |
// Reuse trailing consecutive sequence at same slot |
475 |
|
|
HashEntry<K,V> lastRun = e; |
476 |
|
|
int lastIdx = idx; |
477 |
tim |
1.11 |
for (HashEntry<K,V> last = next; |
478 |
|
|
last != null; |
479 |
dl |
1.4 |
last = last.next) { |
480 |
|
|
int k = last.hash & sizeMask; |
481 |
|
|
if (k != lastIdx) { |
482 |
|
|
lastIdx = k; |
483 |
|
|
lastRun = last; |
484 |
|
|
} |
485 |
|
|
} |
486 |
|
|
newTable[lastIdx] = lastRun; |
487 |
tim |
1.11 |
|
488 |
dl |
1.4 |
// Clone all remaining nodes |
489 |
|
|
for (HashEntry<K,V> p = e; p != lastRun; p = p.next) { |
490 |
|
|
int k = p.hash & sizeMask; |
491 |
dl |
1.45 |
HashEntry<K,V> n = (HashEntry<K,V>)newTable[k]; |
492 |
|
|
newTable[k] = new HashEntry<K,V>(p.key, p.hash, |
493 |
|
|
n, p.value); |
494 |
dl |
1.4 |
} |
495 |
|
|
} |
496 |
|
|
} |
497 |
|
|
} |
498 |
dl |
1.45 |
table = newTable; |
499 |
dl |
1.4 |
} |
500 |
dl |
1.6 |
|
501 |
|
|
/** |
502 |
|
|
* Remove; match on key only if value null, else match both. |
503 |
|
|
*/ |
504 |
dl |
1.4 |
V remove(Object key, int hash, Object value) { |
505 |
tim |
1.11 |
lock(); |
506 |
dl |
1.4 |
try { |
507 |
dl |
1.45 |
int c = count - 1; |
508 |
dl |
1.4 |
HashEntry[] tab = table; |
509 |
dl |
1.9 |
int index = hash & (tab.length - 1); |
510 |
tim |
1.12 |
HashEntry<K,V> first = (HashEntry<K,V>)tab[index]; |
511 |
dl |
1.4 |
HashEntry<K,V> e = first; |
512 |
dl |
1.45 |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
513 |
dl |
1.4 |
e = e.next; |
514 |
dl |
1.45 |
|
515 |
|
|
V oldValue = null; |
516 |
|
|
if (e != null) { |
517 |
|
|
V v = e.value; |
518 |
|
|
if (value == null || value.equals(v)) { |
519 |
|
|
oldValue = v; |
520 |
|
|
// All entries following removed node can stay |
521 |
|
|
// in list, but all preceding ones need to be |
522 |
|
|
// cloned. |
523 |
|
|
++modCount; |
524 |
|
|
HashEntry<K,V> newFirst = e.next; |
525 |
|
|
for (HashEntry<K,V> p = first; p != e; p = p.next) |
526 |
|
|
newFirst = new HashEntry<K,V>(p.key, p.hash, |
527 |
|
|
newFirst, p.value); |
528 |
|
|
tab[index] = newFirst; |
529 |
|
|
count = c; // write-volatile |
530 |
|
|
} |
531 |
dl |
1.4 |
} |
532 |
dl |
1.9 |
return oldValue; |
533 |
tim |
1.16 |
} finally { |
534 |
dl |
1.4 |
unlock(); |
535 |
|
|
} |
536 |
|
|
} |
537 |
|
|
|
538 |
|
|
void clear() { |
539 |
dl |
1.45 |
if (count != 0) { |
540 |
|
|
lock(); |
541 |
|
|
try { |
542 |
|
|
HashEntry[] tab = table; |
543 |
|
|
for (int i = 0; i < tab.length ; i++) |
544 |
|
|
tab[i] = null; |
545 |
|
|
++modCount; |
546 |
|
|
count = 0; // write-volatile |
547 |
|
|
} finally { |
548 |
|
|
unlock(); |
549 |
|
|
} |
550 |
dl |
1.4 |
} |
551 |
|
|
} |
552 |
tim |
1.1 |
} |
553 |
|
|
|
554 |
|
|
|
555 |
tim |
1.11 |
|
556 |
dl |
1.4 |
/* ---------------- Public operations -------------- */ |
557 |
tim |
1.1 |
|
558 |
|
|
/** |
559 |
dl |
1.44 |
* Creates a new, empty map with the specified initial |
560 |
tim |
1.1 |
* capacity and the specified load factor. |
561 |
|
|
* |
562 |
dl |
1.19 |
* @param initialCapacity the initial capacity. The implementation |
563 |
|
|
* performs internal sizing to accommodate this many elements. |
564 |
tim |
1.1 |
* @param loadFactor the load factor threshold, used to control resizing. |
565 |
dl |
1.19 |
* @param concurrencyLevel the estimated number of concurrently |
566 |
|
|
* updating threads. The implementation performs internal sizing |
567 |
dl |
1.21 |
* to try to accommodate this many threads. |
568 |
dl |
1.4 |
* @throws IllegalArgumentException if the initial capacity is |
569 |
dl |
1.19 |
* negative or the load factor or concurrencyLevel are |
570 |
dl |
1.4 |
* nonpositive. |
571 |
|
|
*/ |
572 |
tim |
1.11 |
public ConcurrentHashMap(int initialCapacity, |
573 |
dl |
1.19 |
float loadFactor, int concurrencyLevel) { |
574 |
|
|
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0) |
575 |
dl |
1.4 |
throw new IllegalArgumentException(); |
576 |
|
|
|
577 |
dl |
1.21 |
if (concurrencyLevel > MAX_SEGMENTS) |
578 |
|
|
concurrencyLevel = MAX_SEGMENTS; |
579 |
|
|
|
580 |
dl |
1.4 |
// Find power-of-two sizes best matching arguments |
581 |
|
|
int sshift = 0; |
582 |
|
|
int ssize = 1; |
583 |
dl |
1.19 |
while (ssize < concurrencyLevel) { |
584 |
dl |
1.4 |
++sshift; |
585 |
|
|
ssize <<= 1; |
586 |
|
|
} |
587 |
dl |
1.9 |
segmentShift = 32 - sshift; |
588 |
dl |
1.8 |
segmentMask = ssize - 1; |
589 |
tim |
1.11 |
this.segments = new Segment[ssize]; |
590 |
dl |
1.4 |
|
591 |
|
|
if (initialCapacity > MAXIMUM_CAPACITY) |
592 |
|
|
initialCapacity = MAXIMUM_CAPACITY; |
593 |
|
|
int c = initialCapacity / ssize; |
594 |
tim |
1.11 |
if (c * ssize < initialCapacity) |
595 |
dl |
1.4 |
++c; |
596 |
|
|
int cap = 1; |
597 |
|
|
while (cap < c) |
598 |
|
|
cap <<= 1; |
599 |
|
|
|
600 |
|
|
for (int i = 0; i < this.segments.length; ++i) |
601 |
|
|
this.segments[i] = new Segment<K,V>(cap, loadFactor); |
602 |
tim |
1.1 |
} |
603 |
|
|
|
604 |
|
|
/** |
605 |
dl |
1.44 |
* Creates a new, empty map with the specified initial |
606 |
dl |
1.19 |
* capacity, and with default load factor and concurrencyLevel. |
607 |
tim |
1.1 |
* |
608 |
dl |
1.19 |
* @param initialCapacity The implementation performs internal |
609 |
|
|
* sizing to accommodate this many elements. |
610 |
dl |
1.4 |
* @throws IllegalArgumentException if the initial capacity of |
611 |
|
|
* elements is negative. |
612 |
tim |
1.1 |
*/ |
613 |
|
|
public ConcurrentHashMap(int initialCapacity) { |
614 |
dl |
1.4 |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
615 |
tim |
1.1 |
} |
616 |
|
|
|
617 |
|
|
/** |
618 |
dl |
1.44 |
* Creates a new, empty map with a default initial capacity, |
619 |
dl |
1.23 |
* load factor, and concurrencyLevel. |
620 |
tim |
1.1 |
*/ |
621 |
|
|
public ConcurrentHashMap() { |
622 |
dl |
1.4 |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
623 |
tim |
1.1 |
} |
624 |
|
|
|
625 |
|
|
/** |
626 |
dl |
1.44 |
* Creates a new map with the same mappings as the given map. The |
627 |
tim |
1.1 |
* map is created with a capacity of twice the number of mappings in |
628 |
dl |
1.4 |
* the given map or 11 (whichever is greater), and a default load factor. |
629 |
dl |
1.40 |
* @param t the map |
630 |
tim |
1.1 |
*/ |
631 |
tim |
1.39 |
public ConcurrentHashMap(Map<? extends K, ? extends V> t) { |
632 |
tim |
1.1 |
this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1, |
633 |
dl |
1.4 |
11), |
634 |
|
|
DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
635 |
|
|
putAll(t); |
636 |
tim |
1.1 |
} |
637 |
|
|
|
638 |
dl |
1.4 |
// inherit Map javadoc |
639 |
tim |
1.1 |
public boolean isEmpty() { |
640 |
dl |
1.35 |
final Segment[] segments = this.segments; |
641 |
dl |
1.21 |
/* |
642 |
dl |
1.45 |
* We keep track of per-segment modCounts to avoid ABA |
643 |
dl |
1.21 |
* problems in which an element in one segment was added and |
644 |
|
|
* in another removed during traversal, in which case the |
645 |
|
|
* table was never actually empty at any point. Note the |
646 |
|
|
* similar use of modCounts in the size() and containsValue() |
647 |
|
|
* methods, which are the only other methods also susceptible |
648 |
|
|
* to ABA problems. |
649 |
|
|
*/ |
650 |
|
|
int[] mc = new int[segments.length]; |
651 |
|
|
int mcsum = 0; |
652 |
|
|
for (int i = 0; i < segments.length; ++i) { |
653 |
dl |
1.4 |
if (segments[i].count != 0) |
654 |
tim |
1.1 |
return false; |
655 |
dl |
1.21 |
else |
656 |
|
|
mcsum += mc[i] = segments[i].modCount; |
657 |
|
|
} |
658 |
|
|
// If mcsum happens to be zero, then we know we got a snapshot |
659 |
|
|
// before any modifications at all were made. This is |
660 |
|
|
// probably common enough to bother tracking. |
661 |
|
|
if (mcsum != 0) { |
662 |
|
|
for (int i = 0; i < segments.length; ++i) { |
663 |
|
|
if (segments[i].count != 0 || |
664 |
|
|
mc[i] != segments[i].modCount) |
665 |
|
|
return false; |
666 |
|
|
} |
667 |
|
|
} |
668 |
tim |
1.1 |
return true; |
669 |
|
|
} |
670 |
|
|
|
671 |
dl |
1.21 |
// inherit Map javadoc |
672 |
|
|
public int size() { |
673 |
dl |
1.35 |
final Segment[] segments = this.segments; |
674 |
dl |
1.45 |
long sum = 0; |
675 |
|
|
long check = 0; |
676 |
dl |
1.21 |
int[] mc = new int[segments.length]; |
677 |
dl |
1.46 |
// Try a few times to get accurate count. On failure due to |
678 |
dl |
1.45 |
// continuous async changes in table, resort to locking. |
679 |
dl |
1.46 |
for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) { |
680 |
dl |
1.45 |
check = 0; |
681 |
|
|
sum = 0; |
682 |
dl |
1.21 |
int mcsum = 0; |
683 |
|
|
for (int i = 0; i < segments.length; ++i) { |
684 |
|
|
sum += segments[i].count; |
685 |
|
|
mcsum += mc[i] = segments[i].modCount; |
686 |
|
|
} |
687 |
|
|
if (mcsum != 0) { |
688 |
|
|
for (int i = 0; i < segments.length; ++i) { |
689 |
|
|
check += segments[i].count; |
690 |
|
|
if (mc[i] != segments[i].modCount) { |
691 |
|
|
check = -1; // force retry |
692 |
|
|
break; |
693 |
|
|
} |
694 |
|
|
} |
695 |
|
|
} |
696 |
dl |
1.45 |
if (check == sum) |
697 |
|
|
break; |
698 |
|
|
} |
699 |
|
|
if (check != sum) { // Resort to locking all segments |
700 |
|
|
sum = 0; |
701 |
|
|
for (int i = 0; i < segments.length; ++i) |
702 |
|
|
segments[i].lock(); |
703 |
|
|
for (int i = 0; i < segments.length; ++i) |
704 |
|
|
sum += segments[i].count; |
705 |
|
|
for (int i = 0; i < segments.length; ++i) |
706 |
|
|
segments[i].unlock(); |
707 |
dl |
1.21 |
} |
708 |
dl |
1.45 |
if (sum > Integer.MAX_VALUE) |
709 |
|
|
return Integer.MAX_VALUE; |
710 |
|
|
else |
711 |
|
|
return (int)sum; |
712 |
dl |
1.21 |
} |
713 |
|
|
|
714 |
|
|
|
715 |
tim |
1.1 |
/** |
716 |
|
|
* Returns the value to which the specified key is mapped in this table. |
717 |
|
|
* |
718 |
|
|
* @param key a key in the table. |
719 |
|
|
* @return the value to which the key is mapped in this table; |
720 |
dl |
1.19 |
* <tt>null</tt> if the key is not mapped to any value in |
721 |
tim |
1.1 |
* this table. |
722 |
dl |
1.8 |
* @throws NullPointerException if the key is |
723 |
dl |
1.19 |
* <tt>null</tt>. |
724 |
tim |
1.1 |
*/ |
725 |
tim |
1.11 |
public V get(Object key) { |
726 |
dl |
1.4 |
int hash = hash(key); // throws NullPointerException if key null |
727 |
dl |
1.29 |
return segmentFor(hash).get(key, hash); |
728 |
tim |
1.1 |
} |
729 |
|
|
|
730 |
|
|
/** |
731 |
|
|
* Tests if the specified object is a key in this table. |
732 |
tim |
1.11 |
* |
733 |
tim |
1.1 |
* @param key possible key. |
734 |
dl |
1.19 |
* @return <tt>true</tt> if and only if the specified object |
735 |
tim |
1.11 |
* is a key in this table, as determined by the |
736 |
dl |
1.19 |
* <tt>equals</tt> method; <tt>false</tt> otherwise. |
737 |
dl |
1.8 |
* @throws NullPointerException if the key is |
738 |
dl |
1.19 |
* <tt>null</tt>. |
739 |
tim |
1.1 |
*/ |
740 |
|
|
public boolean containsKey(Object key) { |
741 |
dl |
1.4 |
int hash = hash(key); // throws NullPointerException if key null |
742 |
dl |
1.9 |
return segmentFor(hash).containsKey(key, hash); |
743 |
tim |
1.1 |
} |
744 |
|
|
|
745 |
|
|
/** |
746 |
|
|
* Returns <tt>true</tt> if this map maps one or more keys to the |
747 |
|
|
* specified value. Note: This method requires a full internal |
748 |
|
|
* traversal of the hash table, and so is much slower than |
749 |
|
|
* method <tt>containsKey</tt>. |
750 |
|
|
* |
751 |
|
|
* @param value value whose presence in this map is to be tested. |
752 |
|
|
* @return <tt>true</tt> if this map maps one or more keys to the |
753 |
tim |
1.11 |
* specified value. |
754 |
dl |
1.19 |
* @throws NullPointerException if the value is <tt>null</tt>. |
755 |
tim |
1.1 |
*/ |
756 |
|
|
public boolean containsValue(Object value) { |
757 |
tim |
1.11 |
if (value == null) |
758 |
dl |
1.4 |
throw new NullPointerException(); |
759 |
dl |
1.45 |
|
760 |
|
|
// See explanation of modCount use above |
761 |
tim |
1.1 |
|
762 |
dl |
1.35 |
final Segment[] segments = this.segments; |
763 |
dl |
1.21 |
int[] mc = new int[segments.length]; |
764 |
dl |
1.45 |
|
765 |
dl |
1.46 |
// Try a few times without locking |
766 |
|
|
for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) { |
767 |
dl |
1.21 |
int sum = 0; |
768 |
|
|
int mcsum = 0; |
769 |
|
|
for (int i = 0; i < segments.length; ++i) { |
770 |
|
|
int c = segments[i].count; |
771 |
|
|
mcsum += mc[i] = segments[i].modCount; |
772 |
|
|
if (segments[i].containsValue(value)) |
773 |
|
|
return true; |
774 |
|
|
} |
775 |
|
|
boolean cleanSweep = true; |
776 |
|
|
if (mcsum != 0) { |
777 |
|
|
for (int i = 0; i < segments.length; ++i) { |
778 |
|
|
int c = segments[i].count; |
779 |
|
|
if (mc[i] != segments[i].modCount) { |
780 |
|
|
cleanSweep = false; |
781 |
|
|
break; |
782 |
|
|
} |
783 |
|
|
} |
784 |
|
|
} |
785 |
|
|
if (cleanSweep) |
786 |
|
|
return false; |
787 |
tim |
1.1 |
} |
788 |
dl |
1.45 |
// Resort to locking all segments |
789 |
|
|
for (int i = 0; i < segments.length; ++i) |
790 |
|
|
segments[i].lock(); |
791 |
|
|
boolean found = false; |
792 |
|
|
try { |
793 |
|
|
for (int i = 0; i < segments.length; ++i) { |
794 |
|
|
if (segments[i].containsValue(value)) { |
795 |
|
|
found = true; |
796 |
|
|
break; |
797 |
|
|
} |
798 |
|
|
} |
799 |
|
|
} finally { |
800 |
|
|
for (int i = 0; i < segments.length; ++i) |
801 |
|
|
segments[i].unlock(); |
802 |
|
|
} |
803 |
|
|
return found; |
804 |
tim |
1.1 |
} |
805 |
dl |
1.19 |
|
806 |
tim |
1.1 |
/** |
807 |
dl |
1.18 |
* Legacy method testing if some key maps into the specified value |
808 |
dl |
1.23 |
* in this table. This method is identical in functionality to |
809 |
|
|
* {@link #containsValue}, and exists solely to ensure |
810 |
dl |
1.19 |
* full compatibility with class {@link java.util.Hashtable}, |
811 |
dl |
1.18 |
* which supported this method prior to introduction of the |
812 |
dl |
1.23 |
* Java Collections framework. |
813 |
dl |
1.17 |
|
814 |
tim |
1.1 |
* @param value a value to search for. |
815 |
dl |
1.19 |
* @return <tt>true</tt> if and only if some key maps to the |
816 |
|
|
* <tt>value</tt> argument in this table as |
817 |
tim |
1.1 |
* determined by the <tt>equals</tt> method; |
818 |
dl |
1.19 |
* <tt>false</tt> otherwise. |
819 |
|
|
* @throws NullPointerException if the value is <tt>null</tt>. |
820 |
tim |
1.1 |
*/ |
821 |
dl |
1.4 |
public boolean contains(Object value) { |
822 |
tim |
1.1 |
return containsValue(value); |
823 |
|
|
} |
824 |
|
|
|
825 |
|
|
/** |
826 |
dl |
1.19 |
* Maps the specified <tt>key</tt> to the specified |
827 |
|
|
* <tt>value</tt> in this table. Neither the key nor the |
828 |
dl |
1.44 |
* value can be <tt>null</tt>. |
829 |
dl |
1.4 |
* |
830 |
dl |
1.44 |
* <p> The value can be retrieved by calling the <tt>get</tt> method |
831 |
tim |
1.11 |
* with a key that is equal to the original key. |
832 |
dl |
1.4 |
* |
833 |
|
|
* @param key the table key. |
834 |
|
|
* @param value the value. |
835 |
|
|
* @return the previous value of the specified key in this table, |
836 |
dl |
1.19 |
* or <tt>null</tt> if it did not have one. |
837 |
dl |
1.8 |
* @throws NullPointerException if the key or value is |
838 |
dl |
1.19 |
* <tt>null</tt>. |
839 |
dl |
1.4 |
*/ |
840 |
tim |
1.11 |
public V put(K key, V value) { |
841 |
|
|
if (value == null) |
842 |
dl |
1.4 |
throw new NullPointerException(); |
843 |
tim |
1.11 |
int hash = hash(key); |
844 |
dl |
1.9 |
return segmentFor(hash).put(key, hash, value, false); |
845 |
dl |
1.4 |
} |
846 |
|
|
|
847 |
|
|
/** |
848 |
|
|
* If the specified key is not already associated |
849 |
|
|
* with a value, associate it with the given value. |
850 |
|
|
* This is equivalent to |
851 |
|
|
* <pre> |
852 |
dl |
1.17 |
* if (!map.containsKey(key)) |
853 |
|
|
* return map.put(key, value); |
854 |
|
|
* else |
855 |
|
|
* return map.get(key); |
856 |
dl |
1.4 |
* </pre> |
857 |
|
|
* Except that the action is performed atomically. |
858 |
|
|
* @param key key with which the specified value is to be associated. |
859 |
|
|
* @param value value to be associated with the specified key. |
860 |
|
|
* @return previous value associated with specified key, or <tt>null</tt> |
861 |
|
|
* if there was no mapping for key. A <tt>null</tt> return can |
862 |
|
|
* also indicate that the map previously associated <tt>null</tt> |
863 |
|
|
* with the specified key, if the implementation supports |
864 |
|
|
* <tt>null</tt> values. |
865 |
|
|
* |
866 |
dl |
1.17 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation is |
867 |
|
|
* not supported by this map. |
868 |
|
|
* @throws ClassCastException if the class of the specified key or value |
869 |
|
|
* prevents it from being stored in this map. |
870 |
|
|
* @throws NullPointerException if the specified key or value is |
871 |
dl |
1.4 |
* <tt>null</tt>. |
872 |
|
|
* |
873 |
|
|
**/ |
874 |
tim |
1.11 |
public V putIfAbsent(K key, V value) { |
875 |
|
|
if (value == null) |
876 |
dl |
1.4 |
throw new NullPointerException(); |
877 |
tim |
1.11 |
int hash = hash(key); |
878 |
dl |
1.9 |
return segmentFor(hash).put(key, hash, value, true); |
879 |
dl |
1.4 |
} |
880 |
|
|
|
881 |
|
|
|
882 |
|
|
/** |
883 |
tim |
1.1 |
* Copies all of the mappings from the specified map to this one. |
884 |
|
|
* |
885 |
|
|
* These mappings replace any mappings that this map had for any of the |
886 |
|
|
* keys currently in the specified Map. |
887 |
|
|
* |
888 |
|
|
* @param t Mappings to be stored in this map. |
889 |
|
|
*/ |
890 |
tim |
1.11 |
public void putAll(Map<? extends K, ? extends V> t) { |
891 |
dl |
1.23 |
for (Iterator<Map.Entry<? extends K, ? extends V>> it = (Iterator<Map.Entry<? extends K, ? extends V>>) t.entrySet().iterator(); it.hasNext(); ) { |
892 |
tim |
1.12 |
Entry<? extends K, ? extends V> e = it.next(); |
893 |
dl |
1.4 |
put(e.getKey(), e.getValue()); |
894 |
tim |
1.1 |
} |
895 |
dl |
1.4 |
} |
896 |
|
|
|
897 |
|
|
/** |
898 |
tim |
1.11 |
* Removes the key (and its corresponding value) from this |
899 |
dl |
1.4 |
* table. This method does nothing if the key is not in the table. |
900 |
|
|
* |
901 |
|
|
* @param key the key that needs to be removed. |
902 |
|
|
* @return the value to which the key had been mapped in this table, |
903 |
dl |
1.19 |
* or <tt>null</tt> if the key did not have a mapping. |
904 |
dl |
1.8 |
* @throws NullPointerException if the key is |
905 |
dl |
1.19 |
* <tt>null</tt>. |
906 |
dl |
1.4 |
*/ |
907 |
|
|
public V remove(Object key) { |
908 |
|
|
int hash = hash(key); |
909 |
dl |
1.9 |
return segmentFor(hash).remove(key, hash, null); |
910 |
dl |
1.4 |
} |
911 |
tim |
1.1 |
|
912 |
dl |
1.4 |
/** |
913 |
dl |
1.17 |
* Remove entry for key only if currently mapped to given value. |
914 |
|
|
* Acts as |
915 |
|
|
* <pre> |
916 |
|
|
* if (map.get(key).equals(value)) { |
917 |
|
|
* map.remove(key); |
918 |
|
|
* return true; |
919 |
|
|
* } else return false; |
920 |
|
|
* </pre> |
921 |
|
|
* except that the action is performed atomically. |
922 |
|
|
* @param key key with which the specified value is associated. |
923 |
|
|
* @param value value associated with the specified key. |
924 |
|
|
* @return true if the value was removed |
925 |
|
|
* @throws NullPointerException if the specified key is |
926 |
|
|
* <tt>null</tt>. |
927 |
dl |
1.4 |
*/ |
928 |
dl |
1.13 |
public boolean remove(Object key, Object value) { |
929 |
dl |
1.4 |
int hash = hash(key); |
930 |
dl |
1.13 |
return segmentFor(hash).remove(key, hash, value) != null; |
931 |
tim |
1.1 |
} |
932 |
dl |
1.31 |
|
933 |
dl |
1.32 |
|
934 |
dl |
1.31 |
/** |
935 |
|
|
* Replace entry for key only if currently mapped to given value. |
936 |
|
|
* Acts as |
937 |
|
|
* <pre> |
938 |
|
|
* if (map.get(key).equals(oldValue)) { |
939 |
|
|
* map.put(key, newValue); |
940 |
|
|
* return true; |
941 |
|
|
* } else return false; |
942 |
|
|
* </pre> |
943 |
|
|
* except that the action is performed atomically. |
944 |
|
|
* @param key key with which the specified value is associated. |
945 |
|
|
* @param oldValue value expected to be associated with the specified key. |
946 |
|
|
* @param newValue value to be associated with the specified key. |
947 |
|
|
* @return true if the value was replaced |
948 |
|
|
* @throws NullPointerException if the specified key or values are |
949 |
|
|
* <tt>null</tt>. |
950 |
|
|
*/ |
951 |
|
|
public boolean replace(K key, V oldValue, V newValue) { |
952 |
|
|
if (oldValue == null || newValue == null) |
953 |
|
|
throw new NullPointerException(); |
954 |
|
|
int hash = hash(key); |
955 |
|
|
return segmentFor(hash).replace(key, hash, oldValue, newValue); |
956 |
dl |
1.32 |
} |
957 |
|
|
|
958 |
|
|
/** |
959 |
dl |
1.33 |
* Replace entry for key only if currently mapped to some value. |
960 |
dl |
1.32 |
* Acts as |
961 |
|
|
* <pre> |
962 |
|
|
* if ((map.containsKey(key)) { |
963 |
dl |
1.33 |
* return map.put(key, value); |
964 |
|
|
* } else return null; |
965 |
dl |
1.32 |
* </pre> |
966 |
|
|
* except that the action is performed atomically. |
967 |
|
|
* @param key key with which the specified value is associated. |
968 |
|
|
* @param value value to be associated with the specified key. |
969 |
dl |
1.33 |
* @return previous value associated with specified key, or <tt>null</tt> |
970 |
|
|
* if there was no mapping for key. |
971 |
dl |
1.32 |
* @throws NullPointerException if the specified key or value is |
972 |
|
|
* <tt>null</tt>. |
973 |
|
|
*/ |
974 |
dl |
1.33 |
public V replace(K key, V value) { |
975 |
dl |
1.32 |
if (value == null) |
976 |
|
|
throw new NullPointerException(); |
977 |
|
|
int hash = hash(key); |
978 |
dl |
1.33 |
return segmentFor(hash).replace(key, hash, value); |
979 |
dl |
1.31 |
} |
980 |
|
|
|
981 |
tim |
1.1 |
|
982 |
|
|
/** |
983 |
|
|
* Removes all mappings from this map. |
984 |
|
|
*/ |
985 |
|
|
public void clear() { |
986 |
tim |
1.11 |
for (int i = 0; i < segments.length; ++i) |
987 |
dl |
1.4 |
segments[i].clear(); |
988 |
tim |
1.1 |
} |
989 |
|
|
|
990 |
dl |
1.4 |
|
991 |
tim |
1.1 |
/** |
992 |
|
|
* Returns a shallow copy of this |
993 |
|
|
* <tt>ConcurrentHashMap</tt> instance: the keys and |
994 |
|
|
* values themselves are not cloned. |
995 |
|
|
* |
996 |
|
|
* @return a shallow copy of this map. |
997 |
|
|
*/ |
998 |
|
|
public Object clone() { |
999 |
dl |
1.4 |
// We cannot call super.clone, since it would share final |
1000 |
|
|
// segments array, and there's no way to reassign finals. |
1001 |
|
|
|
1002 |
|
|
float lf = segments[0].loadFactor; |
1003 |
|
|
int segs = segments.length; |
1004 |
|
|
int cap = (int)(size() / lf); |
1005 |
|
|
if (cap < segs) cap = segs; |
1006 |
tim |
1.12 |
ConcurrentHashMap<K,V> t = new ConcurrentHashMap<K,V>(cap, lf, segs); |
1007 |
dl |
1.4 |
t.putAll(this); |
1008 |
|
|
return t; |
1009 |
tim |
1.1 |
} |
1010 |
|
|
|
1011 |
|
|
/** |
1012 |
|
|
* Returns a set view of the keys contained in this map. The set is |
1013 |
|
|
* backed by the map, so changes to the map are reflected in the set, and |
1014 |
|
|
* vice-versa. The set supports element removal, which removes the |
1015 |
|
|
* corresponding mapping from this map, via the <tt>Iterator.remove</tt>, |
1016 |
|
|
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and |
1017 |
|
|
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
1018 |
|
|
* <tt>addAll</tt> operations. |
1019 |
dl |
1.14 |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1020 |
|
|
* will never throw {@link java.util.ConcurrentModificationException}, |
1021 |
|
|
* and guarantees to traverse elements as they existed upon |
1022 |
|
|
* construction of the iterator, and may (but is not guaranteed to) |
1023 |
|
|
* reflect any modifications subsequent to construction. |
1024 |
tim |
1.1 |
* |
1025 |
|
|
* @return a set view of the keys contained in this map. |
1026 |
|
|
*/ |
1027 |
|
|
public Set<K> keySet() { |
1028 |
|
|
Set<K> ks = keySet; |
1029 |
dl |
1.8 |
return (ks != null) ? ks : (keySet = new KeySet()); |
1030 |
tim |
1.1 |
} |
1031 |
|
|
|
1032 |
|
|
|
1033 |
|
|
/** |
1034 |
|
|
* Returns a collection view of the values contained in this map. The |
1035 |
|
|
* collection is backed by the map, so changes to the map are reflected in |
1036 |
|
|
* the collection, and vice-versa. The collection supports element |
1037 |
|
|
* removal, which removes the corresponding mapping from this map, via the |
1038 |
|
|
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
1039 |
|
|
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
1040 |
|
|
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
1041 |
dl |
1.14 |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1042 |
|
|
* will never throw {@link java.util.ConcurrentModificationException}, |
1043 |
|
|
* and guarantees to traverse elements as they existed upon |
1044 |
|
|
* construction of the iterator, and may (but is not guaranteed to) |
1045 |
|
|
* reflect any modifications subsequent to construction. |
1046 |
tim |
1.1 |
* |
1047 |
|
|
* @return a collection view of the values contained in this map. |
1048 |
|
|
*/ |
1049 |
|
|
public Collection<V> values() { |
1050 |
|
|
Collection<V> vs = values; |
1051 |
dl |
1.8 |
return (vs != null) ? vs : (values = new Values()); |
1052 |
tim |
1.1 |
} |
1053 |
|
|
|
1054 |
|
|
|
1055 |
|
|
/** |
1056 |
|
|
* Returns a collection view of the mappings contained in this map. Each |
1057 |
|
|
* element in the returned collection is a <tt>Map.Entry</tt>. The |
1058 |
|
|
* collection is backed by the map, so changes to the map are reflected in |
1059 |
|
|
* the collection, and vice-versa. The collection supports element |
1060 |
|
|
* removal, which removes the corresponding mapping from the map, via the |
1061 |
|
|
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
1062 |
|
|
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
1063 |
|
|
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
1064 |
dl |
1.14 |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1065 |
|
|
* will never throw {@link java.util.ConcurrentModificationException}, |
1066 |
|
|
* and guarantees to traverse elements as they existed upon |
1067 |
|
|
* construction of the iterator, and may (but is not guaranteed to) |
1068 |
|
|
* reflect any modifications subsequent to construction. |
1069 |
tim |
1.1 |
* |
1070 |
|
|
* @return a collection view of the mappings contained in this map. |
1071 |
|
|
*/ |
1072 |
|
|
public Set<Map.Entry<K,V>> entrySet() { |
1073 |
|
|
Set<Map.Entry<K,V>> es = entrySet; |
1074 |
dl |
1.23 |
return (es != null) ? es : (entrySet = (Set<Map.Entry<K,V>>) (Set) new EntrySet()); |
1075 |
tim |
1.1 |
} |
1076 |
|
|
|
1077 |
|
|
|
1078 |
|
|
/** |
1079 |
|
|
* Returns an enumeration of the keys in this table. |
1080 |
|
|
* |
1081 |
|
|
* @return an enumeration of the keys in this table. |
1082 |
dl |
1.23 |
* @see #keySet |
1083 |
tim |
1.1 |
*/ |
1084 |
dl |
1.4 |
public Enumeration<K> keys() { |
1085 |
tim |
1.1 |
return new KeyIterator(); |
1086 |
|
|
} |
1087 |
|
|
|
1088 |
|
|
/** |
1089 |
|
|
* Returns an enumeration of the values in this table. |
1090 |
|
|
* |
1091 |
|
|
* @return an enumeration of the values in this table. |
1092 |
dl |
1.23 |
* @see #values |
1093 |
tim |
1.1 |
*/ |
1094 |
dl |
1.4 |
public Enumeration<V> elements() { |
1095 |
tim |
1.1 |
return new ValueIterator(); |
1096 |
|
|
} |
1097 |
|
|
|
1098 |
dl |
1.4 |
/* ---------------- Iterator Support -------------- */ |
1099 |
tim |
1.11 |
|
1100 |
dl |
1.41 |
abstract class HashIterator { |
1101 |
|
|
int nextSegmentIndex; |
1102 |
|
|
int nextTableIndex; |
1103 |
|
|
HashEntry[] currentTable; |
1104 |
|
|
HashEntry<K, V> nextEntry; |
1105 |
dl |
1.30 |
HashEntry<K, V> lastReturned; |
1106 |
tim |
1.1 |
|
1107 |
dl |
1.41 |
HashIterator() { |
1108 |
dl |
1.8 |
nextSegmentIndex = segments.length - 1; |
1109 |
dl |
1.4 |
nextTableIndex = -1; |
1110 |
|
|
advance(); |
1111 |
tim |
1.1 |
} |
1112 |
|
|
|
1113 |
|
|
public boolean hasMoreElements() { return hasNext(); } |
1114 |
|
|
|
1115 |
dl |
1.41 |
final void advance() { |
1116 |
dl |
1.4 |
if (nextEntry != null && (nextEntry = nextEntry.next) != null) |
1117 |
|
|
return; |
1118 |
tim |
1.11 |
|
1119 |
dl |
1.4 |
while (nextTableIndex >= 0) { |
1120 |
tim |
1.12 |
if ( (nextEntry = (HashEntry<K,V>)currentTable[nextTableIndex--]) != null) |
1121 |
dl |
1.4 |
return; |
1122 |
|
|
} |
1123 |
tim |
1.11 |
|
1124 |
dl |
1.4 |
while (nextSegmentIndex >= 0) { |
1125 |
tim |
1.12 |
Segment<K,V> seg = (Segment<K,V>)segments[nextSegmentIndex--]; |
1126 |
dl |
1.4 |
if (seg.count != 0) { |
1127 |
|
|
currentTable = seg.table; |
1128 |
dl |
1.8 |
for (int j = currentTable.length - 1; j >= 0; --j) { |
1129 |
tim |
1.12 |
if ( (nextEntry = (HashEntry<K,V>)currentTable[j]) != null) { |
1130 |
dl |
1.8 |
nextTableIndex = j - 1; |
1131 |
dl |
1.4 |
return; |
1132 |
|
|
} |
1133 |
tim |
1.1 |
} |
1134 |
|
|
} |
1135 |
|
|
} |
1136 |
|
|
} |
1137 |
|
|
|
1138 |
dl |
1.4 |
public boolean hasNext() { return nextEntry != null; } |
1139 |
tim |
1.1 |
|
1140 |
dl |
1.4 |
HashEntry<K,V> nextEntry() { |
1141 |
|
|
if (nextEntry == null) |
1142 |
tim |
1.1 |
throw new NoSuchElementException(); |
1143 |
dl |
1.4 |
lastReturned = nextEntry; |
1144 |
|
|
advance(); |
1145 |
|
|
return lastReturned; |
1146 |
tim |
1.1 |
} |
1147 |
|
|
|
1148 |
|
|
public void remove() { |
1149 |
|
|
if (lastReturned == null) |
1150 |
|
|
throw new IllegalStateException(); |
1151 |
|
|
ConcurrentHashMap.this.remove(lastReturned.key); |
1152 |
|
|
lastReturned = null; |
1153 |
|
|
} |
1154 |
dl |
1.4 |
} |
1155 |
|
|
|
1156 |
dl |
1.41 |
final class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> { |
1157 |
dl |
1.4 |
public K next() { return super.nextEntry().key; } |
1158 |
|
|
public K nextElement() { return super.nextEntry().key; } |
1159 |
|
|
} |
1160 |
|
|
|
1161 |
dl |
1.41 |
final class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> { |
1162 |
dl |
1.4 |
public V next() { return super.nextEntry().value; } |
1163 |
|
|
public V nextElement() { return super.nextEntry().value; } |
1164 |
|
|
} |
1165 |
tim |
1.1 |
|
1166 |
dl |
1.30 |
|
1167 |
|
|
|
1168 |
|
|
/** |
1169 |
dl |
1.41 |
* Entry iterator. Exported Entry objects must write-through |
1170 |
|
|
* changes in setValue, even if the nodes have been cloned. So we |
1171 |
|
|
* cannot return internal HashEntry objects. Instead, the iterator |
1172 |
|
|
* itself acts as a forwarding pseudo-entry. |
1173 |
dl |
1.30 |
*/ |
1174 |
dl |
1.41 |
final class EntryIterator extends HashIterator implements Map.Entry<K,V>, Iterator<Entry<K,V>> { |
1175 |
dl |
1.30 |
public Map.Entry<K,V> next() { |
1176 |
|
|
nextEntry(); |
1177 |
|
|
return this; |
1178 |
|
|
} |
1179 |
|
|
|
1180 |
|
|
public K getKey() { |
1181 |
|
|
if (lastReturned == null) |
1182 |
|
|
throw new IllegalStateException("Entry was removed"); |
1183 |
|
|
return lastReturned.key; |
1184 |
|
|
} |
1185 |
|
|
|
1186 |
|
|
public V getValue() { |
1187 |
|
|
if (lastReturned == null) |
1188 |
|
|
throw new IllegalStateException("Entry was removed"); |
1189 |
|
|
return ConcurrentHashMap.this.get(lastReturned.key); |
1190 |
|
|
} |
1191 |
|
|
|
1192 |
|
|
public V setValue(V value) { |
1193 |
|
|
if (lastReturned == null) |
1194 |
|
|
throw new IllegalStateException("Entry was removed"); |
1195 |
|
|
return ConcurrentHashMap.this.put(lastReturned.key, value); |
1196 |
|
|
} |
1197 |
|
|
|
1198 |
|
|
public boolean equals(Object o) { |
1199 |
dl |
1.43 |
// If not acting as entry, just use default. |
1200 |
|
|
if (lastReturned == null) |
1201 |
|
|
return super.equals(o); |
1202 |
dl |
1.30 |
if (!(o instanceof Map.Entry)) |
1203 |
|
|
return false; |
1204 |
tim |
1.39 |
Map.Entry e = (Map.Entry)o; |
1205 |
|
|
return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue()); |
1206 |
|
|
} |
1207 |
dl |
1.30 |
|
1208 |
|
|
public int hashCode() { |
1209 |
dl |
1.43 |
// If not acting as entry, just use default. |
1210 |
|
|
if (lastReturned == null) |
1211 |
|
|
return super.hashCode(); |
1212 |
|
|
|
1213 |
dl |
1.30 |
Object k = getKey(); |
1214 |
|
|
Object v = getValue(); |
1215 |
|
|
return ((k == null) ? 0 : k.hashCode()) ^ |
1216 |
|
|
((v == null) ? 0 : v.hashCode()); |
1217 |
|
|
} |
1218 |
|
|
|
1219 |
|
|
public String toString() { |
1220 |
dl |
1.43 |
// If not acting as entry, just use default. |
1221 |
dl |
1.34 |
if (lastReturned == null) |
1222 |
|
|
return super.toString(); |
1223 |
|
|
else |
1224 |
|
|
return getKey() + "=" + getValue(); |
1225 |
dl |
1.30 |
} |
1226 |
|
|
|
1227 |
dl |
1.41 |
boolean eq(Object o1, Object o2) { |
1228 |
dl |
1.30 |
return (o1 == null ? o2 == null : o1.equals(o2)); |
1229 |
|
|
} |
1230 |
|
|
|
1231 |
tim |
1.1 |
} |
1232 |
|
|
|
1233 |
dl |
1.41 |
final class KeySet extends AbstractSet<K> { |
1234 |
dl |
1.4 |
public Iterator<K> iterator() { |
1235 |
|
|
return new KeyIterator(); |
1236 |
|
|
} |
1237 |
|
|
public int size() { |
1238 |
|
|
return ConcurrentHashMap.this.size(); |
1239 |
|
|
} |
1240 |
|
|
public boolean contains(Object o) { |
1241 |
|
|
return ConcurrentHashMap.this.containsKey(o); |
1242 |
|
|
} |
1243 |
|
|
public boolean remove(Object o) { |
1244 |
|
|
return ConcurrentHashMap.this.remove(o) != null; |
1245 |
|
|
} |
1246 |
|
|
public void clear() { |
1247 |
|
|
ConcurrentHashMap.this.clear(); |
1248 |
|
|
} |
1249 |
tim |
1.1 |
} |
1250 |
|
|
|
1251 |
dl |
1.41 |
final class Values extends AbstractCollection<V> { |
1252 |
dl |
1.4 |
public Iterator<V> iterator() { |
1253 |
|
|
return new ValueIterator(); |
1254 |
|
|
} |
1255 |
|
|
public int size() { |
1256 |
|
|
return ConcurrentHashMap.this.size(); |
1257 |
|
|
} |
1258 |
|
|
public boolean contains(Object o) { |
1259 |
|
|
return ConcurrentHashMap.this.containsValue(o); |
1260 |
|
|
} |
1261 |
|
|
public void clear() { |
1262 |
|
|
ConcurrentHashMap.this.clear(); |
1263 |
|
|
} |
1264 |
tim |
1.1 |
} |
1265 |
|
|
|
1266 |
dl |
1.41 |
final class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
1267 |
dl |
1.4 |
public Iterator<Map.Entry<K,V>> iterator() { |
1268 |
|
|
return new EntryIterator(); |
1269 |
|
|
} |
1270 |
|
|
public boolean contains(Object o) { |
1271 |
|
|
if (!(o instanceof Map.Entry)) |
1272 |
|
|
return false; |
1273 |
|
|
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
1274 |
|
|
V v = ConcurrentHashMap.this.get(e.getKey()); |
1275 |
|
|
return v != null && v.equals(e.getValue()); |
1276 |
|
|
} |
1277 |
|
|
public boolean remove(Object o) { |
1278 |
|
|
if (!(o instanceof Map.Entry)) |
1279 |
|
|
return false; |
1280 |
|
|
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
1281 |
dl |
1.13 |
return ConcurrentHashMap.this.remove(e.getKey(), e.getValue()); |
1282 |
dl |
1.4 |
} |
1283 |
|
|
public int size() { |
1284 |
|
|
return ConcurrentHashMap.this.size(); |
1285 |
|
|
} |
1286 |
|
|
public void clear() { |
1287 |
|
|
ConcurrentHashMap.this.clear(); |
1288 |
dl |
1.30 |
} |
1289 |
|
|
public Object[] toArray() { |
1290 |
|
|
// Since we don't ordinarily have distinct Entry objects, we |
1291 |
|
|
// must pack elements using exportable SimpleEntry |
1292 |
|
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1293 |
|
|
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1294 |
|
|
c.add(new SimpleEntry<K,V>(i.next())); |
1295 |
|
|
return c.toArray(); |
1296 |
|
|
} |
1297 |
|
|
public <T> T[] toArray(T[] a) { |
1298 |
|
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1299 |
|
|
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1300 |
|
|
c.add(new SimpleEntry<K,V>(i.next())); |
1301 |
|
|
return c.toArray(a); |
1302 |
|
|
} |
1303 |
|
|
|
1304 |
|
|
} |
1305 |
|
|
|
1306 |
|
|
/** |
1307 |
|
|
* This duplicates java.util.AbstractMap.SimpleEntry until this class |
1308 |
|
|
* is made accessible. |
1309 |
|
|
*/ |
1310 |
dl |
1.41 |
static final class SimpleEntry<K,V> implements Entry<K,V> { |
1311 |
tim |
1.39 |
K key; |
1312 |
|
|
V value; |
1313 |
dl |
1.30 |
|
1314 |
tim |
1.39 |
public SimpleEntry(K key, V value) { |
1315 |
|
|
this.key = key; |
1316 |
dl |
1.30 |
this.value = value; |
1317 |
tim |
1.39 |
} |
1318 |
dl |
1.30 |
|
1319 |
tim |
1.39 |
public SimpleEntry(Entry<K,V> e) { |
1320 |
|
|
this.key = e.getKey(); |
1321 |
dl |
1.30 |
this.value = e.getValue(); |
1322 |
tim |
1.39 |
} |
1323 |
|
|
|
1324 |
|
|
public K getKey() { |
1325 |
|
|
return key; |
1326 |
|
|
} |
1327 |
dl |
1.30 |
|
1328 |
tim |
1.39 |
public V getValue() { |
1329 |
|
|
return value; |
1330 |
|
|
} |
1331 |
|
|
|
1332 |
|
|
public V setValue(V value) { |
1333 |
|
|
V oldValue = this.value; |
1334 |
|
|
this.value = value; |
1335 |
|
|
return oldValue; |
1336 |
|
|
} |
1337 |
|
|
|
1338 |
|
|
public boolean equals(Object o) { |
1339 |
|
|
if (!(o instanceof Map.Entry)) |
1340 |
|
|
return false; |
1341 |
|
|
Map.Entry e = (Map.Entry)o; |
1342 |
|
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
1343 |
|
|
} |
1344 |
|
|
|
1345 |
|
|
public int hashCode() { |
1346 |
|
|
return ((key == null) ? 0 : key.hashCode()) ^ |
1347 |
|
|
((value == null) ? 0 : value.hashCode()); |
1348 |
|
|
} |
1349 |
|
|
|
1350 |
|
|
public String toString() { |
1351 |
|
|
return key + "=" + value; |
1352 |
|
|
} |
1353 |
dl |
1.30 |
|
1354 |
dl |
1.41 |
static boolean eq(Object o1, Object o2) { |
1355 |
dl |
1.30 |
return (o1 == null ? o2 == null : o1.equals(o2)); |
1356 |
dl |
1.4 |
} |
1357 |
tim |
1.1 |
} |
1358 |
|
|
|
1359 |
dl |
1.4 |
/* ---------------- Serialization Support -------------- */ |
1360 |
|
|
|
1361 |
tim |
1.1 |
/** |
1362 |
|
|
* Save the state of the <tt>ConcurrentHashMap</tt> |
1363 |
|
|
* instance to a stream (i.e., |
1364 |
|
|
* serialize it). |
1365 |
dl |
1.8 |
* @param s the stream |
1366 |
tim |
1.1 |
* @serialData |
1367 |
|
|
* the key (Object) and value (Object) |
1368 |
|
|
* for each key-value mapping, followed by a null pair. |
1369 |
|
|
* The key-value mappings are emitted in no particular order. |
1370 |
|
|
*/ |
1371 |
|
|
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
1372 |
|
|
s.defaultWriteObject(); |
1373 |
|
|
|
1374 |
|
|
for (int k = 0; k < segments.length; ++k) { |
1375 |
tim |
1.12 |
Segment<K,V> seg = (Segment<K,V>)segments[k]; |
1376 |
dl |
1.2 |
seg.lock(); |
1377 |
|
|
try { |
1378 |
tim |
1.11 |
HashEntry[] tab = seg.table; |
1379 |
dl |
1.4 |
for (int i = 0; i < tab.length; ++i) { |
1380 |
tim |
1.12 |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; e != null; e = e.next) { |
1381 |
dl |
1.4 |
s.writeObject(e.key); |
1382 |
|
|
s.writeObject(e.value); |
1383 |
|
|
} |
1384 |
|
|
} |
1385 |
tim |
1.16 |
} finally { |
1386 |
dl |
1.2 |
seg.unlock(); |
1387 |
|
|
} |
1388 |
tim |
1.1 |
} |
1389 |
|
|
s.writeObject(null); |
1390 |
|
|
s.writeObject(null); |
1391 |
|
|
} |
1392 |
|
|
|
1393 |
|
|
/** |
1394 |
|
|
* Reconstitute the <tt>ConcurrentHashMap</tt> |
1395 |
|
|
* instance from a stream (i.e., |
1396 |
|
|
* deserialize it). |
1397 |
dl |
1.8 |
* @param s the stream |
1398 |
tim |
1.1 |
*/ |
1399 |
|
|
private void readObject(java.io.ObjectInputStream s) |
1400 |
|
|
throws IOException, ClassNotFoundException { |
1401 |
|
|
s.defaultReadObject(); |
1402 |
|
|
|
1403 |
dl |
1.4 |
// Initialize each segment to be minimally sized, and let grow. |
1404 |
|
|
for (int i = 0; i < segments.length; ++i) { |
1405 |
tim |
1.11 |
segments[i].setTable(new HashEntry[1]); |
1406 |
dl |
1.4 |
} |
1407 |
tim |
1.1 |
|
1408 |
|
|
// Read the keys and values, and put the mappings in the table |
1409 |
dl |
1.9 |
for (;;) { |
1410 |
tim |
1.1 |
K key = (K) s.readObject(); |
1411 |
|
|
V value = (V) s.readObject(); |
1412 |
|
|
if (key == null) |
1413 |
|
|
break; |
1414 |
|
|
put(key, value); |
1415 |
|
|
} |
1416 |
|
|
} |
1417 |
|
|
} |
1418 |
tim |
1.11 |
|