org.unitils.reflectionassert.comparator.impl
Class LenientNumberComparator

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

public class LenientNumberComparator
extends Object
implements Comparator

A comparator that compares primitive types by value and not by type. You can for example compare a double with an integer value.

Author:
Tim Ducheyne, Filip Neven

Constructor Summary
LenientNumberComparator()
           
 
Method Summary
 boolean canCompare(Object left, Object right)
          Returns true if both objects are not null and instances of Number or Character.
 Difference compare(Object left, Object right, boolean onlyFirstDifference, ReflectionComparator reflectionComparator)
          Compares the two values by converting them to a double and comparing these double values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LenientNumberComparator

public LenientNumberComparator()
Method Detail

canCompare

public boolean canCompare(Object left,
                          Object right)
Returns true if both objects are not null and instances of Number or Character.

Specified by:
canCompare in interface Comparator
Parameters:
left - The left object
right - The right object
Returns:
True for Numbers and Charaters

compare

public Difference compare(Object left,
                          Object right,
                          boolean onlyFirstDifference,
                          ReflectionComparator reflectionComparator)
Compares the two values by converting them to a double and comparing these double values.

Specified by:
compare in interface Comparator
Parameters:
left - The left Number or Character, not null
right - The right Number or Character, not null
onlyFirstDifference - True if only the first difference should be returned
reflectionComparator - The root comparator for inner comparisons, not null
Returns:
A difference if the values are different, null otherwise


Copyright © 2011. All Rights Reserved.