org.apache.commons.collections.map
Class DefaultedMap
- Map, Serializable
public class DefaultedMap
implements Map, Serializable
Decorates another
Map returning a default value if the map
does not contain the requested key.
When the
get(Object) method is called with a key that does not
exist in the map, this map will return the default value specified in
the constructor/factory. Only the get method is altered, so the
Map.containsKey(Object) can be used to determine if a key really
is in the map or not.
The defaulted value is not added to the map.
Compare this behaviour with
LazyMap, which does add the value
to the map (via a Transformer).
For instance:
Map map = new DefaultedMap("NULL");
Object obj = map.get("Surname");
// obj == "NULL"
After the above code is executed the map is still empty.
Note that DefaultedMap is not synchronized and is not thread-safe.
If you wish to use this map from multiple threads concurrently, you must use
appropriate synchronization. The simplest approach is to wrap this map
using
java.util.Collections.synchronizedMap(Map). This class may throw
exceptions when accessed by concurrent threads without synchronization.
$Revision: 1.7 $ $Date: 2005-11-21 22:52:57 +0000 (Mon, 21 Nov 2005) $- Stephen Colebourne
- Rafael U.C. Afonso
protected Object | value- The transformer to use if the map does not contain a key
|
DefaultedMap(Map map, Object value)- Constructor that wraps (not copies).
|
DefaultedMap(Object defaultValue)- Constructs a new empty
DefaultedMap that decorates
a HashMap.
|
static Map | decorate(Map map, Object defaultValue)- Factory method to create a defaulting map.
|
static Map | decorate(Map map, Factory factory)- Factory method to create a defaulting map.
|
static Map | decorate(Map map, Transformer factory)- Factory method to create a defaulting map.
|
Object | get(Object key)
|
clear, containsKey, containsValue, entrySet, equals, get, getMap, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values |
value
protected final Object value
The transformer to use if the map does not contain a key
DefaultedMap
protected DefaultedMap(Map map,
Object value) Constructor that wraps (not copies).
map - the map to decorate, must not be nullvalue - the value to use
DefaultedMap
public DefaultedMap(Object defaultValue)
Constructs a new empty
DefaultedMap that decorates
a
HashMap.
The object passed in will be returned by the map whenever an
unknown key is requested.
defaultValue - the default value to return when the key is not found
decorate
public static Map decorate(Map map,
Object defaultValue) Factory method to create a defaulting map.
The value specified is returned when a missing key is found.
map - the map to decorate, must not be nulldefaultValue - the default value to return when the key is not found
decorate
public static Map decorate(Map map,
Factory factory) Factory method to create a defaulting map.
The factory specified is called when a missing key is found.
The result will be returned as the result of the map get(key) method.
map - the map to decorate, must not be nullfactory - the factory to use, must not be null
decorate
public static Map decorate(Map map,
Transformer factory) Factory method to create a defaulting map.
The transformer specified is called when a missing key is found.
The key is passed to the transformer as the input, and the result
will be returned as the result of the map get(key) method.
map - the map to decorate, must not be nullfactory - the factory to use, must not be null
Copyright © 2001-2008 Apache Software Foundation. All Rights Reserved.