Class LazyLoadingMap<K,T>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,T>
-
- org.apache.sling.graphql.helpers.lazyloading.LazyLoadingMap<K,T>
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,T>
public class LazyLoadingMap<K,T> extends HashMap<K,T>
AHashMap
that optionally uses Suppliers to provide its values. Each Supplier is called at most once, if the corresponding value is requested. Some "global" operations requires all values to be computed, and should be considered costly. Like HashMap, this class is NOT thread safe. If needed,Collections.synchronizedMap(java.util.Map<K, V>)
can be used to synchronize it. Note thatremove(java.lang.Object)
behaves slightly differently than the HashMap version.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
LazyLoadingMap.Stats
Statistics on Supplier usage-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description LazyLoadingMap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
LazyLoadingMap<K,T>
computeAllThrowsException(boolean b)
Optionally throw a RuntimeException if computeAll is calledLazyLoadingMap<K,T>
computeValueOnRemove(boolean b)
Optionally compute the value on remove, so that it doesn't return nullboolean
containsKey(Object key)
boolean
containsValue(Object value)
Calls computeAll - should be avoided if possibleSet<Map.Entry<K,T>>
entrySet()
Calls computeAll - should be avoided if possibleboolean
equals(Object o)
Calls computeAll - should be avoided if possibleT
get(Object key)
LazyLoadingMap.Stats
getStats()
Return statistics on our suppliers, for metrics etc.int
hashCode()
boolean
isEmpty()
Set<K>
keySet()
Supplier<T>
put(K key, Supplier<T> supplier)
Adds a Supplier that provides a lazy loaded value.T
remove(Object key)
UnlesscomputeValueOnRemove(boolean)
is set to true, this returns null to avoid calling a supplier "for nothing".int
size()
Collection<T>
values()
Calls computeAll - should be avoided if possible-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, put, putAll, putIfAbsent, remove, replace, replace, replaceAll
-
Methods inherited from class java.util.AbstractMap
toString
-
-
-
-
Method Detail
-
equals
public boolean equals(Object o)
Calls computeAll - should be avoided if possible
-
hashCode
public int hashCode()
-
put
public Supplier<T> put(K key, Supplier<T> supplier)
Adds a Supplier that provides a lazy loaded value. Removes existing value with the same key if it exists.
-
remove
public T remove(Object key)
UnlesscomputeValueOnRemove(boolean)
is set to true, this returns null to avoid calling a supplier "for nothing".
-
clear
public void clear()
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K,T>
- Overrides:
containsKey
in classHashMap<K,T>
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
values
public Collection<T> values()
Calls computeAll - should be avoided if possible
-
containsValue
public boolean containsValue(Object value)
Calls computeAll - should be avoided if possible- Specified by:
containsValue
in interfaceMap<K,T>
- Overrides:
containsValue
in classHashMap<K,T>
-
getStats
public LazyLoadingMap.Stats getStats()
Return statistics on our suppliers, for metrics etc.
-
computeValueOnRemove
public LazyLoadingMap<K,T> computeValueOnRemove(boolean b)
Optionally compute the value on remove, so that it doesn't return null
-
computeAllThrowsException
public LazyLoadingMap<K,T> computeAllThrowsException(boolean b)
Optionally throw a RuntimeException if computeAll is called
-
-