Skip to content

Commit

Permalink
feature: add PageRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Nov 1, 2024
1 parent a739475 commit 744e9fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ static PageRequest of(@Nullable String after, int first) {
var pageNumber = (startAt + first) / first;
return PageRequest.ofPage(pageNumber, first, true);
}

static int firstResult(PageRequest request){
return Long.valueOf((request.page() - 1) * request.size()).intValue();
}

static int maxResults(PageRequest request){
return request.size();
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ void should_of() {
checkRequest("2", 4, 1L);
}

@Test
void should_firstResult() {
assertThat(PageRequests.firstResult(PageRequests.of("0", 3)), is(0));
assertThat(PageRequests.firstResult(PageRequests.of("0", 1)), is(0));
assertThat(PageRequests.firstResult(PageRequests.of("1", 2)), is(0));
assertThat(PageRequests.firstResult(PageRequests.of("2", 2)), is(2));
assertThat(PageRequests.firstResult(PageRequests.of("3", 2)), is(2));
assertThat(PageRequests.firstResult(PageRequests.of("4", 2)), is(4));
}

@Test
void should_maxResults() {
assertThat(PageRequests.maxResults(PageRequests.of("0", 3)), is(3));
assertThat(PageRequests.maxResults(PageRequests.of("0", 1)), is(1));
assertThat(PageRequests.maxResults(PageRequests.of("1", 2)), is(2));
assertThat(PageRequests.maxResults(PageRequests.of("2", 2)), is(2));
assertThat(PageRequests.maxResults(PageRequests.of("3", 2)), is(2));
assertThat(PageRequests.maxResults(PageRequests.of("4", 2)), is(2));
}

void checkRequest(String after, Integer first, long page) {
var pageRequest = PageRequests.of(after, first);
assertThat(pageRequest.page(), is(page));
Expand Down

0 comments on commit 744e9fd

Please sign in to comment.