Package software.amazon.smithy.utils
Class MapUtils
java.lang.Object
software.amazon.smithy.utils.MapUtils
Immutable Map utilities to polyfill Java 9+ features.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Map<K, V> Creates an immutable copy of the given map.static <K,
V> Map.Entry<K, V> entry
(K key, V value) Creates an immutableMap.Entry
from the given key and value.static <K,
V> Map<K, V> of()
Returns an unmodifiable map containing zero mappings.static <K,
V> Map<K, V> of
(K k1, V v1) Returns an unmodifiable map containing a single mapping.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2) Returns an unmodifiable map containing two mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3) Returns an unmodifiable map containing three mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Returns an unmodifiable map containing four mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Returns an unmodifiable map containing five mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Returns an unmodifiable map containing six mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Returns an unmodifiable map containing seven mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Returns an unmodifiable map containing eight mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) Returns an unmodifiable map containing nine mappings.static <K,
V> Map<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) Returns an unmodifiable map containing ten mappings.static <K,
V> Map<K, V> Creates an unmodifiable Map from an array ofMap.Entry
values.static <K,
V> Map<K, V> orderedCopyOf
(Map<? extends K, ? extends V> map) Creates an ordered immutable copy of the given map.toUnmodifiableMap
(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Creates a collector that collects into an unmodifiable Map.
-
Method Details
-
copyOf
Creates an immutable copy of the given map.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
map
- The map to make an immutable copy of- Returns:
- An immutable Map copy
-
orderedCopyOf
Creates an ordered immutable copy of the given map.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
map
- The map to make an immutable copy of- Returns:
- An ordered immutable Map copy that maintains the order of the original.
-
of
Returns an unmodifiable map containing zero mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Returns:
- an empty Map
-
of
Returns an unmodifiable map containing a single mapping.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the mapping's keyv1
- the mapping's value- Returns:
- a Map containing the specified mapping
- Throws:
NullPointerException
- if the key or the value isnull
-
of
Returns an unmodifiable map containing two mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if the keys are duplicatesNullPointerException
- if any key or value isnull
-
of
Returns an unmodifiable map containing three mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
Returns an unmodifiable map containing four mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
Returns an unmodifiable map containing five mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's valuek5
- the fifth mapping's keyv5
- the fifth mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
public static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Returns an unmodifiable map containing six mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's valuek5
- the fifth mapping's keyv5
- the fifth mapping's valuek6
- the sixth mapping's keyv6
- the sixth mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
public static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Returns an unmodifiable map containing seven mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's valuek5
- the fifth mapping's keyv5
- the fifth mapping's valuek6
- the sixth mapping's keyv6
- the sixth mapping's valuek7
- the seventh mapping's keyv7
- the seventh mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
public static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Returns an unmodifiable map containing eight mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's valuek5
- the fifth mapping's keyv5
- the fifth mapping's valuek6
- the sixth mapping's keyv6
- the sixth mapping's valuek7
- the seventh mapping's keyv7
- the seventh mapping's valuek8
- the eighth mapping's keyv8
- the eighth mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
public static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) Returns an unmodifiable map containing nine mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's valuek5
- the fifth mapping's keyv5
- the fifth mapping's valuek6
- the sixth mapping's keyv6
- the sixth mapping's valuek7
- the seventh mapping's keyv7
- the seventh mapping's valuek8
- the eighth mapping's keyv8
- the eighth mapping's valuek9
- the ninth mapping's keyv9
- the ninth mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
of
public static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) Returns an unmodifiable map containing ten mappings.- Type Parameters:
K
- the Map's key typeV
- the Map's value type- Parameters:
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuek3
- the third mapping's keyv3
- the third mapping's valuek4
- the fourth mapping's keyv4
- the fourth mapping's valuek5
- the fifth mapping's keyv5
- the fifth mapping's valuek6
- the sixth mapping's keyv6
- the sixth mapping's valuek7
- the seventh mapping's keyv7
- the seventh mapping's valuek8
- the eighth mapping's keyv8
- the eighth mapping's valuek9
- the ninth mapping's keyv9
- the ninth mapping's valuek10
- the tenth mapping's keyv10
- the tenth mapping's value- Returns:
- a Map containing the specified mappings
- Throws:
IllegalArgumentException
- if there are any duplicate keysNullPointerException
- if any key or value isnull
-
entry
Creates an immutableMap.Entry
from the given key and value.- Type Parameters:
K
- Key type.V
- Value type.- Parameters:
key
- Key to set in theMap.Entry
.value
- Value to set in theMap.Entry
.- Returns:
- Returns the created entry.
- Throws:
NullPointerException
- if the key or value are null.
-
ofEntries
Creates an unmodifiable Map from an array ofMap.Entry
values.- Type Parameters:
K
- Map key type.V
- Map value type.- Parameters:
entries
- Map entries to add to the createdMap
.- Returns:
- Returns an unmodifiable map of the given entries.
-
toUnmodifiableMap
public static <T,K, Collector<T,U> ?, toUnmodifiableMapMap<K, U>> (Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) Creates a collector that collects into an unmodifiable Map.This is a polyfill equivalent of Java 10's
Collectors#toUnmodifiableMap
.- Type Parameters:
T
- the type to retrieve keys and values from.K
- the Map's key type.U
- the Map's value type.- Parameters:
keyMapper
- Function that retrieves the key.valueMapper
- Function that retrieves the value.- Returns:
- a Collector that accumulates the entries into an unmodifiable Map.
-