@Test public void testCompare() throws Exception { TestObjectComparator objectComparator = new TestObjectComparator(); Assert.assertEquals("1 & 2 test, empty sort order", 0, objectComparator.compare(testObject1, testObject2)); Assert.assertEquals("2 & 1 test, empty sort order", 0, objectComparator.compare(testObject2, testObject1)); objectComparator.replaceSortorder("string"); Assert.assertEquals("Identity test, string", 0, objectComparator.compare(testObject1, testObject1)); Assert.assertEquals("1 & 2 test, string", 0, objectComparator.compare(testObject1, testObject2)); Assert.assertEquals("2 & 1 test, string", 0, objectComparator.compare(testObject2, testObject1)); objectComparator.replaceSortorder("hash"); Assert.assertEquals("Identity test, hash", 0, objectComparator.compare(testObject1, testObject1)); Assert.assertEquals("1 & 2 test, hash", -1, objectComparator.compare(testObject1, testObject2)); Assert.assertEquals("2 & 1 test, hash", 1, objectComparator.compare(testObject2, testObject1)); objectComparator.replaceSortorder("-hash"); Assert.assertEquals("Identity test, -hash", 0, objectComparator.compare(testObject1, testObject1)); Assert.assertEquals("1 & 2 test, -hash", 1, objectComparator.compare(testObject1, testObject2)); Assert.assertEquals("2 & 1 test, -hash", -1, objectComparator.compare(testObject2, testObject1)); objectComparator.replaceSortorder("string,hash"); Assert.assertEquals("Identity test, string,hash", 0, objectComparator.compare(testObject1, testObject1)); Assert.assertEquals("1 & 2 test, string,hash", -1, objectComparator.compare(testObject1, testObject2)); Assert.assertEquals("2 & 1 test, string,hash", 1, objectComparator.compare(testObject2, testObject1)); objectComparator.replaceSortorder("string,-hash"); Assert.assertEquals("Identity test, string,-hash", 0, objectComparator.compare(testObject1, testObject1)); Assert.assertEquals("1 & 2 test, string,-hash", 1, objectComparator.compare(testObject1, testObject2)); Assert.assertEquals("2 & 1 test, string,-hash", -1, objectComparator.compare(testObject2, testObject1)); try { objectComparator.replaceSortorder("noSortOrder"); Assert.assertEquals("Identity test, noSortOrder", 0, objectComparator.compare(testObject1, testObject1)); } catch (PaginationException ignored) { } catch (Exception exception) { Assert.fail("Failed due to wrong exception: " + exception.getClass()); } }
protected <V extends Enum<V> & PaginationComparatorProperty> int compare(T left, T right, Class<V> enumType) { for (String sort : sortOrder.toLowerCase().split(",")) { boolean descending = sort.startsWith("-"); String propertyKey = descending ? sort.substring(1) : sort; if (StringUtils.isBlank(propertyKey)) { continue; } V property; try { property = Enum.valueOf(enumType, propertyKey); } catch (IllegalArgumentException illegalArgumentException) { throw new PaginationException(ErrorCode.SORT_KEY_UNPROCESSABLE, "The request can not be sorted by " + propertyKey, illegalArgumentException); } int result = compareByProperty(left, right, property.getPropertyExtractor(), property.getComparisonFunction(), descending); if (result != 0) { return result; } } return 0; }
PaginationComparator implements Comparator<T> { protected <V extends Enum<V> & PaginationComparatorProperty> int compare(T left, T right, Class<V> enumType) { for (String sort : sortOrder.toLowerCase().split(",")) { boolean descending = sort.startsWith("-"); String propertyKey = descending ? sort.substring(1) : sort; if (StringUtils.isBlank(propertyKey)) { continue; } V property; try { property = Enum.valueOf(enumType, propertyKey); } catch (IllegalArgumentException illegalArgumentException) { throw new PaginationException(ErrorCode.SORT_KEY_UNPROCESSABLE, "The request can not be sorted by " + propertyKey, illegalArgumentException); } int result = compareByProperty(left, right, property.getPropertyExtractor(), property.getComparisonFunction(), descending); if (result != 0) { return result; } } return 0; } }
PaginationComparator implements Comparator<T> { protected <V extends Enum<V> & PaginationComparatorProperty> int compare(T left, T right, Class<V> enumType) { for (String sort : sortOrder.toLowerCase().split(",")) { boolean descending = sort.startsWith("-"); String propertyKey = descending ? sort.substring(1) : sort; if (StringUtils.isBlank(propertyKey)) { continue; } V property; try { property = Enum.valueOf(enumType, propertyKey); } catch (IllegalArgumentException illegalArgumentException) { throw new PaginationException(ErrorCode.SORT_KEY_UNPROCESSABLE, "The request can not be sorted by " + propertyKey, illegalArgumentException); } int result = compareByProperty(left, right, property.getPropertyExtractor(), property.getComparisonFunction(), descending); if (result != 0) { return result; } } return 0; } PaginationComparator(); protected  PaginationComparator(String defaultSortOrder); }
PaginationComparator implements Comparator<T> { protected <V extends Enum<V> & PaginationComparatorProperty> int compare(T left, T right, Class<V> enumType) { for (String sort : sortOrder.toLowerCase().split(",")) { boolean descending = sort.startsWith("-"); String propertyKey = descending ? sort.substring(1) : sort; if (StringUtils.isBlank(propertyKey)) { continue; } V property; try { property = Enum.valueOf(enumType, propertyKey); } catch (IllegalArgumentException illegalArgumentException) { throw new PaginationException(ErrorCode.SORT_KEY_UNPROCESSABLE, "The request can not be sorted by " + propertyKey, illegalArgumentException); } int result = compareByProperty(left, right, property.getPropertyExtractor(), property.getComparisonFunction(), descending); if (result != 0) { return result; } } return 0; } PaginationComparator(); protected  PaginationComparator(String defaultSortOrder); PaginationComparator<T> replaceSortorder(String sortOrder); }
PaginationComparator implements Comparator<T> { protected <V extends Enum<V> & PaginationComparatorProperty> int compare(T left, T right, Class<V> enumType) { for (String sort : sortOrder.toLowerCase().split(",")) { boolean descending = sort.startsWith("-"); String propertyKey = descending ? sort.substring(1) : sort; if (StringUtils.isBlank(propertyKey)) { continue; } V property; try { property = Enum.valueOf(enumType, propertyKey); } catch (IllegalArgumentException illegalArgumentException) { throw new PaginationException(ErrorCode.SORT_KEY_UNPROCESSABLE, "The request can not be sorted by " + propertyKey, illegalArgumentException); } int result = compareByProperty(left, right, property.getPropertyExtractor(), property.getComparisonFunction(), descending); if (result != 0) { return result; } } return 0; } PaginationComparator(); protected  PaginationComparator(String defaultSortOrder); PaginationComparator<T> replaceSortorder(String sortOrder);  }