com.neeve.util
Class UtlObjectGraph

java.lang.Object
  extended by com.neeve.util.UtlObjectGraph

public class UtlObjectGraph
extends Object

Convenience class for working with object graphs.

This class IS NOT intended to be used by end users.


Nested Class Summary
static class UtlObjectGraph.BuiltInHandler
          The default handler for object population.
static interface UtlObjectGraph.ComparatorFactory
          Used to return a custom comparator for a type rather than the default comparator
static interface UtlObjectGraph.FieldAccessorVisitor
          Visitor for field introspection.
static interface UtlObjectGraph.PopulationHelper
          A helper interface to assist in object population.
 
Constructor Summary
UtlObjectGraph()
           
 
Method Summary
static
<T> Object
createRandomArray(Class<T> clazz, int maxLength, Random random)
           
static String createRandomString(int length, Random random)
          Creates a random string of the given length.
static
<T> boolean
deepApiEquals(T o1, T o2, StringBuffer diffSummary, Collection<Class<?>> consideredClasses)
          Same as deepApiEquals(o1, o1, buffer, consideredClasses, null).
static
<T> boolean
deepApiEquals(T o1, T o2, StringBuffer diffSummary, Collection<Class<?>> consideredClasses, UtlObjectGraph.ComparatorFactory comparatorFactory)
          Tests if two objects are equal from an api perspective.
static
<T> boolean
deepApiEquals(T o1, T o2, StringBuffer diffSummary, UtlTableFormatter.Format format, Collection<Class<?>> consideredClasses, UtlObjectGraph.ComparatorFactory comparatorFactory)
          Tests if two objects are equal from an api perspective.
static
<T> boolean
deepEquals(T o1, T o2)
           
static
<T> String
diffObjects(T o1, T o2)
          Does a deep introspective comparison of the two objects fields returning a summary of how they differ.
static boolean equals(Object o1, Object o2)
           
static String getLastGetter(Stack<String> stack)
           
static boolean isVerbose()
           
static void populateObject(Object o)
          Populates an object graph with Random data.
static void populateObject(Object o, Class<?>... consideredClasses)
          Populates an object graph with Random data.
static void populateObject(Object o, Collection<Class<?>> consideredClasses)
          Populates an object graph with Random data.
static void populateObject(Object o, Collection<Class<?>> consideredClasses, UtlObjectGraph.PopulationHelper helper)
          Populates an object graph with Random data.
static void populateObject(Object o, long seed, Collection<Class<?>> consideredClasses, UtlObjectGraph.PopulationHelper helper)
          Randomly populates an object with values.
static void setVerbose(boolean verbose)
           
static String unwindStack(Stack<String> stack)
           
static void visitBeanFields(Class<?> type, UtlObjectGraph.FieldAccessorVisitor visitor)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UtlObjectGraph

public UtlObjectGraph()
Method Detail

isVerbose

public static boolean isVerbose()

setVerbose

public static void setVerbose(boolean verbose)

populateObject

public static final void populateObject(Object o)
                                 throws IllegalArgumentException,
                                        IllegalAccessException,
                                        InvocationTargetException
Populates an object graph with Random data. Identical to populateObject(Object, long, Collection, PopulationHelper) called with System.currentTimeMillis() as the seed, all classes considered and no helper class provided

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException
See Also:
populateObject(Object, long, Collection, PopulationHelper)

populateObject

public static final void populateObject(Object o,
                                        Class<?>... consideredClasses)
                                 throws IllegalArgumentException,
                                        IllegalAccessException,
                                        InvocationTargetException
Populates an object graph with Random data. Identical to populateObject(Object, long, Collection, PopulationHelper) called with System.currentTimeMillis() as the seed, and no helper class provided

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException
See Also:
populateObject(Object, long, Collection, PopulationHelper)

populateObject

public static final void populateObject(Object o,
                                        Collection<Class<?>> consideredClasses)
                                 throws IllegalArgumentException,
                                        IllegalAccessException,
                                        InvocationTargetException
Populates an object graph with Random data. Identical to populateObject(Object, long, Collection, PopulationHelper) called with System.currentTimeMillis() as the seed, and no helper class provided

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException
See Also:
populateObject(Object, long, Collection, PopulationHelper)

populateObject

public static final void populateObject(Object o,
                                        Collection<Class<?>> consideredClasses,
                                        UtlObjectGraph.PopulationHelper helper)
                                 throws IllegalArgumentException,
                                        IllegalAccessException,
                                        InvocationTargetException
Populates an object graph with Random data. Identical to populateObject(Object, long, Collection, PopulationHelper) called with System.currentTimeMillis() as the seed.

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException
See Also:
populateObject(Object, long, Collection, PopulationHelper)

populateObject

public static final void populateObject(Object o,
                                        long seed,
                                        Collection<Class<?>> consideredClasses,
                                        UtlObjectGraph.PopulationHelper helper)
                                 throws IllegalArgumentException,
                                        IllegalAccessException,
                                        InvocationTargetException
Randomly populates an object with values.

Parameters:
o - The object to populates
seed - The seed value to use with the random generator. Useful for populating two object graphs with the same data
consideredClasses - The collection of interfaces and classes whose setter/getter methods will be introspected.
Throws:
InvocationTargetException - If there is an error reflectively setting a field.
IllegalAccessException - If a setter cannot be accessed.
IllegalArgumentException - If an invalid argument is passed to a setter.

createRandomArray

public static final <T> Object createRandomArray(Class<T> clazz,
                                                 int maxLength,
                                                 Random random)

diffObjects

public static <T> String diffObjects(T o1,
                                     T o2)
Does a deep introspective comparison of the two objects fields returning a summary of how they differ.

Type Parameters:
T - The type of object to compare.
Parameters:
o1 - First object to compare
o2 - Second object to compare
Returns:
A String describing any differences (empty if no differences)

deepApiEquals

public static <T> boolean deepApiEquals(T o1,
                                        T o2,
                                        StringBuffer diffSummary,
                                        Collection<Class<?>> consideredClasses)
                             throws Exception
Same as deepApiEquals(o1, o1, buffer, consideredClasses, null).

Parameters:
o1 - The first object
o2 - The second object
diffSummary - The summary for diffs or null for no summary
consideredClasses - The apis to consider in comparison
Returns:
True if the objecst are equal.
Throws:
Exception - If there is an error checking equality.

deepApiEquals

public static <T> boolean deepApiEquals(T o1,
                                        T o2,
                                        StringBuffer diffSummary,
                                        Collection<Class<?>> consideredClasses,
                                        UtlObjectGraph.ComparatorFactory comparatorFactory)
                             throws Exception
Tests if two objects are equal from an api perspective. Rather than introspecting the two objects' fields to walk the object graph, the getter apis supplied by the considered interfaces parameter is used to determine the children. If the returned type of those methods is an primitive or enum, then normal object comparison is used. If the value is an array then the arrays contents will be compared. If the value is Collection then the collection contents will be compared only if the contents implement one of the considered interfaces and similarly if it is a map comparison will only be done if the values implement one of the considered interfaces.

Parameters:
o1 - The first object
o2 - The second object
diffSummary - If provided a summary of the differences will be generated.
consideredClasses - The apis to consider in comparison
Returns:
True if the objecst are equal.
Throws:
Exception - If there is an error checking equality.

deepApiEquals

public static <T> boolean deepApiEquals(T o1,
                                        T o2,
                                        StringBuffer diffSummary,
                                        UtlTableFormatter.Format format,
                                        Collection<Class<?>> consideredClasses,
                                        UtlObjectGraph.ComparatorFactory comparatorFactory)
                             throws Exception
Tests if two objects are equal from an api perspective. Rather than introspecting the two objects' fields to walk the object graph, the getter apis supplied by the considered interfaces parameter is used to determine the children. If the returned type of those methods is an primitive or enum, then normal object comparison is used. If the value is an array then the arrays contents will be compared. If the value is Collection then the collection contents will be compared only if the contents implement one of the considered interfaces and similarly if it is a map comparison will only be done if the values implement one of the considered interfaces.

Parameters:
o1 - The first object
o2 - The second object
diffSummary - If provided a summary of the differences will be generated.
format - if a diffSummary is specified the format to dump the summary in.
consideredClasses - The apis to consider in comparison
Returns:
True if the objects are equal.
Throws:
Exception - If there is an error checking equality.

unwindStack

public static String unwindStack(Stack<String> stack)

deepEquals

public static <T> boolean deepEquals(T o1,
                                     T o2)

equals

public static boolean equals(Object o1,
                             Object o2)

getLastGetter

public static String getLastGetter(Stack<String> stack)

createRandomString

public static String createRandomString(int length,
                                        Random random)
Creates a random string of the given length.

Parameters:
length - The max length of the string to create.
random - The seed
Returns:
A String with random content

visitBeanFields

public static final void visitBeanFields(Class<?> type,
                                         UtlObjectGraph.FieldAccessorVisitor visitor)


Copyright © 2016 Neeve Research, LLC. All Rights Reserved.