PropertyComparator implements Comparator<Object> { @Override public int compare(Object first, Object second) { final Object firstValue = safeReadPropertyValue(first); final Object secondValue = safeReadPropertyValue(second); int comparison; if (firstValue == null || secondValue == null) { comparison = compareIfEitherIsNull(firstValue, secondValue); } else { comparison = compareIfCompatible(firstValue, secondValue); } return comparison * (SortDirection.DESCENDING.equals(direction) ? -1 : 1); } PropertyComparator(Order order); @Override int compare(Object first, Object second); static void sort(List<?> collection, Sort sort);  }
@Test(expectedExceptions = InvalidArgumentException.class) public void testWhenFirstValueDoesNotExist() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "address.city", NullHandling.DEFAULT)); comparator.compare(new Address(), new Person()); }
@Test(expectedExceptions = InvalidArgumentException.class) public void testWhenSecondValueDoesNotExist() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "address.city", NullHandling.DEFAULT)); comparator.compare(new Person(), new Address()); }
@Test public void testNullFirst() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "address.city", NullHandling.NULLS_FIRST)); assertThat(comparator.compare(new Person().setAddress(new Address().setCity("A")), new Person()), is(1)); assertThat(comparator.compare(new Person(), new Person().setAddress(new Address().setCity("A"))), is(-1)); }
@Test public void testNullLast() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "address.city", NullHandling.NULLS_LAST)); assertThat(comparator.compare(new Person().setAddress(new Address().setCity("A")), new Person()), is(-1)); assertThat(comparator.compare(new Person(), new Person().setAddress(new Address().setCity("A"))), is(1)); }
@Test public void testBothAreNull() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "address.city", NullHandling.NULLS_LAST)); assertThat(comparator.compare(new Person(), new Person()), is(0)); }
@Test(expectedExceptions = InvalidArgumentException.class) public void testWhenValuesAreNotComparable() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "address", NullHandling.NULLS_LAST)); comparator.compare(new Person().setAddress(new Address()), new Person().setAddress(new Address())); }
@Test public void testWhenSecondIsSubTypeOfFirst() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "addressZip", NullHandling.NULLS_FIRST)); final int comparison = comparator.compare(new Person().setAddressZip(new Zip().setPrefix("a")), new Person().setAddressZip(new ChildZip().setPrefix("b"))); assertThat(comparison, is(-1)); }
@Test public void testWhenFirstIsSubTypeOfSecond() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "addressZip", NullHandling.NULLS_FIRST)); final int comparison = comparator.compare(new Person().setAddressZip(new ChildZip().setPrefix("b")), new Person().setAddressZip(new Zip().setPrefix("a"))); assertThat(comparison, is(1)); }
@Test(expectedExceptions = InvalidArgumentException.class) public void testWhenValuesAreNotOfTheSameType() { final PropertyComparator comparator = new PropertyComparator( new ImmutableOrder(SortDirection.ASCENDING, "addressZip", NullHandling.NULLS_FIRST)); comparator.compare(new Person().setAddressZip(new ChildZip().setPrefix("b")), new Person().setAddressZip(new OtherChildZip().setPrefix("a"))); }