org.aspsimon.termcomp.infrastructure.utility
Class AutoHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by org.aspsimon.termcomp.infrastructure.utility.AutoHashMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>

public class AutoHashMap<K,V>
extends java.util.HashMap<K,V>

This class extends HashMap to add the functionality for inserting default values into the map if the requested key does not already exist. The default value, if none is set is null, the user is required to set his own default value if she wants to use something else. Of course, this value should be of the type V.

Version:
$Revision: 104 $
Author:
binabik
See Also:
Serialized Form

Constructor Summary
AutoHashMap()
          Default constructor.
AutoHashMap(V defVal)
          Constructs a new AutoHashMap setting a default value.
 
Method Summary
 V get(java.lang.Object key)
          Gets a value from the Map.
 V getDefVal()
          Get the default value set for this map.
 void setDefVal(V defVal)
          Set the default value for initializing new elements in this map.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

AutoHashMap

public AutoHashMap()
Default constructor. Initializes the parent HashMap and sets the default value to an empty new V.


AutoHashMap

public AutoHashMap(V defVal)
Constructs a new AutoHashMap setting a default value.

Parameters:
defVal - The default value to set.
Method Detail

get

public V get(java.lang.Object key)
Gets a value from the Map. If the key does not exist, it is added to the map and the default value is returned.

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class java.util.HashMap<K,V>
Parameters:
key - The key whose associated value is to be returned.
Returns:
The value to which this map maps the specified key, or the default value if the map contains no mapping for this key.

getDefVal

public V getDefVal()
Get the default value set for this map. May be an empty instance of V.

Returns:
The default value set for this map.

setDefVal

public void setDefVal(V defVal)
Set the default value for initializing new elements in this map.

Parameters:
defVal - The default value.