generator.server.pagination.jpa.main.AppPages.mustache Maven / Gradle / Ivy
package {{packageName}}.shared.pagination.infrastructure.secondary;
import java.util.function.Function;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import {{packageName}}.shared.error.domain.Assert;
import {{packageName}}.shared.pagination.domain.{{baseName}}Page;
import {{packageName}}.shared.pagination.domain.{{baseName}}Pageable;
public final class {{baseName}}Pages {
private {{baseName}}Pages() {}
public static Pageable from({{baseName}}Pageable pagination) {
return from(pagination, Sort.unsorted());
}
public static Pageable from({{baseName}}Pageable pagination, Sort sort) {
Assert.notNull("pagination", pagination);
Assert.notNull("sort", sort);
return PageRequest.of(pagination.page(), pagination.pageSize(), sort);
}
public static {{baseName}}Page from(Page springPage, Function mapper) {
Assert.notNull("springPage", springPage);
Assert.notNull("mapper", mapper);
return {{baseName}}Page.builder(springPage.getContent().stream().map(mapper).toList())
.currentPage(springPage.getNumber())
.pageSize(springPage.getSize())
.totalElementsCount(springPage.getTotalElements())
.build();
}
}