Class DefaultedMap

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map

    public class DefaultedMap
    extends AbstractMapDecorator
    implements java.util.Map, java.io.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 Collections.synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

    Since:
    Commons Collections 3.2
    Version:
    $Revision: 1.7 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
    See Also:
    LazyMap, Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long serialVersionUID
      Serialization version
      protected java.lang.Object value
      The transformer to use if the map does not contain a key
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        DefaultedMap​(java.lang.Object defaultValue)
      Constructs a new empty DefaultedMap that decorates a HashMap.
      protected DefaultedMap​(java.util.Map map, java.lang.Object value)
      Constructor that wraps (not copies).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map decorate​(java.util.Map map, java.lang.Object defaultValue)
      Factory method to create a defaulting map.
      static java.util.Map decorate​(java.util.Map map, Factory factory)
      Factory method to create a defaulting map.
      static java.util.Map decorate​(java.util.Map map, Transformer factory)
      Factory method to create a defaulting map.
      java.lang.Object get​(java.lang.Object key)  
      private void readObject​(java.io.ObjectInputStream in)
      Read the map in using a custom routine.
      private void writeObject​(java.io.ObjectOutputStream out)
      Write the map out using a custom routine.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        Serialization version
        See Also:
        Constant Field Values
      • value

        protected final java.lang.Object value
        The transformer to use if the map does not contain a key
    • Constructor Detail

      • DefaultedMap

        public DefaultedMap​(java.lang.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.

        Parameters:
        defaultValue - the default value to return when the key is not found
      • DefaultedMap

        protected DefaultedMap​(java.util.Map map,
                               java.lang.Object value)
        Constructor that wraps (not copies).
        Parameters:
        map - the map to decorate, must not be null
        value - the value to use
        Throws:
        java.lang.IllegalArgumentException - if map or transformer is null
    • Method Detail

      • decorate

        public static java.util.Map decorate​(java.util.Map map,
                                             java.lang.Object defaultValue)
        Factory method to create a defaulting map.

        The value specified is returned when a missing key is found.

        Parameters:
        map - the map to decorate, must not be null
        defaultValue - the default value to return when the key is not found
        Throws:
        java.lang.IllegalArgumentException - if map is null
      • decorate

        public static java.util.Map decorate​(java.util.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.

        Parameters:
        map - the map to decorate, must not be null
        factory - the factory to use, must not be null
        Throws:
        java.lang.IllegalArgumentException - if map or factory is null
      • decorate

        public static java.util.Map decorate​(java.util.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.

        Parameters:
        map - the map to decorate, must not be null
        factory - the factory to use, must not be null
        Throws:
        java.lang.IllegalArgumentException - if map or factory is null
      • writeObject

        private void writeObject​(java.io.ObjectOutputStream out)
                          throws java.io.IOException
        Write the map out using a custom routine.
        Parameters:
        out - the output stream
        Throws:
        java.io.IOException
      • readObject

        private void readObject​(java.io.ObjectInputStream in)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Read the map in using a custom routine.
        Parameters:
        in - the input stream
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • get

        public java.lang.Object get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map
        Overrides:
        get in class AbstractMapDecorator