@Test public void testCompareByProperty() throws Exception { TestObjectComparator objectComparator = new TestObjectComparator(); Assert.assertEquals("Identity test, toString, asc", 0, objectComparator.compareByProperty(testObject1, testObject1, Object::toString, String::compareToIgnoreCase, false) ); Assert.assertEquals("Identity test, toString, desc", 0, objectComparator.compareByProperty(testObject1, testObject1, Object::toString, String::compareToIgnoreCase, true) ); Assert.assertEquals("1 & 2 test, toString, asc", 0, objectComparator.compareByProperty(testObject1, testObject2, Object::toString, String::compareToIgnoreCase, false) ); Assert.assertEquals("1 & 2 test, toString, desc", 0, objectComparator.compareByProperty(testObject1, testObject2, Object::toString, String::compareToIgnoreCase, true) ); Assert.assertEquals("1 & 2 test, hash, asc", -1, objectComparator.compareByProperty(testObject1, testObject2, Object::hashCode, Integer::compareTo, false) ); Assert.assertEquals("1 & 2 test, hash, desc", 1, objectComparator.compareByProperty(testObject1, testObject2, Object::hashCode, Integer::compareTo, true) ); Assert.assertEquals("1 & 2 test, npe left, asc", 1, objectComparator.compareByProperty(testObject1, testObject2, TestObjectComparator::nullPointerLambda1234, String::compareToIgnoreCase, false) ); Assert.assertEquals("1 & 2 test, npe left, desc", 1, objectComparator.compareByProperty(testObject1, testObject2, TestObjectComparator::nullPointerLambda1234, String::compareToIgnoreCase, true) ); Assert.assertEquals("1 & 2 test, npe right, asc", -1, objectComparator.compareByProperty(testObject1, testObject2, TestObjectComparator::nullPointerLambda2345, String::compareToIgnoreCase, false) ); Assert.assertEquals("1 & 2 test, npe right, desc", -1, objectComparator.compareByProperty(testObject1, testObject2, TestObjectComparator::nullPointerLambda2345, String::compareToIgnoreCase, true) ); }
<V> int compareByProperty(T left, T right, Function<T, V> propertyExtractor, BiFunction<V, V, Integer> comparisonFunc, boolean descending) { V property1 = null; V property2 = null; try { property1 = propertyExtractor.apply(left); } catch (NullPointerException ignored) { } try { property2 = propertyExtractor.apply(right); } catch (NullPointerException ignored) { } int result; if ((result = compareNull(property1, property2, descending)) == 2) { result = comparisonFunc.apply(property1, property2); } return result * (descending ? -1 : 1); }
PaginationComparator implements Comparator<T> { <V> int compareByProperty(T left, T right, Function<T, V> propertyExtractor, BiFunction<V, V, Integer> comparisonFunc, boolean descending) { V property1 = null; V property2 = null; try { property1 = propertyExtractor.apply(left); } catch (NullPointerException ignored) { } try { property2 = propertyExtractor.apply(right); } catch (NullPointerException ignored) { } int result; if ((result = compareNull(property1, property2, descending)) == 2) { result = comparisonFunc.apply(property1, property2); } return result * (descending ? -1 : 1); } }
PaginationComparator implements Comparator<T> { <V> int compareByProperty(T left, T right, Function<T, V> propertyExtractor, BiFunction<V, V, Integer> comparisonFunc, boolean descending) { V property1 = null; V property2 = null; try { property1 = propertyExtractor.apply(left); } catch (NullPointerException ignored) { } try { property2 = propertyExtractor.apply(right); } catch (NullPointerException ignored) { } int result; if ((result = compareNull(property1, property2, descending)) == 2) { result = comparisonFunc.apply(property1, property2); } return result * (descending ? -1 : 1); } PaginationComparator(); protected  PaginationComparator(String defaultSortOrder); }
PaginationComparator implements Comparator<T> { <V> int compareByProperty(T left, T right, Function<T, V> propertyExtractor, BiFunction<V, V, Integer> comparisonFunc, boolean descending) { V property1 = null; V property2 = null; try { property1 = propertyExtractor.apply(left); } catch (NullPointerException ignored) { } try { property2 = propertyExtractor.apply(right); } catch (NullPointerException ignored) { } int result; if ((result = compareNull(property1, property2, descending)) == 2) { result = comparisonFunc.apply(property1, property2); } return result * (descending ? -1 : 1); } PaginationComparator(); protected  PaginationComparator(String defaultSortOrder); PaginationComparator<T> replaceSortorder(String sortOrder); }
PaginationComparator implements Comparator<T> { <V> int compareByProperty(T left, T right, Function<T, V> propertyExtractor, BiFunction<V, V, Integer> comparisonFunc, boolean descending) { V property1 = null; V property2 = null; try { property1 = propertyExtractor.apply(left); } catch (NullPointerException ignored) { } try { property2 = propertyExtractor.apply(right); } catch (NullPointerException ignored) { } int result; if ((result = compareNull(property1, property2, descending)) == 2) { result = comparisonFunc.apply(property1, property2); } return result * (descending ? -1 : 1); } PaginationComparator(); protected  PaginationComparator(String defaultSortOrder); PaginationComparator<T> replaceSortorder(String sortOrder);  }