com.threerings.util
Class DefaultMap<K,V>

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.collect.ForwardingMap<K,V>
          extended by com.threerings.util.DefaultMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class DefaultMap<K,V>
extends ForwardingMap<K,V>

Provides a map implementation that automatically creates, and inserts into the map, a default value for any value retrieved via the fetch(K) method which has no entry for that key.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Method Summary
 V fetch(K key)
          Looks up the supplied key in the map, returning the value to which it is mapped.
static
<K,V> DefaultMap<K,V>
newHashMap(Function<K,V> creator)
          Creates a default map backed by a HashMap using the supplied default creator.
static
<K,V> Function<K,V>
newInstanceCreator(Class<V> clazz)
          Returns a Function that makes instances of the supplied class (using its no-args constructor) as default values.
static
<K,V> DefaultMap<K,V>
newInstanceHashMap(Class<V> clazz)
          Creates a default map backed by a HashMap that creates instances of the supplied class (using its no-args constructor) as defaults.
static
<K,V> DefaultMap<K,V>
newInstanceMap(Map<K,V> delegate, Class<V> clazz)
          Creates a default map backed by the supplied map that creates instances of the supplied class (using its no-args constructor) as defaults.
static
<K,V> DefaultMap<K,V>
newMap(Map<K,V> delegate, Function<K,V> creator)
          Creates a default map backed by the supplied map using the supplied default creator.
 
Methods inherited from class com.google.common.collect.ForwardingMap
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class com.google.common.collect.ForwardingObject
toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

newInstanceHashMap

public static <K,V> DefaultMap<K,V> newInstanceHashMap(Class<V> clazz)
Creates a default map backed by a HashMap that creates instances of the supplied class (using its no-args constructor) as defaults.


newInstanceMap

public static <K,V> DefaultMap<K,V> newInstanceMap(Map<K,V> delegate,
                                                   Class<V> clazz)
Creates a default map backed by the supplied map that creates instances of the supplied class (using its no-args constructor) as defaults.


newInstanceCreator

public static <K,V> Function<K,V> newInstanceCreator(Class<V> clazz)
Returns a Function that makes instances of the supplied class (using its no-args constructor) as default values.


newHashMap

public static <K,V> DefaultMap<K,V> newHashMap(Function<K,V> creator)
Creates a default map backed by a HashMap using the supplied default creator.


newMap

public static <K,V> DefaultMap<K,V> newMap(Map<K,V> delegate,
                                           Function<K,V> creator)
Creates a default map backed by the supplied map using the supplied default creator.


fetch

public V fetch(K key)
Looks up the supplied key in the map, returning the value to which it is mapped. If the key has no mapping, a default value will be obtained for the requested key and placed into the map. The current and subsequent lookups will return that value.



Copyright © 2012. All Rights Reserved.