Class LazyMap<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>, Get<K,​V>, IterableGet<K,​V>, IterableMap<K,​V>, Put<K,​V>
    Direct Known Subclasses:
    LazySortedMap

    public class LazyMap<K,​V>
    extends AbstractMapDecorator<K,​V>
    implements java.io.Serializable
    Decorates another Map to create objects in the map on demand.

    When the get(Object) method is called with a key that does not exist in the map, the factory is used to create the object. The created object will be added to the map using the requested key.

    For instance:

     Factory<Date> factory = new Factory<Date>() {
         public Date create() {
             return new Date();
         }
     }
     Map<String, Date> lazy = LazyMap.lazyMap(new HashMap<String, Date>(), factory);
     Date date = lazy.get("NOW");
     
    After the above code is executed, date will refer to a new Date instance. Furthermore, that Date instance is mapped to the "NOW" key in the map.

    Note that LazyMap 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.

    This class is Serializable from Commons Collections 3.1.

    Since:
    3.0
    Version:
    $Id: LazyMap.java 1686855 2015-06-22 13:00:27Z tn $
    See Also:
    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
      protected Transformer<? super K,​? extends V> factory
      The factory to use to construct elements
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected LazyMap​(java.util.Map<K,​V> map, Factory<? extends V> factory)
      Constructor that wraps (not copies).
      protected LazyMap​(java.util.Map<K,​V> map, Transformer<? super K,​? extends V> factory)
      Constructor that wraps (not copies).
    • Field Detail

      • factory

        protected final Transformer<? super K,​? extends V> factory
        The factory to use to construct elements
    • Constructor Detail

      • LazyMap

        protected LazyMap​(java.util.Map<K,​V> map,
                          Factory<? extends V> factory)
        Constructor that wraps (not copies).
        Parameters:
        map - the map to decorate, must not be null
        factory - the factory to use, must not be null
        Throws:
        java.lang.NullPointerException - if map or factory is null
      • LazyMap

        protected LazyMap​(java.util.Map<K,​V> map,
                          Transformer<? super K,​? extends V> factory)
        Constructor that wraps (not copies).
        Parameters:
        map - the map to decorate, must not be null
        factory - the factory to use, must not be null
        Throws:
        java.lang.NullPointerException - if map or factory is null
    • Method Detail

      • lazyMap

        public static <K,​V> LazyMap<K,​V> lazyMap​(java.util.Map<K,​V> map,
                                                             Factory<? extends V> factory)
        Factory method to create a lazily instantiated map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - the map to decorate, must not be null
        factory - the factory to use, must not be null
        Returns:
        a new lazy map
        Throws:
        java.lang.NullPointerException - if map or factory is null
        Since:
        4.0
      • lazyMap

        public static <V,​K> LazyMap<K,​V> lazyMap​(java.util.Map<K,​V> map,
                                                             Transformer<? super K,​? extends V> factory)
        Factory method to create a lazily instantiated map.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - the map to decorate, must not be null
        factory - the factory to use, must not be null
        Returns:
        a new lazy map
        Throws:
        java.lang.NullPointerException - if map or factory is null
        Since:
        4.0
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface Get<K,​V>
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class AbstractMapDecorator<K,​V>
        See Also:
        Map.get(Object)