ReflectionComparator reflectionComparator) {
List leftList = ListHelper.toList(left);
List rightList = ListHelper.toList(right);
int elementIndex = -1;
CollectionDifference difference = new CollectionDifference("Different elements", left, right, leftList,
rightList);
Iterator<?> leftIterator = leftList.iterator();
Iterator<?> rightIterator = rightList.iterator();
while (leftIterator.hasNext() && rightIterator.hasNext()) {
elementIndex++;
Difference elementDifference = reflectionComparator.getDifference(leftIterator.next(),
rightIterator.next(), onlyFirstDifference);
if (elementDifference != null) {
difference.addElementDifference(elementIndex, elementDifference);
if (onlyFirstDifference) {
return difference;
}
}
}
// check for missing elements
int leftElementIndex = elementIndex;
while (leftIterator.hasNext()) {
leftIterator.next();
difference.addLeftMissingIndex(++leftElementIndex);
}
int rightElementIndex = elementIndex;
while (rightIterator.hasNext()) {
rightIterator.next();
difference.addRightMissingIndex(++rightElementIndex);
}
if (difference.getElementDifferences().isEmpty() && difference.getLeftMissingIndexes().isEmpty()
&& difference.getRightMissingIndexes().isEmpty()) {
return null;
}
return difference;
}