io.github.icodegarden.nutrient.lang.util.PageUtils Maven / Gradle / Ivy
The newest version!
package io.github.icodegarden.nutrient.lang.util;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.github.icodegarden.nutrient.lang.query.BaseQuery;
import io.github.icodegarden.nutrient.lang.query.NextQuerySupportArrayList;
import io.github.icodegarden.nutrient.lang.query.NextQuerySupportList;
import io.github.icodegarden.nutrient.lang.query.NextQuerySupportPage;
import io.github.icodegarden.nutrient.lang.query.Page;
/**
*
* @author Fangfang.Xu
*
*/
public abstract class PageUtils {
private static final ThreadLocal> LOCAL_PAGE = new ThreadLocal<>();
public static Page startPage(int pageNum, int pageSize, boolean count) {
return startPage(pageNum, pageSize, count, null);
}
public static Page startPage(int pageNum, int pageSize, String orderBy) {
return startPage(pageNum, pageSize, true, orderBy);
}
public static Page startPage(int pageNum, int pageSize, boolean count, String orderBy) {
Page page = new Page(pageNum, pageSize, count);
page.setOrderBy(orderBy);
LOCAL_PAGE.set(page);
return page;
}
/**
* 是否执行了分页,即是否执行了startPage 且 正在分页中
* 如果分页调用已经结束,则是false,因为LocalPage已被自动remove
*
* @return
*/
public static boolean isPage() {
Page> page = LOCAL_PAGE.get();
return page != null;
}
/**
* 是否执行count,即是否执行了startPage 且 正在分页中 + count参数=true
* 否则false
*
* @return
*/
public static boolean isCount() {
Page> page = LOCAL_PAGE.get();
return page != null ? page.isCount() : false;
}
public static void clearPage() {
LOCAL_PAGE.remove();
}
/**
* 只转换类型
*/
public static Page ofPage(Page page, Function elementConvertor) {
Page newPage = new Page(page.getPageNum(), page.getPageSize());
newPage.setTotalCount(page.getTotalCount());
newPage.setTotalPages(page.getTotalPages());
newPage.setCount(page.isCount());
newPage.setOrderBy(page.getOrderBy());
convertAddAll(page, elementConvertor, newPage);
return newPage;
}
/**
* 自适应可能不进行count
*/
public static Page ofPageNoCountAdapt(Page page) {
ofPageNoCountAdapt(page, BaseQuery.MAX_TOTAL_COUNT);
return page;
}
/**
* 自适应可能不进行count
*
* @param maxTotal 当没有进行count时,限制的最多条数,例如10000条
*/
public static Page ofPageNoCountAdapt(Page page, long maxTotal) {
noCountAdapt(page, page, maxTotal);
return page;
}
/**
* 自适应可能不进行count
*/
public static Page ofPageNoCountAdapt(Page page, Function elementConvertor) {
return ofPageNoCountAdapt(page, elementConvertor, BaseQuery.MAX_TOTAL_COUNT);
}
/**
* 自适应可能不进行count
*
* @param maxTotal 当没有进行count时,限制的最多条数,例如10000条
*/
public static Page ofPageNoCountAdapt(Page page, Function elementConvertor, long maxTotal) {
Page newPage = new Page(page.getPageNum(), page.getPageSize());
noCountAdapt(page, newPage, maxTotal);
convertAddAll(page, elementConvertor, newPage);
return newPage;
}
/**
* 自适应可能不进行count
*
* 分页的按正常处理
* 不分页但结果条数小于页大小,则总页数按当前页处理,总条数按(总页数-1)*每页大小+当前返回条数
* 不分页但结果条数等于页大小,则按最大值处理
*
* @param maxTotal 当没有进行count时,限制的最多条数,例如10000条
*/
private static void noCountAdapt(Page> page, Page> targetPage, long maxTotal) {
if (page.isCount()) {
targetPage.setTotalCount(page.getTotalCount());
targetPage.setTotalPages(page.getTotalPages());
} else {
if (page.getResult().size() < page.getPageSize()) {
targetPage.setTotalCount((page.getPageNum() - 1) * page.getPageSize() + page.getResult().size());
targetPage.setTotalPages(page.getPageNum());
} else {
targetPage.setTotalCount(maxTotal);
targetPage.setTotalPages((int) (maxTotal / page.getPageSize()));
}
}
targetPage.setCount(page.isCount());
targetPage.setOrderBy(page.getOrderBy());
}
private static void convertAddAll(Page page, Function elementConvertor, Page targetPage) {
if (!page.getResult().isEmpty()) {
List list = page.getResult().stream().map(t -> {
return elementConvertor.apply(t);
}).collect(Collectors.toList());
targetPage.addAll(list);
}
}
// ----------------------------------------------------------
/**
* Page 转 NextQuerySupportPage
*/
public static NextQuerySupportPage pageToNextQuerySupportPage(Page page,
Function searchAfterSupplier) {
NextQuerySupportList nextQuerySupportList = NextQuerySupportArrayList.newSupportSearchAfter(page,
searchAfterSupplier);
return new NextQuerySupportPage(page.getPageNum(), page.getPageSize(), page.getTotalCount(), page.isCount(),
page.getOrderBy(), nextQuerySupportList);
}
/**
* Page 转 NextQuerySupportPage
*/
public static NextQuerySupportPage pageToNextQuerySupportPage(Page page,
Function elementConvertor, Function searchAfterSupplier) {
NextQuerySupportList nextQuerySupportList = NextQuerySupportArrayList.newSupportSearchAfter(page,
elementConvertor, searchAfterSupplier);
return new NextQuerySupportPage(page.getPageNum(), page.getPageSize(), page.getTotalCount(), page.isCount(),
page.getOrderBy(), nextQuerySupportList);
}
/**
* 只转换类型
*/
public static NextQuerySupportPage ofNextQuerySupportPage(NextQuerySupportPage page,
Function elementConvertor) {
NextQuerySupportList nextQuerySupportList = NextQuerySupportArrayList.newSupportSearchAfter(page,
elementConvertor, one -> page.getSearchAfter());
return new NextQuerySupportPage(page.getPageNum(), page.getPageSize(), page.getTotalCount(), page.isCount(),
page.getOrderBy(), nextQuerySupportList);
}
/**
* 自适应可能不进行count
*/
public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page) {
return ofNextQuerySupportPageNoCountAdapt(page, BaseQuery.MAX_TOTAL_COUNT);
}
/**
* 自适应可能不进行count
*
* @param maxTotal 当没有进行count时,限制的最多条数,例如10000条
*/
public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page,
long maxTotal) {
noCountAdapt(page, page, maxTotal);
return page;
}
/**
* 自适应可能不进行count
*/
public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page,
Function elementConvertor) {
return ofNextQuerySupportPageNoCountAdapt(page, elementConvertor, BaseQuery.MAX_TOTAL_COUNT);
}
/**
* 自适应可能不进行count
*
* @param maxTotal 当没有进行count时,限制的最多条数,例如10000条
*/
public static NextQuerySupportPage ofNextQuerySupportPageNoCountAdapt(NextQuerySupportPage page,
Function elementConvertor, long maxTotal) {
NextQuerySupportPage newPage = ofNextQuerySupportPage(page, elementConvertor);
noCountAdapt(page, newPage, maxTotal);
return newPage;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy