Class CopyUtils

java.lang.Object
org.apache.juneau.commons.utils.CopyUtils
Direct Known Subclasses:
OpenApiCopyUtils, SwaggerCopyUtils

public class CopyUtils extends Object
Utility methods for making copies and clones of collections, maps, arrays, and cloneable objects.

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 protected constructor) so that per-module utility classes can add domain-specific copyOf(<BeanType>) overloads that coexist with the inherited collection overloads via ordinary Java overload resolution. A single static-import-on-demand of such a subclass brings in both the inherited collection overloads and the module's bean overloads.

See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor — this class is meant to be subclassed.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    cloneOf(T value)
    Null-safe clone of an object via its public clone() 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>
    Creates a new collection from the specified collection.
    static <E> List<E>
    copyOf(List<E> value)
    Convenience method for copying a list.
    static <E> List<E>
    copyOf(List<E> l, Function<? super E,? extends E> valueMapper)
    Makes a deep copy of the specified list.
    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.
    static <K, V> Map<K,V>
    copyOf(Map<K,V> val)
    Creates a new map from the specified map.
    static <K, V> Map<K,V>
    copyOf(Map<K,V> m, Function<? super V,? extends V> valueMapper)
    Makes a deep copy of the specified map.
    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.
    static <E> Set<E>
    copyOf(Set<E> val)
    Creates a new set from the specified collection.
    static <E> Set<E>
    copyOf(Set<E> l, Function<? super E,? extends E> valueMapper)
    Makes a deep copy of the specified list.
    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.
    static <T> T[]
    copyOf(T[] array)
    Makes a copy of the specified array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CopyUtils

      protected CopyUtils()
      Constructor — this class is meant to be subclassed.
  • Method Details

    • copyOf

      public static <E> Collection<E> copyOf(Collection<E> val)
      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, or null if the input was null.
    • copyOf

      public static <E> List<E> copyOf(List<E> value)
      Convenience method for copying a list.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The list to copy.
      Returns:
      A new modifiable list.
    • copyOf

      public static <E> List<E> copyOf(List<E> l, Function<? super E,? extends E> valueMapper)
      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

      public static <K, V> Map<K,V> copyOf(Map<K,V> val)
      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, or null if the input was null.
    • copyOf

      public static <K, V> Map<K,V> copyOf(Map<K,V> m, Function<? super V,? extends V> valueMapper)
      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

      public static <E> Set<E> copyOf(Set<E> val)
      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, or null if the input was null.
    • copyOf

      public static <E> Set<E> copyOf(Set<E> l, Function<? super E,? extends E> valueMapper)
      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

      public static <T> T[] copyOf(T[] array)
      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 was null.s
    • copyOf

      public static byte[] copyOf(byte[] array)
      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 was null.
    • copyOf

      public static char[] copyOf(char[] array)
      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 was null.
    • copyOf

      public static int[] copyOf(int[] array)
      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 was null.
    • copyOf

      public static long[] copyOf(long[] array)
      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 was null.
    • copyOf

      public static short[] copyOf(short[] array)
      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 was null.
    • copyOf

      public static boolean[] copyOf(boolean[] array)
      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 was null.
    • copyOf

      public static double[] copyOf(double[] array)
      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 was null.
    • copyOf

      public static float[] copyOf(float[] array)
      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 was null.
    • cloneOf

      public static <T> T cloneOf(T value)
      Null-safe clone of an object via its public clone() method.

      Invokes the object's clone() method reflectively (since Object.clone() is protected and Cloneable declares no clone() 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 be null.
      Returns:
      A clone of the value, or null if the value was null.
      Throws:
      IllegalArgumentException - if the value's runtime type does not expose a public clone() method, or the clone failed.