Class CloneHelper

java.lang.Object
com.helger.base.clone.CloneHelper

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

    • getClonedValue

      public static <DATATYPE> @Nullable 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

      public static <DATATYPE extends ICloneable<DATATYPE>> @Nullable 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.