uka.transport
Class DeepClone

java.lang.Object
  extended byuka.transport.DeepClone
All Implemented Interfaces:
Printable, java.io.Serializable
Direct Known Subclasses:
CopyEnvironment

public class DeepClone
extends java.lang.Object
implements Printable, java.io.Serializable

Provides reference cycle resolution in the deep-clone process. Objects of a class that implements the DeepCloneable interface, can also be fast locally cloned. Unlike the regular Java cloning mechanism (see Object.clone()), a clone produced by the uka.transport serialization is a deep clone with all referenced objects also being cloned. The deep clone facility is a fast replacement for writing an object into a local stream and reading it back immediately.

The method DeepCloneable#deepClone(DeepClone) should produce a deep clone by first calling Object.clone() and then cloning all fields of reference type. The references to other objects should be cloned by calling the appropriate doDeepClone(Object) method of this class.

Author:
Bernhard Haumacher
See Also:
Serialized Form

Nested Class Summary
(package private)  class DeepClone.CopyMarshalStream
           
(package private)  class DeepClone.CopyUnmarshalStream
           
 
Field Summary
(package private)  MemoryBuffer buffer
           
(package private)  Queue completionQueue
           
(package private)  ObjectSpace copies
           
(package private)  DeepClone.CopyUnmarshalStream in
           
(package private)  ObjectSpace originals
           
(package private)  DeepClone.CopyMarshalStream out
           
(package private)  EnlargingIntArray streamCopyIDs
          Stores all object identifiers for copies that are created by the fall-back cloning through an DeepClone.CopyMarshalStream/DeepClone.CopyUnmarshalStream pair.
(package private)  boolean streamUsed
           
 
Constructor Summary
DeepClone()
           
DeepClone(ObjectSpace originals, ObjectSpace copies)
          Creates a DeepClone object that operates on application-provided object spaces.
 
Method Summary
 int add(int id, java.lang.Object copy)
           
 void appendTo(ToString s)
          This method should append the contents of each instance variable of the current object to the given ToString object.
private  java.lang.Object[] doDeepClone(int id, java.lang.Object[] orig, java.lang.Class component)
           
 java.lang.Object doDeepClone(java.lang.Object orig)
          Deeply clones an arbitrary object.
 java.lang.Object[] doDeepClone(java.lang.Object[] orig)
          Deeply clones an object array.
 java.lang.Object internalCloneByMarshal(java.lang.Object orig)
          Implements the fall-back strategy for deep cloning by marshaling to an ObjectOutputStream and reading the object back via an ObjectInputStream.
 java.lang.Object internalDeepClone(java.lang.Object orig)
           
private  java.lang.Object internalDeepClone(java.lang.Object orig, int id)
           
 java.lang.Object internalDeepCloneAnonymous(java.lang.Object orig)
           
protected  boolean preserveReference(java.lang.Object orig)
          A subclass may decide for which objects a reference should be used instead of a copy.
protected  java.lang.Object replaceObject(java.lang.Object obj)
           
 void reset()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

originals

final ObjectSpace originals

copies

final ObjectSpace copies

completionQueue

final Queue completionQueue

streamUsed

transient boolean streamUsed

buffer

transient MemoryBuffer buffer

out

transient DeepClone.CopyMarshalStream out

in

transient DeepClone.CopyUnmarshalStream in

streamCopyIDs

transient EnlargingIntArray streamCopyIDs
Stores all object identifiers for copies that are created by the fall-back cloning through an DeepClone.CopyMarshalStream/DeepClone.CopyUnmarshalStream pair.

See Also:
DeepClone.CopyMarshalStream.replaceObject(Object)
Constructor Detail

DeepClone

public DeepClone()

DeepClone

public DeepClone(ObjectSpace originals,
                 ObjectSpace copies)
Creates a DeepClone object that operates on application-provided object spaces.

Parameters:
originals - is an object space that keeps track of all objects that were presented to this DeepClone object as source objects being cloned.
copies - keeps all objects that were created during the clone process. Original objects and their clones are identified by the same object identifier in both object spaces. An object for which a clone is requested, may already be present in the originals space. In that case, the object identifier assigned there is also used for the clone that will be inserted into the copies space. If an object is already present in both, the originals and copies space, the object is not cloned again, but its existing clone form the copies space is returned. If an object is neither present in the originals nor the copies space, a new identifier is allocated from the originals space via ObjectSpace.getID(java.lang.Object). Afterwards the object and its clone is entered into the originals and copies space using the newly allocated identifier. The copies space is never used to allocate a new identifier.
Method Detail

reset

public void reset()

add

public int add(int id,
               java.lang.Object copy)

doDeepClone

public final java.lang.Object[] doDeepClone(java.lang.Object[] orig)
                                     throws java.lang.CloneNotSupportedException
Deeply clones an object array.

Throws:
java.lang.CloneNotSupportedException

doDeepClone

private java.lang.Object[] doDeepClone(int id,
                                       java.lang.Object[] orig,
                                       java.lang.Class component)
                                throws java.lang.CloneNotSupportedException
Throws:
java.lang.CloneNotSupportedException

doDeepClone

public final java.lang.Object doDeepClone(java.lang.Object orig)
                                   throws java.lang.CloneNotSupportedException
Deeply clones an arbitrary object. If the object is actually an array, the appropriate doDeepClone(java.lang.Object[]) is called.

Throws:
java.lang.CloneNotSupportedException

internalDeepClone

public final java.lang.Object internalDeepClone(java.lang.Object orig)
                                         throws java.lang.CloneNotSupportedException
Throws:
java.lang.CloneNotSupportedException

internalDeepCloneAnonymous

public final java.lang.Object internalDeepCloneAnonymous(java.lang.Object orig)
                                                  throws java.lang.CloneNotSupportedException
Throws:
java.lang.CloneNotSupportedException

internalDeepClone

private java.lang.Object internalDeepClone(java.lang.Object orig,
                                           int id)
                                    throws java.lang.CloneNotSupportedException
Throws:
java.lang.CloneNotSupportedException

internalCloneByMarshal

public java.lang.Object internalCloneByMarshal(java.lang.Object orig)
                                        throws java.lang.CloneNotSupportedException
Implements the fall-back strategy for deep cloning by marshaling to an ObjectOutputStream and reading the object back via an ObjectInputStream.

Throws:
java.lang.CloneNotSupportedException

preserveReference

protected boolean preserveReference(java.lang.Object orig)
A subclass may decide for which objects a reference should be used instead of a copy. This is useful for immutable objects. By default all immutable classes defined by Classes.isImmutable(java.lang.Object) are prevented from cloning.


replaceObject

protected java.lang.Object replaceObject(java.lang.Object obj)
                                  throws java.io.IOException
Throws:
java.io.IOException

appendTo

public void appendTo(ToString s)
Description copied from interface: Printable
This method should append the contents of each instance variable of the current object to the given ToString object. The appended data should be labeled with the name of the corresponding instance variable.

Specified by:
appendTo in interface Printable
See Also:
ToString, ToString.append(String, Object), ToString.append(String, boolean), ToString.append(String, byte), ToString.append(String, int)