Class CloneUtils


  • public class CloneUtils
    extends Object
    Some utility methods to use serialization to allow for deep copy cloning without knowing anything about the objects type
    Author:
    markr
    • Constructor Detail

      • CloneUtils

        public CloneUtils()
    • Method Detail

      • serialize

        public static byte[] serialize​(Object object)
                                throws IOException
        Serialise an object
        Parameters:
        object - to serialise
        Returns:
        byte array
        Throws:
        IOException - thrown if error
      • deSerialize

        public static <T> T deSerialize​(byte[] array)
                                 throws IOException,
                                        ClassNotFoundException
        Deserialise based on byte array to object
        Type Parameters:
        T - type to deserialize
        Parameters:
        array - to deserialize
        Returns:
        object created
        Throws:
        IOException - thrown if error
        ClassNotFoundException - thrown if error
      • deepClone

        public static <T> T deepClone​(T object)
        clone an object by using the serialise/deserialise methods in this class. This is a costly operation
        Type Parameters:
        T - object type
        Parameters:
        object - to clone
        Returns:
        cloned version
      • deepCloneFromTo

        public static <K,​V> Map<K,​V> deepCloneFromTo​(Map<K,​V> sourceMap,
                                                                 Map<K,​V> destinationMap)
        clone object values (not keys) in source map by using the serialise/deserialise methods in this class. This is a costly operation
        Type Parameters:
        K - key type
        V - object value type
        Parameters:
        sourceMap - to clone
        destinationMap - to populate
        Returns:
        destination map