org.unitils.reflectionassert.comparator.impl
Class LenientOrderCollectionComparator

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

public class LenientOrderCollectionComparator
extends Object
implements Comparator

A comparator for collections and arrays that ignores the order of both collections. Both collections are found equal if they both contain the same elements (in any order). This implements the LENIENT_ORDER comparison mode.

Author:
Tim Ducheyne, Filip Neven

Constructor Summary
LenientOrderCollectionComparator()
           
 
Method Summary
 boolean canCompare(Object left, Object right)
          Returns true if both objects are not null and are both Arrays or Collections.
 Difference compare(Object left, Object right, boolean onlyFirstDifference, ReflectionComparator reflectionComparator)
          Compares the given collections/arrays but ignoring the actual order of the elements.
protected  ArrayList<Integer> createIndexList(int size)
           
protected  MatchingScoreCalculator createMatchingScoreCalculator()
          Creates the calculator for determining the matching scores of the differences.
protected  void fillAllDifferences(ArrayList<Object> leftList, ArrayList<Object> rightList, ReflectionComparator reflectionComparator, UnorderedCollectionDifference difference)
          Calculates the difference of all elements in the left list with all elements of the right list.
protected  void fillBestMatchingIndexes(ArrayList<Object> leftList, ArrayList<Object> rightList, UnorderedCollectionDifference difference)
          Calculates the indexes of the best matching differences for the given unordered collection difference.
protected  boolean isEqual(ArrayList<Object> leftList, ArrayList<Object> rightList, int leftIndex, ReflectionComparator reflectionComparator)
          Recursively checks whether there is a sequence so that both collections have matching elements.
protected  void removeMatchingIndexes(ArrayList<Integer> leftIndexes, ArrayList<Integer> rightIndexes, UnorderedCollectionDifference difference)
          Removes all left and right indexes for which there is a match in the given difference object.
protected  void setBestMatchingIndexes(ArrayList<Integer> leftIndexes, ArrayList<Integer> rightIndexes, UnorderedCollectionDifference difference)
          Actual implementation of the best match finding algorithm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LenientOrderCollectionComparator

public LenientOrderCollectionComparator()
Method Detail

canCompare

public boolean canCompare(Object left,
                          Object right)
Returns true if both objects are not null and are both Arrays or Collections.

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

compare

public Difference compare(Object left,
                          Object right,
                          boolean onlyFirstDifference,
                          ReflectionComparator reflectionComparator)
Compares the given collections/arrays but ignoring the actual order of the elements. This will first try to find a sequence that is an exact match. If no such sequence can be found, the difference of all elements with all other elements are calculated one by one.

Specified by:
compare in interface Comparator
Parameters:
left - The left array/collection, not null
right - The right array/collection, not null
onlyFirstDifference - True if only the first difference should be returned
reflectionComparator - The root comparator for inner comparisons, not null
Returns:
An UnorderedCollectionDifference or null if both collections are equal

isEqual

protected boolean isEqual(ArrayList<Object> leftList,
                          ArrayList<Object> rightList,
                          int leftIndex,
                          ReflectionComparator reflectionComparator)
Recursively checks whether there is a sequence so that both collections have matching elements. This will loop over the elements of the left list and then try to find a match for these elements in the right list. If a match is found, the element is removed from the right collection and the comparison is recursively performed again on the remaining elements.

NOTE: because difference are cached in the reflection comparator, comparing two elements that were already compared should be very fast.

Parameters:
leftList - The left list, not null
rightList - The right list, not null
leftIndex - The current index in the left collection
reflectionComparator - reflectionComparator The comparator for the element comparisons, not null
Returns:
True if a match is found

fillAllDifferences

protected void fillAllDifferences(ArrayList<Object> leftList,
                                  ArrayList<Object> rightList,
                                  ReflectionComparator reflectionComparator,
                                  UnorderedCollectionDifference difference)
Calculates the difference of all elements in the left list with all elements of the right list. The result is added to the given difference.

NOTE: because difference are cached in the reflection comparator, comparing two elements that were already compared should be very fast.

Parameters:
leftList - The left list, not null
rightList - The right list, not null
reflectionComparator - The comparator for element comparisons, not null
difference - The root difference to which all differences will be added, not null

fillBestMatchingIndexes

protected void fillBestMatchingIndexes(ArrayList<Object> leftList,
                                       ArrayList<Object> rightList,
                                       UnorderedCollectionDifference difference)
Calculates the indexes of the best matching differences for the given unordered collection difference. The resulting indexes are set on the given difference.

Note: The unordered collection difference should contain the differences of all left-elements with all right-elements.

Parameters:
leftList - The left list, not null
rightList - The right list, not null
difference - The difference to which all indexes will be added, not null

setBestMatchingIndexes

protected void setBestMatchingIndexes(ArrayList<Integer> leftIndexes,
                                      ArrayList<Integer> rightIndexes,
                                      UnorderedCollectionDifference difference)
Actual implementation of the best match finding algorithm. This will loop over the different elements in the collections to the match with the lowest total matching score. These indexes are then set on the given difference. The matching scores are determined by the given matchingScoreCalculator.

Parameters:
leftIndexes - The current remaining indexes in the left collection, not null
rightIndexes - The current remaining indexes in the right collection, not null
difference - The difference to which all indexes will be added, not null

removeMatchingIndexes

protected void removeMatchingIndexes(ArrayList<Integer> leftIndexes,
                                     ArrayList<Integer> rightIndexes,
                                     UnorderedCollectionDifference difference)
Removes all left and right indexes for which there is a match in the given difference object.

Parameters:
leftIndexes - The indexes, not null
rightIndexes - The indexes, not null
difference - The collection difference, not null

createIndexList

protected ArrayList<Integer> createIndexList(int size)
Parameters:
size - The nr of elements
Returns:
A list containing 0, 1, 2, ..., not null

createMatchingScoreCalculator

protected MatchingScoreCalculator createMatchingScoreCalculator()
Creates the calculator for determining the matching scores of the differences. These scores are used to determine the best matching elements.

Returns:
The instance, not null


Copyright © 2011. All Rights Reserved.