Package org.apache.sling.graphql.helpers.lazyloading

# 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>
A `HashMap` 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 that `remove(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>`

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

`Map.Entry<K extends Object,V extends Object>`

## Constructor Summary

Constructors
Constructor
Description
`LazyLoadingMap()`

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
`LazyLoadingMap<K,T>`
`computeAllThrowsException(boolean b)`
Optionally throw a RuntimeException if computeAll is called
`LazyLoadingMap<K,T>`
`computeValueOnRemove(boolean b)`
Optionally compute the value on remove, so that it doesn't return null
`boolean`
`containsKey(Object key)`
`boolean`
`containsValue(Object value)`
Calls computeAll - should be avoided if possible
`Set<Map.Entry<K,T>>`
`entrySet()`
Calls computeAll - should be avoided if possible
`boolean`
`equals(Object o)`
Calls computeAll - should be avoided if possible
`T`
`get(Object key)`
`LazyLoadingMap<K,T>.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)`
Unless `computeValueOnRemove(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`

### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### LazyLoadingMap

public LazyLoadingMap()

## Method Details

### equals

public boolean equals(Object o)
Calls computeAll - should be avoided if possible

Specified by:
  `equals` in interface `Map<K,T>`
Overrides:
  `equals` in class `AbstractMap<K,T>`

### hashCode

public int hashCode()

Specified by:
  `hashCode` in interface `Map<K,T>`
Overrides:
  `hashCode` in class `AbstractMap<K,T>`

### 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.

### get

public T get(Object key)

Specified by:
  `get` in interface `Map<K,T>`
Overrides:
  `get` in class `HashMap<K,T>`

### remove

public T remove(Object key)
Unless `computeValueOnRemove(boolean)` is set to true, this returns null to avoid calling a supplier "for nothing".

Specified by:
  `remove` in interface `Map<K,T>`
Overrides:
  `remove` in class `HashMap<K,T>`

### clear

public void clear()

Specified by:
  `clear` in interface `Map<K,T>`
Overrides:
  `clear` in class `HashMap<K,T>`

### containsKey

public boolean containsKey(Object key)

Specified by:
  `containsKey` in interface `Map<K,T>`
Overrides:
  `containsKey` in class `HashMap<K,T>`

### size

public int size()

Specified by:
  `size` in interface `Map<K,T>`
Overrides:
  `size` in class `HashMap<K,T>`

### isEmpty

public boolean isEmpty()

Specified by:
  `isEmpty` in interface `Map<K,T>`
Overrides:
  `isEmpty` in class `HashMap<K,T>`

### keySet

public Set<K> keySet()

Specified by:
  `keySet` in interface `Map<K,T>`
Overrides:
  `keySet` in class `HashMap<K,T>`

### values

public Collection<T> values()
Calls computeAll - should be avoided if possible

Specified by:
  `values` in interface `Map<K,T>`
Overrides:
  `values` in class `HashMap<K,T>`

### entrySet

public Set<Map.Entry<K,T>> entrySet()
Calls computeAll - should be avoided if possible

Specified by:
  `entrySet` in interface `Map<K,T>`
Overrides:
  `entrySet` in class `HashMap<K,T>`

### containsValue

public boolean containsValue(Object value)
Calls computeAll - should be avoided if possible

Specified by:
  `containsValue` in interface `Map<K,T>`
Overrides:
  `containsValue` in class `HashMap<K,T>`

### getStats

public LazyLoadingMap<K,T>.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