Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

gnu.java.util
Class WeakIdentityHashMap  view WeakIdentityHashMap download WeakIdentityHashMap.java

java.lang.Object
  extended byjava.util.AbstractMap
      extended bygnu.java.util.WeakIdentityHashMap
All Implemented Interfaces:
java.util.Map

public class WeakIdentityHashMap
extends java.util.AbstractMap
implements java.util.Map

A weak hash map has only weak references to the key. This means that it allows the key to be garbage collected if it is not used otherwise. If this happens, the entry will eventually disappear from the map, asynchronously.

Other strange behaviors to be aware of: The size of this map may spontaneously shrink (even if you use a synchronized map and synchronize it); it behaves as if another thread removes entries from this table without synchronization. The entry set returned by entrySet has similar phenomenons: The size may spontaneously shrink, or an entry, that was in the set before, suddenly disappears.

A weak hash map is not meant for caches; use a normal map, with soft references as values instead, or try LinkedHashMap.

The weak hash map supports null values and null keys. The null key is never deleted from the map (except explictly of course). The performance of the methods are similar to that of a hash map.

The value objects are strongly referenced by this table. So if a value object maintains a strong reference to the key (either direct or indirect) the key will never be removed from this map. According to Sun, this problem may be fixed in a future release. It is not possible to do it with the jdk 1.2 reference model, though.


Nested Class Summary
private static class WeakIdentityHashMap.WeakBucket
          A bucket is a weak reference to the key, that contains a strong reference to the value, a pointer to the next bucket and its slot number.
private  class WeakIdentityHashMap.WeakEntrySet
          The entry set.
 
Nested classes inherited from class java.util.AbstractMap
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Field Summary
(package private)  WeakIdentityHashMap.WeakBucket[] buckets
          The hash buckets.
private static int DEFAULT_CAPACITY
          The default capacity for an instance of HashMap.
private static float DEFAULT_LOAD_FACTOR
          The default load factor of a HashMap.
private  float loadFactor
          The load factor of this WeakIdentityHashMap.
(package private)  int modCount
          The number of structural modifications.
(package private) static java.lang.Object NULL_KEY
          This is used instead of the key value null.
private  java.lang.ref.ReferenceQueue queue
          The reference queue where our buckets (which are WeakReferences) are registered to.
(package private)  int size
          The number of entries in this hash map.
private  WeakIdentityHashMap.WeakEntrySet theEntrySet
          The entry set returned by entrySet().
private  int threshold
          The rounded product of the capacity (i.e.
 
Fields inherited from class java.util.AbstractMap
 
Constructor Summary
WeakIdentityHashMap()
          Creates a new weak hash map with default load factor and default capacity.
WeakIdentityHashMap(int initialCapacity)
          Creates a new weak hash map with default load factor and the given capacity.
WeakIdentityHashMap(int initialCapacity, float loadFactor)
          Creates a new weak hash map with the given initial capacity and load factor.
WeakIdentityHashMap(java.util.Map m)
          Construct a new WeakIdentityHashMap with the same mappings as the given map.
 
Method Summary
(package private)  void cleanQueue()
          Cleans the reference queue.
 void clear()
          Clears all entries from this map.
 boolean containsKey(java.lang.Object key)
          Tells if the map contains the given key.
 boolean containsValue(java.lang.Object value)
          Returns true if the map contains at least one key which points to the specified object as a value.
 java.util.Set entrySet()
          Returns a set representation of the entries in this map.
 java.lang.Object get(java.lang.Object key)
          Gets the value the key is mapped to.
private  int hash(java.lang.Object key)
          Simply hashes a non-null Object to its array index.
private  void internalAdd(java.lang.Object key, java.lang.Object value)
          Adds a new key/value pair to the hash map.
private  WeakIdentityHashMap.WeakBucket.WeakEntry internalGet(java.lang.Object key)
          Finds the entry corresponding to key.
(package private)  void internalRemove(WeakIdentityHashMap.WeakBucket bucket)
          Removes a bucket from this hash map, if it wasn't removed before (e.g.
 boolean isEmpty()
          Tells if the map is empty.
 java.util.Set keySet()
          Returns a set representation of the keys in this map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Adds a new key/value mapping to this map.
 void putAll(java.util.Map m)
          Puts all of the mappings from the given map into this one.
private  void rehash()
          Rehashes this hashtable.
 java.lang.Object remove(java.lang.Object key)
          Removes the key and the corresponding value from this map.
 int size()
          Returns the size of this hash map.
 java.util.Collection values()
          Returns a collection representation of the values in this map.
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

DEFAULT_CAPACITY

private static final int DEFAULT_CAPACITY
The default capacity for an instance of HashMap. Sun's documentation mildly suggests that this (11) is the correct value.

See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR

private static final float DEFAULT_LOAD_FACTOR
The default load factor of a HashMap.

See Also:
Constant Field Values

NULL_KEY

static final java.lang.Object NULL_KEY
This is used instead of the key value null. It is needed to distinguish between an null key and a removed key.


queue

private final java.lang.ref.ReferenceQueue queue
The reference queue where our buckets (which are WeakReferences) are registered to.


size

int size
The number of entries in this hash map.


loadFactor

private float loadFactor
The load factor of this WeakIdentityHashMap. This is the maximum ratio of size versus number of buckets. If size grows the number of buckets must grow, too.


threshold

private int threshold
The rounded product of the capacity (i.e. number of buckets) and the load factor. When the number of elements exceeds the threshold, the HashMap calls rehash().


modCount

int modCount
The number of structural modifications. This is used by iterators, to see if they should fail. This doesn't count the silent key removals, when a weak reference is cleared by the garbage collection. Instead the iterators must make sure to have strong references to the entries they rely on.


theEntrySet

private final WeakIdentityHashMap.WeakEntrySet theEntrySet
The entry set returned by entrySet().


buckets

WeakIdentityHashMap.WeakBucket[] buckets
The hash buckets. These are linked lists. Package visible for use in nested classes.

Constructor Detail

WeakIdentityHashMap

public WeakIdentityHashMap()
Creates a new weak hash map with default load factor and default capacity.


WeakIdentityHashMap

public WeakIdentityHashMap(int initialCapacity)
Creates a new weak hash map with default load factor and the given capacity.


WeakIdentityHashMap

public WeakIdentityHashMap(int initialCapacity,
                           float loadFactor)
Creates a new weak hash map with the given initial capacity and load factor.


WeakIdentityHashMap

public WeakIdentityHashMap(java.util.Map m)
Construct a new WeakIdentityHashMap with the same mappings as the given map. The WeakIdentityHashMap has a default load factor of 0.75.

Since:
1.3
Method Detail

hash

private int hash(java.lang.Object key)
Simply hashes a non-null Object to its array index.


cleanQueue

void cleanQueue()
Cleans the reference queue. This will poll all references (which are WeakBuckets) from the queue and remove them from this map. This will not change modCount, even if it modifies the map. The iterators have to make sure that nothing bad happens.
Currently the iterator maintains a strong reference to the key, so that is no problem.


rehash

private void rehash()
Rehashes this hashtable. This will be called by the add() method if the size grows beyond the threshold. It will grow the bucket size at least by factor two and allocates new buckets.


internalGet

private WeakIdentityHashMap.WeakBucket.WeakEntry internalGet(java.lang.Object key)
Finds the entry corresponding to key. Since it returns an Entry it will also prevent the key from being removed under us.


internalAdd

private void internalAdd(java.lang.Object key,
                         java.lang.Object value)
Adds a new key/value pair to the hash map.


internalRemove

void internalRemove(WeakIdentityHashMap.WeakBucket bucket)
Removes a bucket from this hash map, if it wasn't removed before (e.g. one time through rehashing and one time through reference queue). Package visible for use in nested classes.


size

public int size()
Returns the size of this hash map. Note that the size() may shrink spontaneously, if the some of the keys were only weakly reachable.

Specified by:
size in interface java.util.Map

isEmpty

public boolean isEmpty()
Tells if the map is empty. Note that the result may change spontanously, if all of the keys were only weakly reachable.

Specified by:
isEmpty in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Tells if the map contains the given key. Note that the result may change spontanously, if the key was only weakly reachable.

Specified by:
containsKey in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)
Gets the value the key is mapped to.

Specified by:
get in interface java.util.Map

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Adds a new key/value mapping to this map.

Specified by:
put in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key and the corresponding value from this map.

Specified by:
remove in interface java.util.Map

entrySet

public java.util.Set entrySet()
Returns a set representation of the entries in this map. This set will not have strong references to the keys, so they can be silently removed. The returned set has therefore the same strange behaviour (shrinking size(), disappearing entries) as this weak hash map.

Specified by:
entrySet in interface java.util.Map

clear

public void clear()
Clears all entries from this map.

Specified by:
clear in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if the map contains at least one key which points to the specified object as a value. Note that the result may change spontanously, if its key was only weakly reachable.

Specified by:
containsValue in interface java.util.Map

keySet

public java.util.Set keySet()
Returns a set representation of the keys in this map. This set will not have strong references to the keys, so they can be silently removed. The returned set has therefore the same strange behaviour (shrinking size(), disappearing entries) as this weak hash map.

Specified by:
keySet in interface java.util.Map

putAll

public void putAll(java.util.Map m)
Puts all of the mappings from the given map into this one. If the key already exists in this map, its value is replaced.

Specified by:
putAll in interface java.util.Map

values

public java.util.Collection values()
Returns a collection representation of the values in this map. This collection will not have strong references to the keys, so mappings can be silently removed. The returned collection has therefore the same strange behaviour (shrinking size(), disappearing entries) as this weak hash map.

Specified by:
values in interface java.util.Map