edu.rice.cs.plt.collect
Class TotalMap<K,V>

java.lang.Object
  extended by edu.rice.cs.plt.collect.TotalMap<K,V>

public class TotalMap<K,V>
extends java.lang.Object

A map that is defined for all values in the domain K. This class is similar to the Map interface, but there are fundamental differences that make it difficult to implement the interface. For example, it's impossible to produce a valueSet for an arbitrary TotalMap. (It's even impossible to safely define the Map.get method, since it is defined for all Objects, and the lambda implementing the total map is only defined in terms of K.)

The total coverage of the domain is achieved by defining the map in terms of a Lambda. Additionally, specific values can be overridden with specific results via the override(K, V) method; these mappings can be undone via revert(K). Optionally, the results of get(K) queries that call the lambda can be automatically cached with the other overriding mappings.

This class is not thread safe (to reduce overhead in sequenential applications, and because external references to _overrides make it difficult to make safety guarantees). It is possible, for example, for a TotalMap with caching enabled to still compute a result more than once.


Constructor Summary
TotalMap()
          Create a TotalMap whose value is null for all inputs.
TotalMap(Lambda<? super K,? extends V> lambda)
          Create a TotalMap defined in terms of lambda.
TotalMap(Lambda<? super K,? extends V> lambda, boolean cache)
          Create a TotalMap defined in terms of lambda.
TotalMap(Lambda<? super K,? extends V> lambda, java.util.Map<? super K,V> overrides)
          Create a TotalMap defined in terms of lambda, with the exception of the given overriding assignments.
TotalMap(Lambda<? super K,? extends V> lambda, java.util.Map<? super K,V> overrides, boolean cache)
          Create a TotalMap defined in terms of lambda, with the exception of the given overriding assignments.
TotalMap(java.util.Map<? super K,V> overrides)
          Create a TotalMap whose value is null for all inputs, with the exception of the given overriding assignments.
 
Method Summary
 int cacheSize()
          Get the size of the cache -- the number of overridden values
 boolean containsOverride(java.lang.Object key)
           
 V get(K key)
           
static
<K,V> TotalMap<K,V>
make()
          Call the constructor (allows the type arguments to be inferred)
static
<K,V> TotalMap<K,V>
make(Lambda<? super K,? extends V> lambda)
          Call the constructor (allows the type arguments to be inferred)
static
<K,V> TotalMap<K,V>
make(Lambda<? super K,? extends V> lambda, boolean cache)
          Call the constructor (allows the type arguments to be inferred)
static
<K,V> TotalMap<K,V>
make(Lambda<? super K,? extends V> lambda, java.util.Map<? super K,V> overrides)
          Call the constructor (allows the type arguments to be inferred)
static
<K,V> TotalMap<K,V>
make(Lambda<? super K,? extends V> lambda, java.util.Map<? super K,V> overrides, boolean cache)
          Call the constructor (allows the type arguments to be inferred)
static
<K,V> TotalMap<K,V>
make(java.util.Map<? super K,V> overrides)
          Call the constructor (allows the type arguments to be inferred)
 void override(K key, V value)
          Add the given mapping to the override map.
 void overrideAll(java.util.Map<? extends K,? extends V> map)
          Add all the given mappings to the override map
 V revert(K key)
          Remove the given key from the override map.
 void revertAll()
          Remove all mappings from the override map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TotalMap

public TotalMap()
Create a TotalMap whose value is null for all inputs. Caching is disabled.


TotalMap

public TotalMap(Lambda<? super K,? extends V> lambda)
Create a TotalMap defined in terms of lambda. Caching is disabled.


TotalMap

public TotalMap(Lambda<? super K,? extends V> lambda,
                boolean cache)
Create a TotalMap defined in terms of lambda. Caching may be enabled or disabled.


TotalMap

public TotalMap(java.util.Map<? super K,V> overrides)
Create a TotalMap whose value is null for all inputs, with the exception of the given overriding assignments. The given map is stored (not copied), so subsequent external changes will be reflected here (and vice versa). Cahcing is disabled.


TotalMap

public TotalMap(Lambda<? super K,? extends V> lambda,
                java.util.Map<? super K,V> overrides)
Create a TotalMap defined in terms of lambda, with the exception of the given overriding assignments. The given map is stored (not copied), so subsequent external changes will be reflected here (and vice versa). Caching is disabled.


TotalMap

public TotalMap(Lambda<? super K,? extends V> lambda,
                java.util.Map<? super K,V> overrides,
                boolean cache)
Create a TotalMap defined in terms of lambda, with the exception of the given overriding assignments. The given map is stored (not copied), so subsequent external changes will be reflected here (and vice versa). Caching is enabled or disabled as specified.

Method Detail

get

public V get(K key)
Returns:
The value corresponding to key

override

public void override(K key,
                     V value)
Add the given mapping to the override map. Subsequent invocations of get(key) will immediately return value.


containsOverride

public boolean containsOverride(java.lang.Object key)
Returns:
true iff the override map contains the given key

revert

public V revert(K key)
Remove the given key from the override map. Subsequent invocations of get(key) will invoke the lambda.

Returns:
The value overridden by the given key, or null if the mapping did not exist.

overrideAll

public void overrideAll(java.util.Map<? extends K,? extends V> map)
Add all the given mappings to the override map


revertAll

public void revertAll()
Remove all mappings from the override map. Subsequent invocations of get will invoke the lambda.


cacheSize

public int cacheSize()
Get the size of the cache -- the number of overridden values


make

public static <K,V> TotalMap<K,V> make()
Call the constructor (allows the type arguments to be inferred)


make

public static <K,V> TotalMap<K,V> make(Lambda<? super K,? extends V> lambda)
Call the constructor (allows the type arguments to be inferred)


make

public static <K,V> TotalMap<K,V> make(Lambda<? super K,? extends V> lambda,
                                       boolean cache)
Call the constructor (allows the type arguments to be inferred)


make

public static <K,V> TotalMap<K,V> make(java.util.Map<? super K,V> overrides)
Call the constructor (allows the type arguments to be inferred)


make

public static <K,V> TotalMap<K,V> make(Lambda<? super K,? extends V> lambda,
                                       java.util.Map<? super K,V> overrides)
Call the constructor (allows the type arguments to be inferred)


make

public static <K,V> TotalMap<K,V> make(Lambda<? super K,? extends V> lambda,
                                       java.util.Map<? super K,V> overrides,
                                       boolean cache)
Call the constructor (allows the type arguments to be inferred)