org.openbp.common.util
Class CopyUtil

java.lang.Object
  extended by org.openbp.common.util.CopyUtil

public final class CopyUtil
extends java.lang.Object

Various static utility methods for copying objects. All based on usage of the Clonable interface.

Author:
Heiko Erhardt

Field Summary
static int CLONE_ALL
          copyCollection(java.util.Collection, int)/copyMap(java.util.Map, int) copy mode: Copy keys and values.
static int CLONE_KEYS
          copyMap(java.util.Map, int) copy mode: Copy keys.
static int CLONE_NONE
          copyCollection(java.util.Collection, int)/copyMap(java.util.Map, int) copy mode: No copy, reference only.
static int CLONE_VALUES
          copyCollection(java.util.Collection, int)/copyMap(java.util.Map, int) copy mode: Copy values.
 
Method Summary
static java.util.Collection copyCollection(java.util.Collection source, int copyMode)
          Creates a copy of a collection object.
static java.util.Map copyMap(java.util.Map source, int copyMode)
          Creates a copy of a map object.
static java.lang.Object copyObject(java.lang.Object object, int copyMode, java.lang.ClassLoader classLoader)
          Creates a copy of an arbitrary object if it supports the clone interface.
static java.lang.Object createNew(java.lang.Object object)
          Returns a new of the same type as the given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLONE_NONE

public static final int CLONE_NONE
copyCollection(java.util.Collection, int)/copyMap(java.util.Map, int) copy mode: No copy, reference only. The new map will refer to the same objects as the original.

See Also:
Constant Field Values

CLONE_KEYS

public static final int CLONE_KEYS
copyMap(java.util.Map, int) copy mode: Copy keys. The new map will contain deep clones of the original.

See Also:
Constant Field Values

CLONE_VALUES

public static final int CLONE_VALUES
copyCollection(java.util.Collection, int)/copyMap(java.util.Map, int) copy mode: Copy values. The new map will contain deep clones of the original.

See Also:
Constant Field Values

CLONE_ALL

public static final int CLONE_ALL
copyCollection(java.util.Collection, int)/copyMap(java.util.Map, int) copy mode: Copy keys and values. The new map will contain deep clones of the original.

See Also:
Constant Field Values
Method Detail

createNew

public static java.lang.Object createNew(java.lang.Object object)
                                  throws java.lang.CloneNotSupportedException
Returns a new of the same type as the given object.

Parameters:
object - Object to create an empty clone for or null
Returns:
The new object or null if object is null
Throws:
java.lang.CloneNotSupportedException - On error

copyObject

public static java.lang.Object copyObject(java.lang.Object object,
                                          int copyMode,
                                          java.lang.ClassLoader classLoader)
                                   throws java.lang.CloneNotSupportedException
Creates a copy of an arbitrary object if it supports the clone interface. This is to work around the fact the Object.clone method is a protected method. The protected access mode will be usually overridden as a public one by objects that implement the method.

Parameters:
object - The object to copy or null
copyMode - Determines if a deep copy, a first level copy or a shallow copy is to be performed (Copyable.COPY_SHALLOW/Copyable.COPY_FIRST_LEVEL/Copyable.COPY_DEEP).
Note that this can be accounted for only if the object implements the Copyable interface. If COPY_DEEP is specified, we will use the object's clone method (is is defined to use deep copy mode). Otherwise, we try to construct a new object using the default constructor and apply the Copyable.copyFrom(java.lang.Object, int) method on it.
If the object does not implement Copyable, the method will try to use the object's clone method. The clone method will be called using the Java reflection API.
classLoader - Class loader if the serialize/deserialize approach (last fallback) will be used
Returns:
The copied object or null if the object itself is null
Throws:
java.lang.CloneNotSupportedException - If the object cannot be cloned

copyCollection

public static java.util.Collection copyCollection(java.util.Collection source,
                                                  int copyMode)
                                           throws java.lang.CloneNotSupportedException
Creates a copy of a collection object.

Parameters:
source - The collection to copy or null
copyMode - Determines if a first level copy or a deep copy is to be performed (CLONE_NONE/CLONE_VALUES/CLONE_ALL)
Returns:
The copied map or null
Throws:
java.lang.CloneNotSupportedException - If either the collection object or one of its value cannot be cloned

copyMap

public static java.util.Map copyMap(java.util.Map source,
                                    int copyMode)
                             throws java.lang.CloneNotSupportedException
Creates a copy of a map object.

Parameters:
source - The map to copy or null
copyMode - Determines if a first level copy or a deep copy is to be performed (CLONE_NONE/CLONE_KEYS/CLONE_VALUES/CLONE_ALL)
Returns:
The copied map or null
Throws:
java.lang.CloneNotSupportedException - If either the map object or one of its keys or value cannot be cloned


Copyright © 2011. All Rights Reserved.