Package net.sourceforge.pmd.util
Class DataMap<K>
- java.lang.Object
-
- net.sourceforge.pmd.util.DataMap<K>
-
- Type Parameters:
K
- Type of keys in this map.
public final class DataMap<K> extends Object
An opaque, strongly typed heterogeneous data container. Data maps can be set to accept only a certain type of key, with the type parameter. The key can itself constrain the type of values, using its own type parameterT
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DataMap.DataKey<K extends DataMap.DataKey<K,T>,T>
A key for type-safe access into aDataMap
.static class
DataMap.SimpleDataKey<T>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
compute(DataMap.DataKey<? extends K,T> key, Function<? super @Nullable T,? extends T> function)
Create or replace a mapping with a value computed from the current value (or null if missing).<T> T
computeIfAbsent(DataMap.DataKey<? extends K,T> key, Supplier<? extends T> supplier)
Retrieve the value, or compute it if it is missing.<T> @Nullable T
get(DataMap.DataKey<? extends K,? extends T> key)
Retrieves the data currently mapped to the key.<T> T
getOrDefault(DataMap.DataKey<? extends K,? extends T> key, T defaultValue)
boolean
isSet(DataMap.DataKey<? extends K,?> key)
Returns true if the given key has a non-null value in the map.<T> T
merge(DataMap.DataKey<? extends K,T> key, T value, BiFunction<? super @NonNull T,? super T,? extends T> function)
static <K> DataMap<K>
newDataMap()
<T> @Nullable T
set(DataMap.DataKey<? extends K,? super T> key, T data)
Set the mapping to the given data.static <T> DataMap.SimpleDataKey<T>
simpleDataKey(String name)
-
-
-
Method Detail
-
set
public <T> @Nullable T set(DataMap.DataKey<? extends K,? super T> key, T data)
Set the mapping to the given data.- Type Parameters:
T
- Type of the data- Parameters:
key
- Keydata
- Data mapped to the key- Returns:
- Previous value associated with the key (nullable)
-
get
public <T> @Nullable T get(DataMap.DataKey<? extends K,? extends T> key)
Retrieves the data currently mapped to the key.- Type Parameters:
T
- Type of the data- Parameters:
key
- Key- Returns:
- Value associated with the key (nullable)
-
getOrDefault
public <T> T getOrDefault(DataMap.DataKey<? extends K,? extends T> key, T defaultValue)
-
computeIfAbsent
public <T> T computeIfAbsent(DataMap.DataKey<? extends K,T> key, Supplier<? extends T> supplier)
Retrieve the value, or compute it if it is missing.- Type Parameters:
T
- Type of the data- Parameters:
key
- Keysupplier
- Supplier for a value- Returns:
- Value associated with the key (as nullable as the
-
compute
public <T> T compute(DataMap.DataKey<? extends K,T> key, Function<? super @Nullable T,? extends T> function)
Create or replace a mapping with a value computed from the current value (or null if missing).- Type Parameters:
T
- Type of the data- Parameters:
key
- Keyfunction
- Supplier for a value- Returns:
- Value returned by the parameter function
-
merge
public <T> T merge(DataMap.DataKey<? extends K,T> key, T value, BiFunction<? super @NonNull T,? super T,? extends T> function)
- See Also:
Map.merge(Object, Object, BiFunction)
-
isSet
public boolean isSet(DataMap.DataKey<? extends K,?> key)
Returns true if the given key has a non-null value in the map.- Parameters:
key
- Key- Returns:
- True if some value is set
-
newDataMap
public static <K> DataMap<K> newDataMap()
-
simpleDataKey
public static <T> DataMap.SimpleDataKey<T> simpleDataKey(String name)
-
-