@Test public void testArrayComparator() { List<Comparator> comparators = new ArrayList<Comparator>(); comparators.add(new SimpleComparator()); comparators.add(new ArrayComparator()); comparators.add(new ObjectComparator()); IntegratedComparator comparator = new IntegratedComparator(comparators); String[] left = new String[]{"a", "b", "c", "d", "e", "f", "g"}; String[] right = new String[]{"A", "b", "c", "D", "e", "f", "G"}; CompareResult result = comparator.compare(left, right); Assert.assertNotEquals(result, null); Assert.assertTrue(result.hasDifference()); Assert.assertEquals(result.getDifferences().size(), 3); Student[] leftStudents = new Student[]{Student.builder() .name("java") .school("w3c") .age(10) .build()}; Student[] rightStudents = new Student[]{ Student.builder() .name("java") .school("w3c") .age(12) .build(), Student.builder() .name("java") .school("w3c") .age(12) .build() }; result = comparator.compare(leftStudents, rightStudents); Assert.assertNotEquals(result, null); Assert.assertTrue(result.hasDifference()); Assert.assertEquals(result.getDifferences().size(), 2); int[] leftInt = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] rightInt = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; result = comparator.compare(leftInt, rightInt); Assert.assertNotEquals(result, null); Assert.assertTrue(result.hasDifference()); Assert.assertEquals(result.getDifferences().size(), 1); }
@Override public CompareResult compare(Object left, Object right) { tryClear(); List<Path> paths = new ArrayList<Path>(0); try { dispatch(left, right, paths); } catch (Exception e) { addDifference(left, right, Difference.Type.COMPARE_ERR, paths); } return new CompareResult(left, right, differences); }
IntegratedComparator implements Comparable { @Override public CompareResult compare(Object left, Object right) { tryClear(); List<Path> paths = new ArrayList<Path>(0); try { dispatch(left, right, paths); } catch (Exception e) { addDifference(left, right, Difference.Type.COMPARE_ERR, paths); } return new CompareResult(left, right, differences); } }
IntegratedComparator implements Comparable { @Override public CompareResult compare(Object left, Object right) { tryClear(); List<Path> paths = new ArrayList<Path>(0); try { dispatch(left, right, paths); } catch (Exception e) { addDifference(left, right, Difference.Type.COMPARE_ERR, paths); } return new CompareResult(left, right, differences); } IntegratedComparator(List<Comparator> comparators); }
IntegratedComparator implements Comparable { @Override public CompareResult compare(Object left, Object right) { tryClear(); List<Path> paths = new ArrayList<Path>(0); try { dispatch(left, right, paths); } catch (Exception e) { addDifference(left, right, Difference.Type.COMPARE_ERR, paths); } return new CompareResult(left, right, differences); } IntegratedComparator(List<Comparator> comparators); void dispatch(Object left, Object right, List<Path> paths); @Override CompareResult compare(Object left, Object right); void addDifference(Object left, Object right, Difference.Type type, List<Path> paths); List<Path> declarePath(List<Path> paths, int index); List<Path> declarePath(List<Path> paths, String key); }
IntegratedComparator implements Comparable { @Override public CompareResult compare(Object left, Object right) { tryClear(); List<Path> paths = new ArrayList<Path>(0); try { dispatch(left, right, paths); } catch (Exception e) { addDifference(left, right, Difference.Type.COMPARE_ERR, paths); } return new CompareResult(left, right, differences); } IntegratedComparator(List<Comparator> comparators); void dispatch(Object left, Object right, List<Path> paths); @Override CompareResult compare(Object left, Object right); void addDifference(Object left, Object right, Difference.Type type, List<Path> paths); List<Path> declarePath(List<Path> paths, int index); List<Path> declarePath(List<Path> paths, String key);  }