PaginationEvaluator { public PageResult<T> takePage(Page page, List<T> items) { if (page.getPageSize() < 0) { throw PaginationException.badPageSize(page.getPageSize()); } if (page.getPageSize() == 0) { return takeEmptyPage(page, items); } List<T> itemsCopy = new ArrayList<>(items); itemsCopy.sort(dataComparator); if (StringExt.isEmpty(page.getCursor())) { return takePageWithoutCursor(page, itemsCopy); } Pair<String, Long> decodedCursor = decode(page.getCursor()); return takePageWithCursor(page, itemsCopy, decodedCursor.getLeft(), decodedCursor.getRight()); } PaginationEvaluator(Function<T, String> idExtractor, Function<T, Long> timestampExtractor); PageResult<T> takePage(Page page, List<T> items); static Builder<T> newBuilder();  }
@Test public void testEmpty() { PageResult<PaginableItem> pageResult = PAGINATION_EVALUATOR.takePage(FIRST_PAGE, Collections.emptyList()); assertThat(pageResult.getItems()).isEmpty(); assertThat(pageResult.getPagination().hasMore()).isFalse(); }
@Test public void testPaginationWithPageSizeZero() { PageResult<PaginableItem> pageResult = PAGINATION_EVALUATOR.takePage(Page.empty(), PaginableItems.items(2)); assertThat(pageResult.getPagination().getTotalItems()).isEqualTo(2); assertThat(pageResult.getItems()).isEmpty(); }
@Test(expected = PaginationException.class) public void testPaginationWithBadPageSize() { PAGINATION_EVALUATOR.takePage(FIRST_PAGE.toBuilder().withPageSize(-1).build(), PaginableItems.items(2)); }
@Test(expected = PaginationException.class) public void testPaginationWithBadCursor() { PAGINATION_EVALUATOR.takePage(FIRST_PAGE.toBuilder().withCursor("badCursor").build(), PaginableItems.items(2)); }