org.unitils.reflectionassert.comparator.impl
Class ObjectComparator

java.lang.Object
  extended by org.unitils.reflectionassert.comparator.impl.ObjectComparator
All Implemented Interfaces:
Comparator

public class ObjectComparator
extends Object
implements Comparator

Comparator for objects. This will compare all corresponding field values.

Author:
Tim Ducheyne, Filip Neven

Constructor Summary
ObjectComparator()
           
 
Method Summary
 boolean canCompare(Object left, Object right)
          Returns true if both objects are not null
 Difference compare(Object left, Object right, boolean onlyFirstDifference, ReflectionComparator reflectionComparator)
          Compares the given objects by iterating over the fields and comparing the corresponding values.
protected  void compareFields(Object left, Object right, Class<?> clazz, ObjectDifference difference, boolean onlyFirstDifference, ReflectionComparator reflectionComparator)
          Compares the values of all fields in the given objects by use of reflection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectComparator

public ObjectComparator()
Method Detail

canCompare

public boolean canCompare(Object left,
                          Object right)
Returns true if both objects are not null

Specified by:
canCompare in interface Comparator
Parameters:
left - The left object
right - The right object
Returns:
True if not null

compare

public Difference compare(Object left,
                          Object right,
                          boolean onlyFirstDifference,
                          ReflectionComparator reflectionComparator)
Compares the given objects by iterating over the fields and comparing the corresponding values. If both objects are of a different type, a difference is returned. The fields of the superclasses are also compared. Fields of java.lang classes are ignored. So for example fields of the Object class are not compared

Specified by:
compare in interface Comparator
Parameters:
left - The left object, not null
right - The right object, not null
onlyFirstDifference - True if only the first difference should be returned
reflectionComparator - The root comparator for inner comparisons, not null
Returns:
A ObjectDifference or null if both maps are equal

compareFields

protected void compareFields(Object left,
                             Object right,
                             Class<?> clazz,
                             ObjectDifference difference,
                             boolean onlyFirstDifference,
                             ReflectionComparator reflectionComparator)
Compares the values of all fields in the given objects by use of reflection.

Parameters:
left - the left object for the comparison, not null
right - the right object for the comparison, not null
clazz - the type of the left object, not null
difference - root difference, not null
onlyFirstDifference - True if only the first difference should be returned
reflectionComparator - the reflection comparator, not null


Copyright © 2011. All Rights Reserved.