Package org.djutils.multikeymap
Class MultiKeyMap<T>
java.lang.Object
org.djutils.multikeymap.MultiKeyMap<T>
- Type Parameters:
T
- value type
public class MultiKeyMap<T> extends Object
Store for data indexed by a combination of objects. An important difference with a normal (Linked-)HashMap is that the
getValue
method has a Supplier
argument that will be invoked when no value was stored under the
provided combination of keys. The value yielded by the Supplier
is then stored under that key combination. If
this behavior is not wanted, the user can provide the null value for the Supplier
argument. The map can store
null values, but on retrieval these are indistinguishable from values that were never set. No key values may be null.
Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Version:
- $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2018
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Constructor Summary
Constructors Constructor Description MultiKeyMap(Class<?>... keyTypes)
Construct a new MultiKeyMap. -
Method Summary
Modifier and Type Method Description Object
clear(Object... keys)
Clears the mapping for a key combination.T
get(Object... keys)
Retrieve a value from this MultiKeyMap.T
get(Supplier<T> supplier, Object... keys)
Retrieve a value from this MultiKeyMap.Set<Object>
getKeys(Object... keys)
Return set of key objects on this level.MultiKeyMap<T>
getSubMap(Object... keys)
Retrieve a sub map.T
put(T newValue, Object... keys)
Put (add or replace) a value in this MultiKeyMap.String
toString()
-
Constructor Details
-
MultiKeyMap
Construct a new MultiKeyMap.- Parameters:
keyTypes
- Class<?>...; the types of the keys
-
-
Method Details
-
get
Retrieve a value from this MultiKeyMap. Can create a new entry if it does not exist yet.- Parameters:
supplier
- Supplier<T>; supplier ofT
in case the leaf does not exist yet. Set this tonull
to suppress creation of new branches and a new leafkeys
- Object...; list of key objects- Returns:
- T; the existing value, or the value that was obtained through the
supplier
. Returnsnull
if the leaf does not exist andsupplier
isnull
-
get
Retrieve a value from this MultiKeyMap.- Parameters:
keys
- Object...; the key objects- Returns:
- T; value, or null if no value is stored under the provided key objects
-
getSubMap
Retrieve a sub map.- Parameters:
keys
- Object...; the key objects (must be at least one item shorter than the full depth)- Returns:
- MultiKeyMap<T>; the sub map
-
put
Put (add or replace) a value in this MultiKeyMap.- Parameters:
newValue
- T; the new valuekeys
- Object...; the key objects- Returns:
- T; the previous value stored under the key objects, or null if no value was currently stored under the key objects
-
getKeys
Return set of key objects on this level.- Parameters:
keys
- Object...; list of key objects (may be empty to select the key set at the top level- Returns:
- Set; set of key objects on this level. This is not a safe copy; this set reflects subsequent changes in this MultiKeyMap and modifying this set would modify this MultiKeyMap (potentially making it inconsistent).
-
clear
Clears the mapping for a key combination.- Parameters:
keys
- Object...; key combination to clear the map for- Returns:
- Object; object that was previously mapped to the key combination, or
null
if it was not cached.
-
toString
-