Top Description Inners Fields Constructors Methods
org.jruby.util

public Class WeakIdentityHashMap

extends GenericMap
implements Map
Class Inheritance
All Implemented Interfaces
java.util.Map
Imports
java.lang.ref.ReferenceQueue, .WeakReference, java.util.Iterator, .Map, .NoSuchElementException

Class WeakIdentityHashMap is a hash map that hashes objects based on System.identityHashMap, and holds weakly onto the key. This fails if values make reference to the keys!
Author
Kresten Krab Thorup
Since
1.0
Version
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
pack-priv class

Field Summary

Modifier and TypeField and Description
private static final float
private static final Object
private final ReferenceQueue<T>
private int
range

the current range for table.

private float
private WeakIdentityHashMap.Entry[]
table

the hash index

Inherited from org.jruby.util.GenericMap:
size

Constructor Summary

AccessConstructor and Description
public
WeakIdentityHashMap()

the default and only constructor

public

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Implements java.util.Map.clear.

Removes all of the mappings from this map (optional operation).

private void
clear(int size)

public boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Overrides org.jruby.util.GenericMap.containsKey.

Implements java.util.Map.containsKey.

return the element with the given key

private boolean
containsKey(int hash, Object masked_key)

protected Iterator<E>
private void
public Object
get(Object
the key whose associated value is to be returned
key
)

Implements java.util.Map.get.

return the element with the given key

private Object
get(int hash, Object masked_key)

private void
grow()

private int
index(int hash)

translate hash code bucket to index

public boolean
isEmpty()

Overrides org.jruby.util.GenericMap.isEmpty.

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

protected final boolean
protected final int
private Object
public Object
put(Object
key with which the specified value is to be associated
key
,
Object
value to be associated with the specified key
value
)

Implements java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation).

private Object
put(int hash, Object masked_key, Object value)

public Object
remove(Object
key whose mapping is to be removed from the map
key
)

Implements java.util.Map.remove.

Removes the mapping for a key from this map if it is present (optional operation).

public Object
remove(int hash, Object key)

private void
public int
size()

Overrides org.jruby.util.GenericMap.size.

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.

private static Object
protected void
Inherited from org.jruby.util.GenericMap:
containsValueentrySetequalshashCodekeyIteratorkeySetputAllvalueEqualsvalueHashvalueIteratorvalues

Field Detail

DEFAULT_RATIOback to summary
private static final float DEFAULT_RATIO
NULL_KEYback to summary
private static final Object NULL_KEY
queueback to summary
private final ReferenceQueue<T> queue
rangeback to summary
private int range

the current range for table.

ratioback to summary
private float ratio
tableback to summary
private WeakIdentityHashMap.Entry[] table

the hash index

Constructor Detail

WeakIdentityHashMapback to summary
public WeakIdentityHashMap()

the default and only constructor

WeakIdentityHashMapback to summary
public WeakIdentityHashMap(int size)

Method Detail

clearback to summary
public void clear()

Implements java.util.Map.clear.

Doc from java.util.Map.clear.

Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.

clearback to summary
private void clear(int size)
containsKeyback to summary
public boolean containsKey(Object key)

Overrides org.jruby.util.GenericMap.containsKey.

Implements java.util.Map.containsKey.

return the element with the given key

Parameters
key:Object

Doc from java.util.Map.containsKey.

key whose presence in this map is to be tested

Returns:boolean

Doc from java.util.Map.containsKey.

true if this map contains a mapping for the specified key

Annotations
@Override
containsKeyback to summary
private boolean containsKey(int hash, Object masked_key)
entryIteratorback to summary
protected Iterator<E> entryIterator()

Implements abstract org.jruby.util.GenericMap.entryIterator.

expungeback to summary
private void expunge()
getback to summary
public Object get(Object key)

Implements java.util.Map.get.

return the element with the given key

Parameters
key:Object

Doc from java.util.Map.get.

the key whose associated value is to be returned

Returns:Object

Doc from java.util.Map.get.

the value to which the specified key is mapped, or null if this map contains no mapping for the key

getback to summary
private Object get(int hash, Object masked_key)
growback to summary
private void grow()
indexback to summary
private int index(int hash)

translate hash code bucket to index

isEmptyback to summary
public boolean isEmpty()

Overrides org.jruby.util.GenericMap.isEmpty.

Implements java.util.Map.isEmpty.

Doc from java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

Returns:boolean

true if this map contains no key-value mappings

Annotations
@Override
keyEqualsback to summary
protected final boolean keyEquals(Object key1, Object key2)

Overrides org.jruby.util.GenericMap.keyEquals.

Annotations
@Override
keyHashback to summary
protected final int keyHash(Object key)

Overrides org.jruby.util.GenericMap.keyHash.

Annotations
@Override
maskKeyback to summary
private Object maskKey(Object key)
putback to summary
public Object put(Object key, Object value)

Implements java.util.Map.put.

Doc from java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
key:Object

key with which the specified value is to be associated

value:Object

value to be associated with the specified key

Returns:Object

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

putback to summary
private Object put(int hash, Object masked_key, Object value)
removeback to summary
public Object remove(Object key)

Implements java.util.Map.remove.

Doc from java.util.Map.remove.

Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed. (The map can contain at most one such mapping.)

Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

If this map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.

The map will not contain a mapping for the specified key once the call returns.

Parameters
key:Object

key whose mapping is to be removed from the map

Returns:Object

the previous value associated with key, or null if there was no mapping for key.

removeback to summary
public Object remove(int hash, Object key)
removeEntryback to summary
private void removeEntry(WeakIdentityHashMap.Entry ent)
sizeback to summary
public int size()

Overrides org.jruby.util.GenericMap.size.

Implements java.util.Map.size.

Doc from java.util.Map.size.

Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of key-value mappings in this map

Annotations
@Override
unmaskKeyback to summary
private static Object unmaskKey(Object key)
valueRemovedback to summary
protected void valueRemoved(Object value)
org.jruby.util back to summary

pack-priv Class WeakIdentityHashMap.Entry

extends WeakReference
implements Entry
Class Inheritance
All Implemented Interfaces
java.util.Map.Entry

Field Summary

Modifier and TypeField and Description
private final int
private WeakIdentityHashMap.Entry
private Object

Constructor Summary

AccessConstructor and Description
pack-priv
Entry(int key_hash, Object masked_key, Object value, WeakIdentityHashMap.Entry next, ReferenceQueue<T> q)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
object to be compared for equality with this map entry
other
)

Overrides java.lang.Object.equals.

Implements java.util.Map.Entry.equals.

Compares the specified object with this entry for equality.

public Object
getKey()

Implements java.util.Map.Entry.getKey.

Returns the key corresponding to this entry.

pack-priv Object
public Object
getValue()

Implements java.util.Map.Entry.getValue.

Returns the value corresponding to this entry.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.Entry.hashCode.

Returns the hash code value for this map entry.

pack-priv boolean
sameKey(int hash, Object masked_key)

public Object
setValue(Object
new value to be stored in this entry
value
)

Implements java.util.Map.Entry.setValue.

Replaces the value corresponding to this entry with the specified value (optional operation).

Field Detail

key_hashback to summary
private final int key_hash
nextback to summary
private WeakIdentityHashMap.Entry next

Hides java.lang.ref.Reference.next.

valueback to summary
private Object value

Constructor Detail

Entryback to summary
pack-priv Entry(int key_hash, Object masked_key, Object value, WeakIdentityHashMap.Entry next, ReferenceQueue<T> q)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Implements java.util.Map.Entry.equals.

Doc from java.util.Map.Entry.equals.

Compares the specified object with this entry for equality. Returns true if the given object is also a map entry and the two entries represent the same mapping. More formally, two entries e1 and e2 represent the same mapping if

    (e1.getKey()==null ?
     e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &&
    (e1.getValue()==null ?
     e2.getValue()==null : e1.getValue().equals(e2.getValue()))
This ensures that the equals method works properly across different implementations of the Map.Entry interface.
Parameters
other:Object

object to be compared for equality with this map entry

Returns:boolean

true if the specified object is equal to this map entry

Annotations
@Override
getKeyback to summary
public Object getKey()

Implements java.util.Map.Entry.getKey.

Doc from java.util.Map.Entry.getKey.

Returns the key corresponding to this entry.

Returns:Object

the key corresponding to this entry

getMaskedKeyback to summary
pack-priv Object getMaskedKey()
getValueback to summary
public Object getValue()

Implements java.util.Map.Entry.getValue.

Doc from java.util.Map.Entry.getValue.

Returns the value corresponding to this entry. If the mapping has been removed from the backing map (by the iterator's remove operation), the results of this call are undefined.

Returns:Object

the value corresponding to this entry

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.Entry.hashCode.

Doc from java.util.Map.Entry.hashCode.

Returns the hash code value for this map entry. The hash code of a map entry e is defined to be:

    (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
    (e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two Entries e1 and e2, as required by the general contract of Object.hashCode.
Returns:int

the hash code value for this map entry

Annotations
@Override
sameKeyback to summary
pack-priv boolean sameKey(int hash, Object masked_key)
setValueback to summary
public Object setValue(Object value)

Implements java.util.Map.Entry.setValue.

Doc from java.util.Map.Entry.setValue.

Replaces the value corresponding to this entry with the specified value (optional operation). (Writes through to the map.) The behavior of this call is undefined if the mapping has already been removed from the map (by the iterator's remove operation).

Parameters
value:Object

new value to be stored in this entry

Returns:Object

old value corresponding to the entry

org.jruby.util back to summary

pack-priv final Class WeakIdentityHashMap.EntryIterator

extends Object
implements Iterator
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Field Summary

Modifier and TypeField and Description
private WeakIdentityHashMap.Entry
private int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

private void
public Object
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

entryback to summary
private WeakIdentityHashMap.Entry entry
idxback to summary
private int idx

Constructor Detail

EntryIteratorback to summary
pack-priv EntryIterator()

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

locateNextback to summary
private void locateNext()
nextback to summary
public Object next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:Object

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.