Package org.linkki.util
Class LazyInitializingMap<K,V>
- java.lang.Object
-
- org.linkki.util.LazyInitializingMap<K,V>
-
-
Constructor Summary
Constructors Constructor Description LazyInitializingMap(Function<K,V> initializer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears this map.Vget(K key)Returns the value the given key maps to.VgetIfPresent(K key)Returns the value the given key maps to.Vremove(K key)Removes the key from the map.
-
-
-
Constructor Detail
-
LazyInitializingMap
public LazyInitializingMap(Function<K,V> initializer)
- Parameters:
initializer- the initializer function for lazily creating values. This function throws aNullPointerExceptionwhen givennull. Returns non-nullvalues otherwise.
-
-
Method Detail
-
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 returnsnull.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 isnull, or if the initializer function violates its contract and returnsnull.
-
getIfPresent
@CheckForNull public V getIfPresent(K key)
Returns the value the given key maps to. Returnsnullif 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 ornullif the key does not map to a value.- See Also:
Map.remove(Object)
-
-