Package co.aikar.util

# Class LoadingMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
co.aikar.util.LoadingMap<K,V>

Type Parameters:
  `K` - Key
  `V` - Value

All Implemented Interfaces:
  `Map<K,V>`

public class LoadingMap<K,V> extends AbstractMap<K,V>
Allows you to pass a Loader function that when a key is accessed that doesn't exists, automatically loads the entry into the map by calling the loader Function. .get() Will only return null if the Loader can return null. You may pass any backing Map to use. This class is not thread safe and should be wrapped with Collections.synchronizedMap on the OUTSIDE of the LoadingMap if needed. Do not wrap the backing map with Collections.synchronizedMap.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static class`
`LoadingMap.Feeder<T>`
Due to java stuff, you will need to cast it to (Function) for some cases

## 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
`LoadingMap(@NotNull Map<K,V> backingMap, Function<K,V> loader)`
Initializes an auto loading map using specified loader and backing map

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
`@NotNull LoadingMap<K,V>`
`clone()`
`boolean`
`containsKey(@Nullable Object key)`
`boolean`
`containsValue(@Nullable Object value)`
`@NotNull Set<Map.Entry<K,V>>`
`entrySet()`
`boolean`
`equals(@Nullable Object o)`
`V`
`get(@Nullable Object key)`
`int`
`hashCode()`
`boolean`
`isEmpty()`
`@NotNull Set<K>`
`keySet()`
`static <K, V> @NotNull Map<K,V>`
`newAutoMap(@NotNull Map<K,V> backingMap, @NotNull Class<? extends V> valueClass)`
Creates a LoadingMap with an auto instantiating loader.
`static <K, V> @NotNull Map<K,V>`
`newAutoMap(@NotNull Map<K,V> backingMap, @Nullable Class<? extends K> keyClass, @NotNull Class<? extends V> valueClass)`
Creates a LoadingMap with an auto instantiating loader.
`static <K, V> @NotNull Map<K,V>`
`newHashAutoMap(@NotNull Class<? extends V> valueClass)`
`static <K, V> @NotNull Map<K,V>`
`newHashAutoMap(@NotNull Class<? extends V> valueClass, int initialCapacity, float loadFactor)`
`static <K, V> @NotNull Map<K,V>`
`newHashAutoMap(@Nullable Class<? extends K> keyClass, @NotNull Class<? extends V> valueClass)`
`static <K, V> @NotNull Map<K,V>`
`newHashAutoMap(@Nullable Class<? extends K> keyClass, @NotNull Class<? extends V> valueClass, int initialCapacity, float loadFactor)`
`static <K, V> @NotNull Map<K,V>`
`newHashMap(@NotNull Function<K,V> loader)`
Initializes an auto loading map using a HashMap
`static <K, V> @NotNull Map<K,V>`
`newHashMap(@NotNull Function<K,V> loader, int initialCapacity)`
Initializes an auto loading map using a HashMap
`static <K, V> @NotNull Map<K,V>`
`newHashMap(@NotNull Function<K,V> loader, int initialCapacity, float loadFactor)`
Initializes an auto loading map using a HashMap
`static <K, V> @NotNull Map<K,V>`
`newIdentityHashMap(@NotNull Function<K,V> loader)`
Initializes an auto loading map using an Identity HashMap
`static <K, V> @NotNull Map<K,V>`
`newIdentityHashMap(@NotNull Function<K,V> loader, int initialCapacity)`
Initializes an auto loading map using an Identity HashMap
`static <K, V> @NotNull Map<K,V>`
`of(@NotNull Map<K,V> backingMap, @NotNull Function<K,V> loader)`
Creates a new LoadingMap with the specified map and loader
`V`
`put(K key, V value)`
`void`
`putAll(@NotNull Map<? extends K,? extends V> m)`
`V`
`remove(@Nullable Object key)`
`int`
`size()`
`@NotNull Collection<V>`
`values()`

### Methods inherited from class java.util.AbstractMap

`toString`

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

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

### Methods inherited from interface java.util.Map

`compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll`

## Constructor Details

### LoadingMap

public LoadingMap(@NotNull @NotNull Map<K,V> backingMap, @NotNull Function<K,V> loader)
Initializes an auto loading map using specified loader and backing map

Parameters:
  `backingMap` - Map to wrap
  `loader` - Loader

## Method Details

### of

@NotNull public static <K, V> @NotNull Map<K,V> of(@NotNull @NotNull Map<K,V> backingMap, @NotNull @NotNull Function<K,V> loader)
Creates a new LoadingMap with the specified map and loader

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `backingMap` - Actual map being used.
  `loader` - Loader to use
Returns:
  Map

### newAutoMap

@NotNull public static <K, V> @NotNull Map<K,V> newAutoMap(@NotNull @NotNull Map<K,V> backingMap, @Nullable @Nullable Class<? extends K> keyClass, @NotNull @NotNull Class<? extends V> valueClass)
Creates a LoadingMap with an auto instantiating loader. Will auto construct class of of Value when not found Since this uses Reflection, It is more effecient to define your own static loader than using this helper, but if performance is not critical, this is easier.

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `backingMap` - Actual map being used.
  `keyClass` - Class used for the K generic
  `valueClass` - Class used for the V generic
Returns:
  Map that auto instantiates on .get()

### newAutoMap

@NotNull public static <K, V> @NotNull Map<K,V> newAutoMap(@NotNull @NotNull Map<K,V> backingMap, @NotNull @NotNull Class<? extends V> valueClass)
Creates a LoadingMap with an auto instantiating loader. Will auto construct class of of Value when not found Since this uses Reflection, It is more effecient to define your own static loader than using this helper, but if performance is not critical, this is easier.

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `backingMap` - Actual map being used.
  `valueClass` - Class used for the V generic
Returns:
  Map that auto instantiates on .get()

### newHashAutoMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashAutoMap(@Nullable @Nullable Class<? extends K> keyClass, @NotNull @NotNull Class<? extends V> valueClass)

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `keyClass` - Class used for the K generic
  `valueClass` - Class used for the V generic
Returns:
  Map that auto instantiates on .get()
See Also:
  - `new Auto initializing map using a HashMap.`

### newHashAutoMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashAutoMap(@NotNull @NotNull Class<? extends V> valueClass)

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `valueClass` - Class used for the V generic
Returns:
  Map that auto instantiates on .get()
See Also:
  - `new Auto initializing map using a HashMap.`

### newHashAutoMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashAutoMap(@Nullable @Nullable Class<? extends K> keyClass, @NotNull @NotNull Class<? extends V> valueClass, int initialCapacity, float loadFactor)

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `keyClass` - Class used for the K generic
  `valueClass` - Class used for the V generic
  `initialCapacity` - Initial capacity to use
  `loadFactor` - Load factor to use
Returns:
  Map that auto instantiates on .get()
See Also:
  - `new Auto initializing map using a HashMap.`

### newHashAutoMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashAutoMap(@NotNull @NotNull Class<? extends V> valueClass, int initialCapacity, float loadFactor)

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `valueClass` - Class used for the V generic
  `initialCapacity` - Initial capacity to use
  `loadFactor` - Load factor to use
Returns:
  Map that auto instantiates on .get()
See Also:
  - `new Auto initializing map using a HashMap.`

### newHashMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashMap(@NotNull @NotNull Function<K,V> loader)
Initializes an auto loading map using a HashMap

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `loader` - Loader to use
Returns:
  Map

### newHashMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashMap(@NotNull @NotNull Function<K,V> loader, int initialCapacity)
Initializes an auto loading map using a HashMap

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `loader` - Loader to use
  `initialCapacity` - Initial capacity to use
Returns:
  Map

### newHashMap

@NotNull public static <K, V> @NotNull Map<K,V> newHashMap(@NotNull @NotNull Function<K,V> loader, int initialCapacity, float loadFactor)
Initializes an auto loading map using a HashMap

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `loader` - Loader to use
  `initialCapacity` - Initial capacity to use
  `loadFactor` - Load factor to use
Returns:
  Map

### newIdentityHashMap

@NotNull public static <K, V> @NotNull Map<K,V> newIdentityHashMap(@NotNull @NotNull Function<K,V> loader)
Initializes an auto loading map using an Identity HashMap

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `loader` - Loader to use
Returns:
  Map

### newIdentityHashMap

@NotNull public static <K, V> @NotNull Map<K,V> newIdentityHashMap(@NotNull @NotNull Function<K,V> loader, int initialCapacity)
Initializes an auto loading map using an Identity HashMap

Type Parameters:
  `K` - Key Type of the Map
  `V` - Value Type of the Map
Parameters:
  `loader` - Loader to use
  `initialCapacity` - Initial capacity to use
Returns:
  Map

### size

public int size()

Specified by:
  `size` in interface `Map<K,V>`
Overrides:
  `size` in class `AbstractMap<K,V>`

### isEmpty

public boolean isEmpty()

Specified by:
  `isEmpty` in interface `Map<K,V>`
Overrides:
  `isEmpty` in class `AbstractMap<K,V>`

### containsKey

public boolean containsKey(@Nullable @Nullable Object key)

Specified by:
  `containsKey` in interface `Map<K,V>`
Overrides:
  `containsKey` in class `AbstractMap<K,V>`

### containsValue

public boolean containsValue(@Nullable @Nullable Object value)

Specified by:
  `containsValue` in interface `Map<K,V>`
Overrides:
  `containsValue` in class `AbstractMap<K,V>`

### get

@Nullable public V get(@Nullable @Nullable Object key)

Specified by:
  `get` in interface `Map<K,V>`
Overrides:
  `get` in class `AbstractMap<K,V>`

### put

@Nullable public V put(@Nullable K key, @Nullable V value)

Specified by:
  `put` in interface `Map<K,V>`
Overrides:
  `put` in class `AbstractMap<K,V>`

### remove

@Nullable public V remove(@Nullable @Nullable Object key)

Specified by:
  `remove` in interface `Map<K,V>`
Overrides:
  `remove` in class `AbstractMap<K,V>`

### putAll

public void putAll(@NotNull @NotNull Map<? extends K,? extends V> m)

Specified by:
  `putAll` in interface `Map<K,V>`
Overrides:
  `putAll` in class `AbstractMap<K,V>`

### clear

public void clear()

Specified by:
  `clear` in interface `Map<K,V>`
Overrides:
  `clear` in class `AbstractMap<K,V>`

### keySet

@NotNull public @NotNull Set<K> keySet()

Specified by:
  `keySet` in interface `Map<K,V>`
Overrides:
  `keySet` in class `AbstractMap<K,V>`

### values

@NotNull public @NotNull Collection<V> values()

Specified by:
  `values` in interface `Map<K,V>`
Overrides:
  `values` in class `AbstractMap<K,V>`

### equals

public boolean equals(@Nullable @Nullable Object o)

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

### hashCode

public int hashCode()

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

### entrySet

@NotNull public @NotNull Set<Map.Entry<K,V>> entrySet()

Specified by:
  `entrySet` in interface `Map<K,V>`
Specified by:
  `entrySet` in class `AbstractMap<K,V>`

### clone

@NotNull public @NotNull LoadingMap<K,V> clone()

Overrides:
  `clone` in class `AbstractMap<K,V>`