| Method from org.hibernate.search.util.WeakIdentityHashMap Detail: |
public void clear() {
// clear out ref queue. We don't need to expunge entries
// since table is getting cleared.
while ( queue.poll() != null )
;
modCount++;
Entry tab[] = table;
for ( int i = 0; i < tab.length; ++i )
tab[i] = null;
size = 0;
// Allocation of array may have caused GC, which may have caused
// additional entries to go stale. Removing these entries from the
// reference queue will make them eligible for reclamation.
while ( queue.poll() != null )
;
}
Removes all mappings from this map. |
public boolean containsKey(Object key) {
return getEntry( key ) != null;
}
Returns true if this map contains a mapping for the
specified key. |
public boolean containsValue(Object value) {
if ( value == null )
return containsNullValue();
Entry tab[] = getTable();
for ( int i = tab.length; i-- > 0; )
for ( Entry e = tab[i]; e != null; e = e.next )
if ( value.equals( e.value ) )
return true;
return false;
}
Returns true if this map maps one or more keys to the
specified value. |
public Set entrySet() {
Set< Map.Entry< K,V > > es = entrySet;
return ( es != null ?
es :
( entrySet = new EntrySet() ) );
}
Returns a collection view of the mappings contained in this map. Each
element in the returned collection is a Map.Entry. The
collection is backed by the map, so changes to the map are reflected in
the collection, and vice-versa. The collection supports element
removal, which removes the corresponding mapping from the map, via the
Iterator.remove, Collection.remove,
removeAll, retainAll, and clear operations.
It does not support the add or addAll operations. |
public V get(Object key) {
Object k = maskNull( key );
int h = hash( k );
Entry< K,V >[] tab = getTable();
int index = indexFor( h, tab.length );
Entry< K,V > e = tab[index];
while ( e != null ) {
if ( e.hash == h && k == e.get() )
return e.value;
e = e.next;
}
return null;
}
Returns the value to which the specified key is mapped in this weak
hash map, or null if the map contains no mapping for
this key. A return value of null does not necessarily
indicate that the map contains no mapping for the key; it is also
possible that the map explicitly maps the key to null. The
containsKey method may be used to distinguish these two
cases. |
WeakIdentityHashMap.Entry getEntry(Object key) {
Object k = maskNull( key );
int h = hash( k );
Entry< K,V >[] tab = getTable();
int index = indexFor( h, tab.length );
Entry< K,V > e = tab[index];
while ( e != null && !( e.hash == h && k == e.get() ) )
e = e.next;
return e;
}
Returns the entry associated with the specified key in the HashMap.
Returns null if the HashMap contains no mapping for this key. |
int hash(Object x) {
int h = System.identityHashCode( x );
return h - ( h < < 7 ); // that is,, -127 * h
}
Return a hash code for non-null Object x. |
static int indexFor(int h,
int length) {
return h & ( length - 1 );
}
Return index for hash code h. |
public boolean isEmpty() {
return size() == 0;
}
Returns true if this map contains no key-value mappings.
This result is a snapshot, and may not reflect unprocessed
entries that will be removed before next attempted access
because they are no longer referenced. |
public Set keySet() {
Set ks = keySet;
return ( ks != null ?
ks :
( keySet = new KeySet() ) );
}
Returns a set view of the keys contained in this map. The set is
backed by the map, so changes to the map are reflected in the set, and
vice-versa. The set supports element removal, which removes the
corresponding mapping from this map, via the Iterator.remove,
Set.remove, removeAll, retainAll, and
clear operations. It does not support the add or
addAll operations. |
public V put(K key,
V value) {
K k = maskNull( key );
int h = hash( k );
Entry< K,V >[] tab = getTable();
int i = indexFor( h, tab.length );
for ( Entry< K,V > e = tab[i]; e != null; e = e.next ) {
if ( h == e.hash && k == e.get() ) {
V oldValue = e.value;
if ( value != oldValue )
e.value = value;
return oldValue;
}
}
modCount++;
tab[i] = new Entry< K,V >( k, value, queue, h, tab[i] );
if ( ++size >= threshold )
resize( tab.length * 2 );
return null;
}
Associates the specified value with the specified key in this map.
If the map previously contained a mapping for this key, the old
value is replaced. |
public void putAll(Map t) {
// Expand enough to hold t's elements without resizing.
int n = t.size();
if ( n == 0 )
return;
if ( n >= threshold ) {
n = (int) ( n / loadFactor + 1 );
if ( n > MAXIMUM_CAPACITY )
n = MAXIMUM_CAPACITY;
int capacity = table.length;
while ( capacity < n )
capacity < < = 1;
resize( capacity );
}
for ( Iterator i = t.entrySet().iterator(); i.hasNext(); ) {
Map.Entry< K,V > e = (Map.Entry< K,V >) i.next(); //FIXME should not have to cast
put( e.getKey(), e.getValue() );
}
}
Copies all of the mappings from the specified map to this map These
mappings will replace any mappings that this map had for any of the
keys currently in the specified map. |
public V remove(Object key) {
Object k = maskNull( key );
int h = hash( k );
Entry< K,V >[] tab = getTable();
int i = indexFor( h, tab.length );
Entry< K,V > prev = tab[i];
Entry< K,V > e = prev;
while ( e != null ) {
Entry< K,V > next = e.next;
if ( h == e.hash && k == e.get() ) {
modCount++;
size--;
if ( prev == e )
tab[i] = next;
else
prev.next = next;
return e.value;
}
prev = e;
e = next;
}
return null;
}
Removes the mapping for this key from this map if present. |
WeakIdentityHashMap.Entry removeMapping(Object o) {
if ( !( o instanceof Map.Entry ) )
return null;
Entry[] tab = getTable();
Map.Entry entry = (Map.Entry) o;
Object k = maskNull( entry.getKey() );
int h = hash( k );
int i = indexFor( h, tab.length );
Entry prev = tab[i];
Entry e = prev;
while ( e != null ) {
Entry next = e.next;
if ( h == e.hash && e.equals( entry ) ) {
modCount++;
size--;
if ( prev == e )
tab[i] = next;
else
prev.next = next;
return e;
}
prev = e;
e = next;
}
return null;
}
Special version of remove needed by Entry set |
public boolean removeValue(Object value) {
if ( value == null )
return removeNullValue();
Entry tab[] = getTable();
Set keys = new HashSet();
for ( int i = tab.length; i-- > 0; )
for ( Entry e = tab[i]; e != null; e = e.next )
if ( value.equals( e.value ) )
keys.add( e.getKey() );
for ( Object key : keys ) remove( key );
return !keys.isEmpty();
}
Remove elements having the according value.
Intended to avoid concurrent access exceptions
It is expected that nobody add a key being removed by value |
void resize(int newCapacity) {
// assert (newCapacity & -newCapacity) == newCapacity; // power of 2
Entry< K,V >[] oldTable = getTable();
int oldCapacity = oldTable.length;
// check if needed
if ( size < threshold || oldCapacity > newCapacity )
return;
Entry< K,V >[] newTable = new Entry[newCapacity];
transfer( oldTable, newTable );
table = newTable;
/*
* If ignoring null elements and processing ref queue caused massive
* shrinkage, then restore old table. This should be rare, but avoids
* unbounded expansion of garbage-filled tables.
*/
if ( size >= threshold / 2 ) {
threshold = (int) ( newCapacity * loadFactor );
}
else {
expungeStaleEntries();
transfer( newTable, oldTable );
table = oldTable;
}
}
Rehashes the contents of this map into a new HashMap instance
with a larger capacity. This method is called automatically when the
number of keys in this map exceeds its capacity and load factor.
Note that this method is a no-op if it's called with newCapacity ==
2*MAXIMUM_CAPACITY (which is Integer.MIN_VALUE). |
public int size() {
if ( size == 0 )
return 0;
expungeStaleEntries();
return size;
}
Returns the number of key-value mappings in this map.
This result is a snapshot, and may not reflect unprocessed
entries that will be removed before next attempted access
because they are no longer referenced. |
public Collection values() {
Collection vs = values;
return ( vs != null ?
vs :
( values = new Values() ) );
}
Returns a collection view of the values contained in this map. The
collection is backed by the map, so changes to the map are reflected in
the collection, and vice-versa. The collection supports element
removal, which removes the corresponding mapping from this map, via the
Iterator.remove, Collection.remove,
removeAll, retainAll, and clear operations.
It does not support the add or addAll operations. |