289 |
|
return false; |
290 |
|
} |
291 |
|
|
292 |
+ |
boolean replace(K key, int hash, V oldValue, V newValue) { |
293 |
+ |
lock(); |
294 |
+ |
try { |
295 |
+ |
int c = count; |
296 |
+ |
HashEntry[] tab = table; |
297 |
+ |
int index = hash & (tab.length - 1); |
298 |
+ |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
299 |
+ |
HashEntry<K,V> e = first; |
300 |
+ |
for (;;) { |
301 |
+ |
if (e == null) |
302 |
+ |
return false; |
303 |
+ |
if (e.hash == hash && key.equals(e.key)) |
304 |
+ |
break; |
305 |
+ |
e = e.next; |
306 |
+ |
} |
307 |
+ |
|
308 |
+ |
V v = e.value; |
309 |
+ |
if (v == null || !oldValue.equals(v)) |
310 |
+ |
return false; |
311 |
+ |
|
312 |
+ |
e.value = newValue; |
313 |
+ |
count = c; // write-volatile |
314 |
+ |
return true; |
315 |
+ |
|
316 |
+ |
} finally { |
317 |
+ |
unlock(); |
318 |
+ |
} |
319 |
+ |
} |
320 |
+ |
|
321 |
|
V put(K key, int hash, V value, boolean onlyIfAbsent) { |
322 |
|
lock(); |
323 |
|
try { |
825 |
|
} |
826 |
|
|
827 |
|
/** |
828 |
+ |
* Replace entry for key only if currently mapped to given value. |
829 |
+ |
* Acts as |
830 |
+ |
* <pre> |
831 |
+ |
* if (map.get(key).equals(oldValue)) { |
832 |
+ |
* map.put(key, newValue); |
833 |
+ |
* return true; |
834 |
+ |
* } else return false; |
835 |
+ |
* </pre> |
836 |
+ |
* except that the action is performed atomically. |
837 |
+ |
* @param key key with which the specified value is associated. |
838 |
+ |
* @param oldValue value expected to be associated with the specified key. |
839 |
+ |
* @param newValue value to be associated with the specified key. |
840 |
+ |
* @return true if the value was replaced |
841 |
+ |
* @throws NullPointerException if the specified key or values are |
842 |
+ |
* <tt>null</tt>. |
843 |
+ |
*/ |
844 |
+ |
public boolean replace(K key, V oldValue, V newValue) { |
845 |
+ |
if (oldValue == null || newValue == null) |
846 |
+ |
throw new NullPointerException(); |
847 |
+ |
int hash = hash(key); |
848 |
+ |
return segmentFor(hash).replace(key, hash, oldValue, newValue); |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
|
852 |
+ |
/** |
853 |
|
* Removes all mappings from this map. |
854 |
|
*/ |
855 |
|
public void clear() { |