CopyHelper (Apache Geode 1.13.3)
Date: 2021-06-22

org.apache.geode

## Class CopyHelper

- java.lang.Object
- - org.apache.geode.CopyHelper

-     public final class CopyHelper
      extends Object

  A static helper for optimally creating copies. Creating copies of cache values provides improved concurrency as well as isolation. For transactions, creating a copy is the guaranteed way to enforce "Read Committed" isolation on changes to cache `Entries`.

  Here is a simple example of how to use `CopyHelper.copy`

       Object o = r.get("stringBuf");
       StringBuffer s = (StringBuffer) CopyHelper.copy(o);
       s.append("... and they lived happily ever after.  The End.");
       r.put("stringBuf", s);

  Since:
    GemFire 4.0
  See Also:
    `Cloneable`, `Serializable`, `DataSerializer`, `GemFireCache.setCopyOnRead(boolean)`, `CacheTransactionManager`

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static <T> T` | `copy(T o)` Makes a copy of the specified object.
    `static <T> T` | `deepCopy(T o)` Makes a deep copy of the specified object o using serialization, so the object has to be serializable by GemFire.
    `static boolean` | `isWellKnownImmutableInstance(Object o)` Return true if the given object is an instance of a well known immutable class.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### isWellKnownImmutableInstance

          public static boolean isWellKnownImmutableInstance(Object o)

      Return true if the given object is an instance of a well known immutable class. The well known classes are:
      - String
      - Byte
      - Character
      - Short
      - Integer
      - Long
      - Float
      - Double
      - BigInteger
      - BigDecimal
      - UUID
      - PdxInstance but not WritablePdxInstance
      Parameters:
        `o` - the object to check
      Returns:
        true if o is an instance of a well known immutable class.
      Since:
        GemFire 6.6.2

    - #### copy

          public static <T> T copy(T o)

      Makes a copy of the specified object. The object returned is not guaranteed to be a deep copy of the original object, as explained below.

      Copies can only be made if the original is a `Cloneable` or serializable by GemFire. If o is a `well known immutable instance` then it will be returned without copying it.

      If the argument o is an instance of `Cloneable`, a copy is made by invoking `clone` on it. Note that not all implementations of `clone` make deep copies (e.g. `HashMap.clone`). Otherwise, if the argument is not an instance of `Cloneable`, a copy is made using serialization: if GemFire serialization is implemented, it is used; otherwise, java serialization is used.

      The difference between this method and `deepCopy`, is that this method uses `clone` if available, whereas `deepCopy` does not. As a result, for `Cloneable` objects copied using this method, how deep a copy the returned object is depends on its implementation of `clone`.

      Parameters:
        `o` - the original object that a copy is needed of
      Returns:
        the new instance that is a copy of of the original
      Throws:
        `CopyException` - if copying fails because a class could not be found or could not be serialized.
      Since:
        GemFire 4.0
      See Also:
        `deepCopy(Object)`

    - #### deepCopy

          public static <T> T deepCopy(T o)

      Makes a deep copy of the specified object o using serialization, so the object has to be serializable by GemFire.

      If o is a `well known immutable instance` then it will be returned without copying it.

      The passed in object is serialized in memory, and then deserialized into a new instance, which is returned. If GemFire serialization is implemented for the object, it is used; otherwise, java serialization is used.

      Parameters:
        `o` - the original object to be copied
      Returns:
        the new instance that is a copy of the original
      Throws:
        `CopyException` - if copying fails because a class could not be found or could not be serialized
      See Also:
        `copy(Object)`