java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
bayern.steinbrecher.javaUtility.SupplyingMap<K,V>
- Type Parameters:
K- The type of the key values.V- The type of values.
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<K,V>
public class SupplyingMap<K,V>
extends java.util.HashMap<K,V>
Represents a
HashMap which creates an empty entry fro a key whenever it is accessed over
HashMap.get(java.lang.Object).- Since:
- 0.1
- See Also:
- Serialized Form
-
Nested Class Summary
-
Constructor Summary
Constructors Constructor Description SupplyingMap(java.util.function.Function<K,V> entrySupplier)Creates aSupplyingMapwhich generates empty entries when accessing them using the passedFunction. -
Method Summary
Modifier and Type Method Description Vget(java.lang.Object key)Returns the value associated with the given key or generates an empty entry, associates it with the key and returns it.java.util.function.Function<K,V>getEntrySupplier()Returns theFunctionused for generating new empty entries.Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Constructor Details
-
SupplyingMap
Creates aSupplyingMapwhich generates empty entries when accessing them using the passedFunction.- Parameters:
entrySupplier- The supplier for generating new empty entries. Its input is the key to generate an empty entry for.
-
-
Method Details
-
get
Returns the value associated with the given key or generates an empty entry, associates it with the key and returns it. This method returnsnullif and only if theFunctionfor generating empty entries returnsnull.- Specified by:
getin interfacejava.util.Map<K,V>- Overrides:
getin classjava.util.HashMap<K,V>- Parameters:
key- The key to get an associated value for.- Returns:
- The value assocated with the given key.
- Throws:
java.lang.ClassCastException- Thrown only if no value is associated with the key and the key is not of typeSupplyingMap.- See Also:
getEntrySupplier(),HashMap.get(java.lang.Object),HashMap.containsKey(java.lang.Object)
-
getEntrySupplier
Returns theFunctionused for generating new empty entries.- Returns:
- The
Functionused for generating new empty entries. Its input is the key to generate an empty entry for.I
-