com.threerings.util
Class DeepUtil

java.lang.Object
  extended by com.threerings.util.DeepUtil

public class DeepUtil
extends Object

Various methods that use reflection to perform "deep" operations: copying, comparison, etc. The usual warnings about circular references apply.


Nested Class Summary
protected static class DeepUtil.FieldHandler
          Copies or compares a field of a single type.
protected static class DeepUtil.ObjectHandler<T>
          Performs the actual object operations.
protected static class DeepUtil.ReflectiveObjectHandler
          Handles an object according to its reflected fields.
 
Field Summary
protected static Map<Class<?>,DeepUtil.ObjectHandler> _objectHandlers
          Object handlers mapped by class.
protected static DeepUtil.ObjectHandler ARRAY_OBJECT_HANDLER
          Field handler for object arrays.
protected static DeepUtil.FieldHandler DEEP_OBJECT_FIELD_HANDLER
          Field handler for deep object fields.
protected static DeepUtil.FieldHandler DEFAULT_OBJECT_FIELD_HANDLER
          Default handler for object fields.
protected static DeepUtil.ObjectHandler IMMUTABLE_OBJECT_HANDLER
          Field handler for immutable fields, which can be handled by reference.
protected static Map<Class<?>,DeepUtil.FieldHandler> PRIMITIVE_FIELD_HANDLERS
          Handlers for primitive fields mapped by class.
protected static DeepUtil.FieldHandler SHALLOW_OBJECT_FIELD_HANDLER
          Field handler for shallow object fields.
 
Constructor Summary
DeepUtil()
           
 
Method Summary
static
<T> T
copy(T source)
          Creates and returns a deep copy of an object using reflection.
static
<T> T
copy(T source, T dest)
          Creates a deep copy of an object using reflection, storing the result in the object provided if possible.
static
<T> T
copy(T source, T dest, Object outer)
          Creates a deep copy of an object using reflection, storing the result in the object provided if possible.
static
<T> boolean
equals(T o1, T o2)
          Compares two objects for deep equality.
protected static void getInstanceFields(Class<?> clazz, ArrayList<Field> fields)
          Populates the supplied list with the copyable/comparable fields of the given class.
protected static DeepUtil.ObjectHandler getObjectHandler(Class<?> clazz)
          Retrieves the handler for the supplied class.
static int hashCode(Object object)
          Computes the deep hash code of an object.
static
<T> T
transfer(T source, T dest)
          Transfers the state in the shared ancestry of the two arguments from the source to the destination.
static
<T> T
transfer(T source, T dest, Object outer)
          Transfers the state in the shared ancestry of the two arguments from the source to the destination.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IMMUTABLE_OBJECT_HANDLER

protected static final DeepUtil.ObjectHandler IMMUTABLE_OBJECT_HANDLER
Field handler for immutable fields, which can be handled by reference.


_objectHandlers

protected static final Map<Class<?>,DeepUtil.ObjectHandler> _objectHandlers
Object handlers mapped by class.


ARRAY_OBJECT_HANDLER

protected static final DeepUtil.ObjectHandler ARRAY_OBJECT_HANDLER
Field handler for object arrays.


PRIMITIVE_FIELD_HANDLERS

protected static final Map<Class<?>,DeepUtil.FieldHandler> PRIMITIVE_FIELD_HANDLERS
Handlers for primitive fields mapped by class.


DEFAULT_OBJECT_FIELD_HANDLER

protected static DeepUtil.FieldHandler DEFAULT_OBJECT_FIELD_HANDLER
Default handler for object fields.


DEEP_OBJECT_FIELD_HANDLER

protected static DeepUtil.FieldHandler DEEP_OBJECT_FIELD_HANDLER
Field handler for deep object fields.


SHALLOW_OBJECT_FIELD_HANDLER

protected static DeepUtil.FieldHandler SHALLOW_OBJECT_FIELD_HANDLER
Field handler for shallow object fields.

Constructor Detail

DeepUtil

public DeepUtil()
Method Detail

copy

public static <T> T copy(T source)
Creates and returns a deep copy of an object using reflection.


copy

public static <T> T copy(T source,
                         T dest)
Creates a deep copy of an object using reflection, storing the result in the object provided if possible.

Returns:
the copied object.

copy

public static <T> T copy(T source,
                         T dest,
                         Object outer)
Creates a deep copy of an object using reflection, storing the result in the object provided if possible.

Parameters:
outer - the outer object reference to use for inner object creation, if any.
Returns:
the copied object.

transfer

public static <T> T transfer(T source,
                             T dest)
Transfers the state in the shared ancestry of the two arguments from the source to the destination. If the two objects are instances of the same class, then this is equivalent to copy(T).

Returns:
a reference to the destination object.

transfer

public static <T> T transfer(T source,
                             T dest,
                             Object outer)
Transfers the state in the shared ancestry of the two arguments from the source to the destination. If the two objects are instances of the same class, then this is equivalent to copy(T).

Parameters:
outer - the outer object reference to use for inner object creation, if any.
Returns:
a reference to the destination object.

equals

public static <T> boolean equals(T o1,
                                 T o2)
Compares two objects for deep equality.


hashCode

public static int hashCode(Object object)
Computes the deep hash code of an object.


getObjectHandler

protected static DeepUtil.ObjectHandler getObjectHandler(Class<?> clazz)
Retrieves the handler for the supplied class.


getInstanceFields

protected static void getInstanceFields(Class<?> clazz,
                                        ArrayList<Field> fields)
Populates the supplied list with the copyable/comparable fields of the given class.