Class CollectionCopies
The helpers make the immutability and null-handling choices explicit at call sites:
immutable*methods are intended for record components and return immutable collections.nullable*methods preserve thenullvalue of optional components.*AllowingNullsmethods are intended for JSON-like maps such as metadata, params, and headers, wherenullvalues may be valid protocol data.mutable*CopyOrEmptymethods are intended for builders that need mutable copies.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> List<E>immutableList(List<E> list) Creates an immutable defensive copy of a non-null list.static <E> List<E>immutableListOrEmpty(@Nullable List<E> list) Creates an immutable defensive copy of a nullable list, defaulting to an empty immutable list.static <K,V> Map<K, V> immutableMap(Map<K, V> map) Creates an immutable defensive copy of a non-null map.static <E> @Nullable List<E>immutableNullableList(@Nullable List<E> list) Creates an immutable defensive copy of a nullable list.static <K,V> @Nullable Map<K, V> immutableNullableMap(@Nullable Map<K, V> map) Creates an immutable defensive copy of a nullable map.static <E> List<E>mutableListCopyOrEmpty(@Nullable List<E> list) Creates a mutable defensive copy of a nullable list.static <K,V> Map<K, V> mutableMapCopyOrEmpty(@Nullable Map<K, V> map) Creates a mutable defensive copy of a nullable map.static <K,V> @Nullable Map<K, V> unmodifiableNullableShallowMap(@Nullable Map<K, V> map) Creates an unmodifiable shallow defensive copy of a nullable map while preserving null values.static <K,V> Map<K, V> unmodifiableShallowMap(Map<K, V> map) Creates an unmodifiable shallow defensive copy of a non-null map while preserving null values.
-
Method Details
-
immutableList
Creates an immutable defensive copy of a non-null list.This method uses
List.copyOf(java.util.Collection)and therefore preserves its null-handling semantics: null elements are not allowed.- Type Parameters:
E- the element type- Parameters:
list- the source list, must not benull- Returns:
- an immutable defensive copy
-
immutableNullableList
Creates an immutable defensive copy of a nullable list.If the source list is
null, this method returnsnull. Otherwise it usesList.copyOf(java.util.Collection)and therefore does not allow null elements.- Type Parameters:
E- the element type- Parameters:
list- the source list, ornull- Returns:
nullif the source isnull, otherwise an immutable defensive copy
-
immutableListOrEmpty
Creates an immutable defensive copy of a nullable list, defaulting to an empty immutable list.This method is intended for fields where a missing list should be normalized to an empty list rather than preserving
null. It usesList.copyOf(java.util.Collection)for non-null input and therefore does not allow null elements.- Type Parameters:
E- the element type- Parameters:
list- the source list, ornull- Returns:
- an immutable empty list if the source is
null, otherwise an immutable defensive copy
-
immutableMap
Creates an immutable defensive copy of a non-null map.This method uses
Map.copyOf(Map)and is intended for typed maps where null keys and null values are not valid data.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map, must not benull- Returns:
- an immutable defensive copy
-
immutableNullableMap
Creates an immutable defensive copy of a nullable map.If the source map is
null, this method returnsnull. Otherwise it usesMap.copyOf(Map)and is intended for typed maps where null keys and null values are not valid data.- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map, ornull- Returns:
nullif the source isnull, otherwise an immutable defensive copy
-
unmodifiableShallowMap
Creates an unmodifiable shallow defensive copy of a non-null map while preserving null values.This method is intended for JSON-like maps such as metadata, params, and headers, where null values may be valid protocol data. It intentionally does not use
Map.copyOf(Map), becauseMap.copyOfthrowsNullPointerExceptionwhen the source map contains null keys or values.The returned map cannot be structurally modified, but this method does not deep-copy mutable keys or values.
- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map, must not benull- Returns:
- an unmodifiable shallow copy preserving null values
-
unmodifiableNullableShallowMap
Creates an unmodifiable shallow defensive copy of a nullable map while preserving null values.If the source map is
null, this method returnsnull. Otherwise it behaves likeunmodifiableShallowMap(Map).- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map, ornull- Returns:
nullif the source isnull, otherwise an unmodifiable shallow copy preserving null values
-
mutableListCopyOrEmpty
Creates a mutable defensive copy of a nullable list.This method is intended for builders that need mutable collection state.
- Type Parameters:
E- the element type- Parameters:
list- the source list, ornull- Returns:
- a mutable defensive copy, or an empty mutable list if the source is
null
-
mutableMapCopyOrEmpty
Creates a mutable defensive copy of a nullable map.This method is intended for builders that need mutable collection state.
- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the source map, ornull- Returns:
- a mutable defensive copy, or an empty mutable map if the source is
null
-