javax.annotation.concurrent.Immutable Java Examples

The following examples show how to use javax.annotation.concurrent.Immutable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloneHelper.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Get a clone (= deep copy) of the passed value. The following things are
 * tried for cloning:
 * <ol>
 * <li>If the object is immutable, it is returned as is (if it is a primitive
 * type or marked with the {@link Immutable} annotation.</li>
 * <li>If the object implements {@link ICloneable} it is invoked.</li>
 * <li>If the object implements {@link Cloneable} it is invoked.</li>
 * <li>If a copy constructor (a constructor taking one argument of the same
 * class as it declares)</li>
 * </ol>
 * If all tries fail, <code>null</code> is returned.
 *
 * @param <DATATYPE>
 *        The source and return type
 * @param aObject
 *        The object to be copied.
 * @return <code>null</code> if the passed value is <code>null</code> or if no
 *         cloning could be performed.
 */
@Nullable
public static <DATATYPE> DATATYPE getClonedValue (@Nullable final DATATYPE aObject)
{
  // null -> null
  if (aObject == null)
    return null;

  final Class <?> aClass = aObject.getClass ();

  // special handling for immutable objects without equals or clone
  if (ClassHelper.isPrimitiveWrapperType (aClass) ||
      aObject instanceof String ||
      aClass.getAnnotation (Immutable.class) != null)
    return aObject;

  // generic clone
  return _getGenericClone (aObject);
}