Skip to content

Commit

Permalink
chore: add jarkata data
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Oct 8, 2024
1 parent 5421c9d commit c874a9d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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);
}
}
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));
}
}

0 comments on commit c874a9d

Please sign in to comment.