Package io.blt.util
Class Ctr
java.lang.Object
io.blt.util.Ctr
Static utility methods for operating on implementations of
Collection
and Map
i.e., Containers.
For methods that return a modification of a passed container, the result will be of the same type if possible.
This is accomplished using Obj.newInstanceOf(Object)
and its limitations apply.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V, E extends Throwable>
VcomputeIfAbsent
(Map<K, V> map, K key, ThrowingFunction<? super K, ? extends V, E> compute) For the specifiedmap
, if there is no value for the specifiedkey
thencompute
will be called and the result entered into the map.static <K,
V, E extends Throwable>
VcomputeIfAbsent
(Map<K, V> map, K key, ThrowingSupplier<? extends V, E> compute) For the specifiedmap
, if there is no value for the specifiedkey
thencompute
will be called and the result entered into the map.transformValues
(Map<K, V> source, ThrowingFunction<? super V, R, E> transform)
-
Method Details
-
transformValues
public static <K,V, Map<K,R, E extends Throwable> R> transformValues(Map<K, V> source, ThrowingFunction<? super V, throws ER, E> transform) Returns a newMap
containing the entries ofsource
withtransform
applied to the values.If possible, the result is of the same type as the passed
e.g.,source
map.var scores = Map.of("Louis", 95, "Greg", 92, "Mike", 71, "Phil", 86); var grades = Ctr.transformValues(scores, score -> { if (score >= 90) { return "A"; } else if (score >= 80) { return "B"; } else if (score >= 70) { return "C"; } else if (score >= 60) { return "D"; } else { return "F"; } }); // grades = Map.of("Louis", "A", "Greg", "A", "Mike", "C", "Phil", "B")
- Type Parameters:
K
-map
key typeV
-map
value typeR
- returned map value typeE
- type oftransform
throwable- Parameters:
source
-Map
whose values should be transformedtransform
- value transformation function- Returns:
- a new
Map
containing the entries ofsource
withtransform
applied to the values - Throws:
E
- See Also:
-
computeIfAbsent
public static <K,V, V computeIfAbsentE extends Throwable> (Map<K, V> map, K key, ThrowingFunction<? super K, throws E? extends V, E> compute) For the specifiedmap
, if there is no value for the specifiedkey
thencompute
will be called and the result entered into the map. If a value is present, then it is returned. e.g.,private final Map<URL, String> cache = new HashMap<>(); public String fetch(URL url) throws IOException { return Ctr.computeIfAbsent(cache, url, this::get); } private String get(URL url) throws IOException { try (var stream = url.openStream()) { return new String(stream.readAllBytes(), StandardCharsets.UTF_8); } }
If
compute
returnsnull
, then the map is not modified andnull
is returned.If
compute
throws, then map is not modified and the exception will bubble up.- Type Parameters:
K
-map
key typeV
-map
value typeE
- type ofcompute
throwable- Parameters:
map
-Map
whose value is returned and may be computedkey
- key with which the specified value is to be associatedcompute
- the computation function to use if the value is absent- Returns:
- the existing or computed value associated with the key, or null if the computed value is null
- Throws:
E
- if an exception is thrown by the computation function- See Also:
- Implementation Note:
- The implementation is equivalent to the following:
if (map.get(key) == null) { V value = compute.apply(key); if (value != null) { map.put(key, value); } } return map.get(key);
-
computeIfAbsent
public static <K,V, V computeIfAbsentE extends Throwable> (Map<K, V> map, K key, ThrowingSupplier<? extends V, throws EE> compute) For the specifiedmap
, if there is no value for the specifiedkey
thencompute
will be called and the result entered into the map. If a value is present, then it is returned. e.g.,private final Map<URL, String> cache = new HashMap<>(); public String fetch(URL url) throws IOException { return Ctr.computeIfAbsent(cache, url, () -> { try (var stream = url.openStream()) { return new String(stream.readAllBytes(), StandardCharsets.UTF_8); } }); }
If
compute
returnsnull
, then the map is not modified andnull
is returned.If
compute
throws, then map is not modified and the exception will bubble up.- Type Parameters:
K
-map
key typeV
-map
value typeE
- type ofcompute
throwable- Parameters:
map
-Map
whose value is returned and may be computedkey
- key with which the specified value is to be associatedcompute
- the computation supplier to use if the value is absent- Returns:
- the existing or computed value associated with the key, or null if the computed value is null
- Throws:
E
- if an exception is thrown by the computation supplier- See Also:
- Implementation Note:
- The implementation is equivalent to the following:
if (map.get(key) == null) { V value = compute.apply(key); if (value != null) { map.put(key, value); } } return map.get(key);
-