com.github.sseserver.util.PageInfo Maven / Gradle / Ivy
package com.github.sseserver.util;
import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PageInfo implements Serializable, Iterable {
private static final long serialVersionUID = 1L;
private long total;
private int pageNum;
private int pageSize;
private List list;
private boolean timeout;
public PageInfo() {
this(new ArrayList<>());
}
public PageInfo(List list) {
this.list = Objects.requireNonNull(list);
this.total = list.size();
}
public static PageInfo empty() {
return PageInfo.of(Collections.emptyList());
}
public static PageInfo timeout() {
PageInfo info = PageInfo.of(Collections.emptyList());
info.setTimeout(true);
return info;
}
public static PageInfo of(List list, int pageNum, int pageSize) {
return of(list, pageNum, pageSize, false);
}
/**
* 分页
*
* @param list
* @param pageNum
* @param pageSize
* @param copy 是否复制. true=返回复制的集合. false=不复制(引用原始对象)
* @param
* @return 分好页的数据
*/
public static PageInfo of(List list, int pageNum, int pageSize, boolean copy) {
int total = list.size();
List sublist;
if (pageSize <= 0) {
sublist = Collections.emptyList();
} else {
int offsetBegin = (Math.max(pageNum, 1) - 1) * pageSize;
int offsetEnd = Math.min(offsetBegin + pageSize, total);
if (offsetBegin == 1 && offsetEnd == total) {
sublist = list;
} else {
sublist = list.subList(Math.min(offsetBegin, total), offsetEnd);
if (sublist.isEmpty()) {
sublist = Collections.emptyList();
}
}
if (copy) {
sublist = new ArrayList<>(sublist);
}
}
PageInfo pageInfo = new PageInfo<>();
pageInfo.setList(sublist);
pageInfo.setTotal(total);
pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(pageSize);
return pageInfo;
}
public static