org.apache.jdo.util
Class WeakValueHashMap

java.lang.Object
  extended byjava.util.AbstractMap
      extended byjava.util.HashMap
          extended byorg.apache.jdo.util.WeakValueHashMap
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable

public class WeakValueHashMap
extends java.util.HashMap

A WeakValueHashMap is implemented as a HashMap that maps keys to WeakValues. Because we don't have access to the innards of the HashMap, we have to wrap/unwrap value objects with WeakValues on every operation. Fortunately WeakValues are small, short-lived objects, so the added allocation overhead is tolerable. This implementaton directly extends java.util.HashMap.

Author:
Markus Fuchs
See Also:
HashMap, WeakReference, Serialized Form

Nested Class Summary
private  class WeakValueHashMap.Entry
          Internal class for entries.
private  class WeakValueHashMap.EntrySet
          Internal class for entry sets to unwrap/wrap WeakValues stored in the map.
private static class WeakValueHashMap.WeakValue
          We need this special class to keep the backward reference from the value to the key, so that we are able to remove the key if the value is garbage collected.
 
Nested classes inherited from class java.util.HashMap
 
Nested classes inherited from class java.util.AbstractMap
 
Field Summary
private  java.util.Set entrySet
           
private  java.util.Set hashEntrySet
           
private  java.lang.ref.ReferenceQueue queue
           
private  java.util.Collection values
           
 
Fields inherited from class java.util.HashMap
 
Fields inherited from class java.util.AbstractMap
 
Constructor Summary
WeakValueHashMap()
           
 
Method Summary
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more keys to the specified value.
 java.util.Set entrySet()
          Returns a Set view of the mappings in this map.
 java.lang.Object get(java.lang.Object key)
          Gets the value for the given key.
private  java.lang.Object getReferenceObject(java.lang.ref.WeakReference ref)
          A convenience method to return the object held by the weak reference or null if it does not exist.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
private  void processQueue()
          Removes all garbage collected values with their keys from the map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Puts a new (key,value) into the map.
 java.lang.Object remove(java.lang.Object key)
          Removes key and value for the given key.
 int size()
          Returns the number of key-value mappings in this map.
 java.util.Collection values()
          Returns a Collection view of the values contained in this map.
 
Methods inherited from class java.util.HashMap
clear, clone, keySet, putAll
 
Methods inherited from class java.util.AbstractMap
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

queue

private java.lang.ref.ReferenceQueue queue

hashEntrySet

private java.util.Set hashEntrySet

entrySet

private java.util.Set entrySet

values

private transient java.util.Collection values
Constructor Detail

WeakValueHashMap

public WeakValueHashMap()
Method Detail

size

public int size()
Returns the number of key-value mappings in this map.

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

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

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

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key.

Parameters:
key - key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key.

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value.

Parameters:
value - value whose presence in this map is to be tested
Returns:
true if this map maps one or more keys to this value.

get

public java.lang.Object get(java.lang.Object key)
Gets the value for the given key.

Parameters:
key - key whose associated value, if any, is to be returned
Returns:
the value to which this map maps the specified key.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Puts a new (key,value) into the map.

Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key or the value has been garbage collected by the garbage collector.

remove

public java.lang.Object remove(java.lang.Object key)
Removes key and value for the given key.

Parameters:
key - key whose mapping is to be removed from the map.
Returns:
previous value associated with specified key, or null if there was no mapping for key or the value has been garbage collected by the garbage collector.

getReferenceObject

private final java.lang.Object getReferenceObject(java.lang.ref.WeakReference ref)
A convenience method to return the object held by the weak reference or null if it does not exist.


processQueue

private void processQueue()
Removes all garbage collected values with their keys from the map. Since we don't know how much the ReferenceQueue.poll() operation costs, we should not call it every map operation.


entrySet

public java.util.Set entrySet()
Returns a Set view of the mappings in this map.

Returns:
a Set view of the mappings in this map.

values

public java.util.Collection values()
Returns a Collection view of the values contained in this map.

Returns:
a Collection view of the values contained in this map.