return builder.add(beanName, null, valueB, ComparisonResults.VALUE_A_IS_NULL); return builder.add(beanName, valueA, null, ComparisonResults.VALUE_B_IS_NULL); return builder.add(beanName, valueA.getClass(), valueB.getClass(), ComparisonResults.CLASSES_DIFFERENT); return (equal) ? builder : builder.add(beanName, valueA, valueB, ComparisonResults.NOT_EQUAL);
private static ComparisonResults.Builder compareMaps(ComparisonResults.Builder builder, Map<?,?> mapA, Map<?,?> mapB, String beanName) { int countA = mapA.size(); int countB = mapB.size(); if (countA != countB) { builder.add(beanName+":count()", countA, countB, ComparisonResults.SIZE); } Collection<?> keysA = mapA.keySet(); Collection<?> keysB = mapB.keySet(); List<?> intersection = BeanUtils.intersection(keysA, keysB); for (Object key : intersection) { Object valueA = mapA.get(key); Object valueB = mapB.get(key); compare(builder, null, valueA, valueB, beanName+"["+key+"]"); } for (Object key : mapA.keySet()) { if (intersection.contains(key) == false) { builder.add(beanName+"["+key+"]", mapA.get(key), null, ComparisonResults.MISSING_B); } } for (Object key : mapB.keySet()) { if (intersection.contains(key) == false) { builder.add(beanName+"["+key+"]", null, mapA.get(key), ComparisonResults.MISSING_A); } } return builder; }
private static ComparisonResults.Builder compareCollection(ComparisonResults.Builder builder, Collection<?> collectionA, Collection<?> collectionB, String beanName) { List<?> listA = new ArrayList<>(collectionA); List<?> listB = new ArrayList<>(collectionB); int countA = listA.size(); int countB = listB.size(); if (countA != countB) { builder.add(beanName + ":count()", countA, countB, ComparisonResults.SIZE); } int max = Math.min(countA, countB); for (int i = 0; i < max; i++) { Object valueA = listA.get(i); Object valueB = listB.get(i); compare(builder, null, valueA, valueB, beanName + "[" + i + "]"); } for (int i = max; i < listA.size(); i++) { builder.add(beanName+"["+i+"]", listA.get(i), null, ComparisonResults.MISSING_B); } for (int i = max; i < listB.size(); i++) { builder.add(beanName+"["+i+"]", null, listB.get(i), ComparisonResults.MISSING_A); } return builder; }
public static Builder builder() { return new Builder(); }
public static ComparisonResults compare(Object objectA, Object objectB) { ComparisonResults.Builder builder = ComparisonResults.builder(); return compare(builder, null, objectA, objectB, "root").build(); }