Class CloneHelper


  • @Immutable
    public final class CloneHelper
    extends Object
    Helper class for cloning objects.
    Author:
    Philip Helger
    • Method Detail

      • getClonedValue

        @Nullable
        public static <DATATYPE> DATATYPE getClonedValue​(@Nullable
                                                         DATATYPE aObject)
        Get a clone (= deep copy) of the passed value. The following things are tried for cloning:
        1. If the object is immutable, it is returned as is (if it is a primitive type or marked with the Immutable annotation.
        2. If the object implements ICloneable it is invoked.
        3. If the object implements Cloneable it is invoked.
        4. If a copy constructor (a constructor taking one argument of the same class as it declares)
        If all tries fail, null is returned.
        Type Parameters:
        DATATYPE - The source and return type
        Parameters:
        aObject - The object to be copied.
        Returns:
        null if the passed value is null or if no cloning could be performed.
      • getCloneIfNotNull

        @Nullable
        public static <DATATYPE extends ICloneable<DATATYPE>> DATATYPE getCloneIfNotNull​(@Nullable
                                                                                         DATATYPE aObject)
        Get a clone (= deep copy) of the passed value for all objects implementing ICloneable.
        Type Parameters:
        DATATYPE - The data type to be cloned
        Parameters:
        aObject - The object to be copied. May be null.
        Returns:
        null if the passed value is null or a clone of the object.
      • getGenericClonedList

        @Nonnull
        @ReturnsMutableCopy
        public static <DATATYPE> ICommonsList<DATATYPE> getGenericClonedList​(@Nullable
                                                                             Iterable<DATATYPE> aList)
        Get a list where each contained item is also cloned. Like a deep copy.
        Type Parameters:
        DATATYPE - The list element type to be cloned
        Parameters:
        aList - Source list. May be null.
        Returns:
        The cloned list. Never null but maybe empty if the source list is empty.
      • getClonedList

        @Nonnull
        @ReturnsMutableCopy
        public static <DATATYPE extends ICloneable<DATATYPE>> ICommonsList<DATATYPE> getClonedList​(@Nullable
                                                                                                   Iterable<DATATYPE> aList)
        Get a list where each contained item is also cloned. Like a deep copy.
        Type Parameters:
        DATATYPE - The set element type to be cloned
        Parameters:
        aList - Source list. May be null.
        Returns:
        The cloned list. Never null but maybe empty if the source list is empty.