Class LazyInitializingMap<K,V>

java.lang.Object
org.linkki.util.LazyInitializingMap<K,V>
Type Parameters:
K - the type of key stored in this map
V - the type of values stored in this map

public class LazyInitializingMap<K,V> extends Object
Key-value store that initializes its values lazily. Values will be initialized using the initializer function the map is created with.
  • Constructor Details

    • LazyInitializingMap

      public LazyInitializingMap(Function<K,V> initializer)
      Parameters:
      initializer - the initializer function for lazily creating values. This function throws a NullPointerException when given null. Returns non- null values otherwise.
  • Method Details

    • get

      public V get(K key)
      Returns the value the given key maps to. Creates a new value if none is present using this map's initializer. Never returns null.

      Use as replacement for the usual "get or initialize and get" code:

       if (!contains(key)) {
           initValue(key);
       }
       return get(key);
       
      Throws:
      NullPointerException - if either the key is null, or if the initializer function violates its contract and returns null.
    • getIfPresent

      @CheckForNull public V getIfPresent(K key)
      Returns the value the given key maps to. Returns null if the key does not map to a value.
    • clear

      public void clear()
      Clears this map.
    • remove

      @CheckForNull public V remove(K key)
      Removes the key from the map. Returns the removed value or null if the key does not map to a value.
      See Also: