Package com.helger.commons.lang
Class CloneHelper
java.lang.Object
com.helger.commons.lang.CloneHelper
Helper class for cloning objects.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic <DATATYPE extends ICloneable<DATATYPE>>
ICommonsList<DATATYPE> getClonedList(Iterable<DATATYPE> aList) Get a list where each contained item is also cloned.static <DATATYPE> DATATYPEgetClonedValue(DATATYPE aObject) Get a clone (= deep copy) of the passed value.static <DATATYPE extends ICloneable<DATATYPE>>
DATATYPEgetCloneIfNotNull(DATATYPE aObject) Get a clone (= deep copy) of the passed value for all objects implementingICloneable.static <DATATYPE> ICommonsList<DATATYPE> getGenericClonedList(Iterable<DATATYPE> aList) Get a list where each contained item is also cloned.
-
Method Details
-
getClonedValue
Get a clone (= deep copy) of the passed value. The following things are tried for cloning:- If the object is immutable, it is returned as is (if it is a primitive
type or marked with the
Immutableannotation. - If the object implements
ICloneableit is invoked. - If the object implements
Cloneableit is invoked. - If a copy constructor (a constructor taking one argument of the same class as it declares)
nullis returned.- Type Parameters:
DATATYPE- The source and return type- Parameters:
aObject- The object to be copied.- Returns:
nullif the passed value isnullor if no cloning could be performed.
- If the object is immutable, it is returned as is (if it is a primitive
type or marked with the
-
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 implementingICloneable.- Type Parameters:
DATATYPE- The data type to be cloned- Parameters:
aObject- The object to be copied. May benull.- Returns:
nullif the passed value isnullor 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 benull.- Returns:
- The cloned list. Never
nullbut 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 benull.- Returns:
- The cloned list. Never
nullbut maybe empty if the source list is empty.
-