Class CopyUtils
- Direct Known Subclasses:
OpenApiCopyUtils,SwaggerCopyUtils
This class is the canonical home for the copyOf(...) family (collection/map/array defensive copies)
and cloneOf(Object) (reflective null-safe clone). It is intentionally subclassable (via a
See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor — this class is meant to be subclassed. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TcloneOf(T value) Null-safe clone of an object via its publicclone()method.static boolean[]copyOf(boolean[] array) Makes a copy of the specified array.static byte[]copyOf(byte[] array) Makes a copy of the specified array.static char[]copyOf(char[] array) Makes a copy of the specified array.static double[]copyOf(double[] array) Makes a copy of the specified array.static float[]copyOf(float[] array) Makes a copy of the specified array.static int[]copyOf(int[] array) Makes a copy of the specified array.static long[]copyOf(long[] array) Makes a copy of the specified array.static short[]copyOf(short[] array) Makes a copy of the specified array.static <E> Collection<E>copyOf(Collection<E> val) Creates a new collection from the specified collection.static <E> List<E>Convenience method for copying a list.static <E> List<E>Makes a deep copy of the specified list.static <E> List<E>Makes a deep copy of the specified list.static <K,V> Map<K, V> Creates a new map from the specified map.static <K,V> Map<K, V> Makes a deep copy of the specified map.static <K,V> Map<K, V> Makes a deep copy of the specified map.static <E> Set<E>Creates a new set from the specified collection.static <E> Set<E>Makes a deep copy of the specified list.static <E> Set<E>Makes a deep copy of the specified list.static <T> T[]copyOf(T[] array) Makes a copy of the specified array.
-
Constructor Details
-
CopyUtils
protected CopyUtils()Constructor — this class is meant to be subclassed.
-
-
Method Details
-
copyOf
Creates a new collection from the specified collection.- Type Parameters:
E- The element type.- Parameters:
val- The value to copy from.- Returns:
- A new
LinkedHashSet, ornull if the input was null.
-
copyOf
Convenience method for copying a list.- Type Parameters:
E- The element type.- Parameters:
value- The list to copy.- Returns:
- A new modifiable list.
-
copyOf
Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
public static <E> List<E> copyOf(List<E> l, Function<? super E, ? extends E> valueMapper, Supplier<List<E>> listFactory) Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.listFactory- The factory for creating the list.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
Creates a new map from the specified map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
val- The value to copy from.- Returns:
- A new
LinkedHashMap, ornull if the input was null.
-
copyOf
Makes a deep copy of the specified map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
m- The map to copy.valueMapper- The function to apply to each value in the map.- Returns:
- A new map with the same keys as the specified map, but with values transformed by the specified function. Null if the map being copied was null.
-
copyOf
public static <K,V> Map<K,V> copyOf(Map<K, V> m, Function<? super V, ? extends V> valueMapper, Supplier<Map<K, V>> mapFactory) Makes a deep copy of the specified map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
m- The map to copy.valueMapper- The function to apply to each value in the map.mapFactory- The factory for creating the map.- Returns:
- A new map with the same keys as the specified map, but with values transformed by the specified function. Null if the map being copied was null.
-
copyOf
Creates a new set from the specified collection.- Type Parameters:
E- The element type.- Parameters:
val- The value to copy from.- Returns:
- A new
LinkedHashSet, ornull if the input was null.
-
copyOf
Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
public static <E> Set<E> copyOf(Set<E> l, Function<? super E, ? extends E> valueMapper, Supplier<Set<E>> setFactory) Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.setFactory- The factory for creating sets.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
Makes a copy of the specified array.- Type Parameters:
T- The element type.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .s
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
copyOf
Makes a copy of the specified array.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .
-
cloneOf
Null-safe clone of an object via its publicclone()method.Invokes the object's
clone()method reflectively (sinceObject.clone()isprotected andCloneabledeclares noclone()method, a generic caller cannot invoke it directly), returning the result typed as the input.- Type Parameters:
T- The object type.- Parameters:
value- The value to clone. Can benull .- Returns:
- A clone of the value, or
null if the value wasnull . - Throws:
IllegalArgumentException- if the value's runtime type does not expose a publicclone()method, or the clone failed.
-