jpaul.DataStructs
Class MapWithDefault<K,V>

java.lang.Object
  extended by jpaul.DataStructs.MapWrapper<K,V>
      extended by jpaul.DataStructs.MapWithDefault<K,V>
All Implemented Interfaces:
java.util.Map<K,V>

public class MapWithDefault<K,V>
extends MapWrapper<K,V>

MapWithDefault is a map that returns default values for unassigned keys. Notice the difference with the usual implementations of Map, that return null for unassigned keys. A boolean flag in the constructor choose between two possible behaviours for the get method.

Version:
$Id: MapWithDefault.java,v 1.6 2006/03/14 02:29:31 salcianu Exp $
Author:
Alexandru Salcianu - salcianu@alum.mit.edu

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
 
Fields inherited from class jpaul.DataStructs.MapWrapper
map
 
Constructor Summary
MapWithDefault(Factory<V> defValFact, boolean withMemory)
          Creates a MapWithDefault, backed by a LinkedHashMap.
MapWithDefault(java.util.Map<K,V> map, Factory<V> defValFact, boolean withMemory)
          Creates a MapWithDefault.
 
Method Summary
 V get(java.lang.Object key)
          Returns the value to which this map maps the specified key.
 V getNoDefault(java.lang.Object key)
          Returns the value assigned to key, if any.
 
Methods inherited from class jpaul.DataStructs.MapWrapper
clear, containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapWithDefault

public MapWithDefault(Factory<V> defValFact,
                      boolean withMemory)
Creates a MapWithDefault, backed by a LinkedHashMap.

Parameters:
defValFact - Factory that provides the default values for previously unassigned keys.
withMemory - Selects one of the two behaviours described above.

MapWithDefault

public MapWithDefault(java.util.Map<K,V> map,
                      Factory<V> defValFact,
                      boolean withMemory)
Creates a MapWithDefault.

Parameters:
map - Underlying map.
defValFact - Factory that provides the default values for previously unassigned keys.
withMemory - Selects one of the two behaviours described above.
Method Detail

get

public V get(java.lang.Object key)
Returns the value to which this map maps the specified key. Unlike the common implementations of Map, if the key is not mapped to anything, a freshly-generated value will be returned instead. In addition, for maps with memory, the previously unassigned key will be mapped to the freshly-generated value (note that in this case, get mutates the map).

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class MapWrapper<K,V>

getNoDefault

public V getNoDefault(java.lang.Object key)
Returns the value assigned to key, if any. Otherwise, returns null (unlike get, no default element is generated, and the map is never mutated by this method.



Copyright 2005 Alexandru Salcianu - salcianu@alum.mit.edu