V replace(K key, int hash, V newValue) { lock(); try { removeStale(); HashEntry<K, V> e = getFirst(hash); while(e != null && (e.hash != hash || !key.equals(e.keyRef.get()))) e = e.next; V oldValue = null; if(e != null) { oldValue = e.value; e.value = newValue; } return oldValue; } finally { unlock(); } }
/** * Maps the specified key to the specified value in this table. * Neither the key nor the value can be null. * * <p> The value can be retrieved by calling the <tt>get</tt> method * with a key that is equal to the original key. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt> * @throws NullPointerException if the specified key or value is null */ public V put(K key, V value) { if(value == null) throw new NullPointerException(); int hash = hash(key.hashCode()); return segmentFor(hash).put(key, hash, value, false); }
boolean containsValue(Object value) { if(count != 0) { // read-volatile HashEntry<K, V>[] tab = table; int len = tab.length; for(int i = 0; i < len; i++) { for(HashEntry<K, V> e = tab[i]; e != null; e = e.next) { V v = e.value; if(v == null) // recheck v = readValueUnderLock(e); if(value.equals(v)) return true; } } } return false; }
int c = segments[i].count; mcsum += mc[i] = segments[i].modCount; if(segments[i].containsValue(value)) return true; segments[i].lock(); boolean found = false; try { for(int i = 0; i < segments.length; ++i) { if(segments[i].containsValue(value)) { found = true; break; segments[i].unlock();
lock(); try { if(!weakRemove) removeStale(); int c = count - 1; HashEntry<K, V>[] tab = table; unlock();
int c = segments[i].count; mcsum += mc[i] = segments[i].modCount; if(segments[i].containsValue(value)) return true; segments[i].lock(); boolean found = false; try { for(int i = 0; i < segments.length; ++i) { if(segments[i].containsValue(value)) { found = true; break; segments[i].unlock();
lock(); try { if(!weakRemove) removeStale(); int c = count - 1; HashEntry<K, V>[] tab = table; unlock();
V put(K key, int hash, V value, boolean onlyIfAbsent) { lock(); try { removeStale(); int c = count; if(c++ > threshold) {// ensure capacity int reduced = rehash(); if(reduced > 0) // adjust from possible weak cleanups count = (c -= reduced) - 1; // write-volatile unlock();
V put(K key, int hash, V value, boolean onlyIfAbsent) { lock(); try { removeStale(); int c = count; if(c++ > threshold) {// ensure capacity int reduced = rehash(); if(reduced > 0) // adjust from possible weak cleanups count = (c -= reduced) - 1; // write-volatile unlock();
/** * Maps the specified key to the specified value in this table. * Neither the key nor the value can be null. * * <p> The value can be retrieved by calling the <tt>get</tt> method * with a key that is equal to the original key. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt> * @throws NullPointerException if the specified key or value is null */ public V put(K key, V value) { if(value == null) throw new NullPointerException(); int hash = hash(key.hashCode()); return segmentFor(hash).put(key, hash, value, false); }
void clear() { if(count != 0) { lock(); try { HashEntry<K, V>[] tab = table; for(int i = 0; i < tab.length; i++) tab[i] = null; ++modCount; // replace the reference queue to avoid unnecessary stale cleanups refQueue = new ReferenceQueue<K>(); count = 0; // write-volatile } finally { unlock(); } } } }
/** * Reads value field of an entry under lock. Called if value * field ever appears to be null. This is possible only if a * compiler happens to reorder a HashEntry initialization with * its table assignment, which is legal under memory model * but is not known to ever occur. */ V readValueUnderLock(HashEntry<K, V> e) { lock(); try { removeStale(); return e.value; } finally { unlock(); } }
void clear() { if(count != 0) { lock(); try { HashEntry<K, V>[] tab = table; for(int i = 0; i < tab.length; i++) tab[i] = null; ++modCount; // replace the reference queue to avoid unnecessary stale cleanups refQueue = new ReferenceQueue<K>(); count = 0; // write-volatile } finally { unlock(); } } } }
V replace(K key, int hash, V newValue) { lock(); try { removeStale(); HashEntry<K, V> e = getFirst(hash); while(e != null && (e.hash != hash || !key.equals(e.keyRef.get()))) e = e.next; V oldValue = null; if(e != null) { oldValue = e.value; e.value = newValue; } return oldValue; } finally { unlock(); } }