-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pipinet
committed
Oct 8, 2024
1 parent
5421c9d
commit c874a9d
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
jakarta-data/src/main/java/com/qwlabs/jakarta/data/PageRequests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.qwlabs.jakarta.data; | ||
|
||
import com.google.common.primitives.Longs; | ||
import jakarta.data.page.PageRequest; | ||
|
||
import java.util.Optional; | ||
|
||
public interface PageRequests { | ||
|
||
static PageRequest of(int first) { | ||
return of(null, first); | ||
} | ||
|
||
static PageRequest of(String after, int first) { | ||
if (first <= 0) { | ||
throw new IllegalArgumentException("first can not be less than 1"); | ||
} | ||
var startAt = Optional.ofNullable(after).map(Longs::tryParse).orElse(0L); | ||
var pageNumber = (startAt + first) / first; | ||
return PageRequest.ofPage(pageNumber, first, true); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jakarta-data/src/test/java/com/qwlabs/jakarta/data/PageRequestsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.qwlabs.jakarta.data; | ||
|
||
import jakarta.data.page.PageRequest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
class PageRequestsTest { | ||
@Test | ||
void should_of() { | ||
checkRequest(null, 2, 1L); | ||
checkRequest("0", 2, 1L); | ||
checkRequest("1", 2, 1L); | ||
checkRequest("1", 3, 1L); | ||
checkRequest("1", 10, 1L); | ||
checkRequest("2", 1, 3L); | ||
checkRequest("2", 2, 2L); | ||
checkRequest("2", 3, 1L); | ||
checkRequest("2", 4, 1L); | ||
} | ||
|
||
void checkRequest(String after, Integer first, long page) { | ||
var pageRequest = PageRequests.of(after, first); | ||
assertThat(pageRequest.page(), is(page)); | ||
assertThat(pageRequest.size(), is(first)); | ||
assertThat(pageRequest.requestTotal(), is(true)); | ||
assertThat(pageRequest.mode(), is(PageRequest.Mode.OFFSET)); | ||
} | ||
} |