package com.sms.tenantexamineserver.util;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @ClassName PageUtil
* @Description TODO
**/
public class PageUtil {
/**
* 将数组分页,返回请求的页码的内容
* @return java.lang.String[]
**/
// public static String[] getPageList(String[] all,int pageNo,int pageSize){
// if(all == null || all.length <= 0){
// return null;
// }
// int length = all.length;
// //分页处理
// PageBean pageBean = new PageBean(pageSize,pageNo,length);
// int page = pageBean.getPageNo();
// int startIndex = (page -1) * pageSize;
// int endIndex = page * pageSize > length ? length : page * pageSize;
//
// return Arrays.copyOfRange(all,startIndex,endIndex);
// }
/**
* 将数组分页,返回请求的页码的内容
* @return java.lang.String[]
**/
public static Object[] getPageArray(Object[] all,int pageNo,int pageSize){
if(all == null || all.length <= 0){
return null;
}
int length = all.length;
//分页处理
PageBean pageBean = new PageBean(pageSize,pageNo,length);
int page = pageBean.getPageNo();
int startIndex = (page -1) * pageSize;
int endIndex = page * pageSize > length ? length : page * pageSize;
return Arrays.copyOfRange(all,startIndex,endIndex);
}
/**
* pageNo 页码
* pageSize 每页数量
*/
public static <T> List<T> getPageList(List<T> all,int pageNo,int pageSize){
if(all == null || all.size() <= 0){
return null;
}
int length = all.size();
//分页处理
PageBean pageBean = new PageBean(pageSize,pageNo,length);
int page = pageBean.getPageNo();
int startIndex = (page -1) * pageSize;
int endIndex = page * pageSize > length ? length : page * pageSize;
List<T> tList = new ArrayList<>();
for(int i = startIndex;i < endIndex;i++){
tList.add(all.get(i));
}
return tList;
}
public static String[] getDescPageList(String[] all,int pageNo,int pageSize){
if(all == null || all.length <= 0){
return null;
}
int length = all.length;
PageBean pageBean = new PageBean("desc",length,pageNo + "",pageSize);
int endIndex = pageBean.getStartIndex() + 1;
int startIndex = endIndex - pageBean.getPageSize();
return getDescArray(Arrays.copyOfRange(all,startIndex,endIndex));
}
private static String[] getDescArray(String[] strings) {
if(strings == null || strings.length <= 0){
return null;
}
String[] arrays = new String[strings.length];
for(int i = 0;i < arrays.length;i++){
arrays[i] = strings[strings.length - 1 - i];
}
return arrays;
}
}
分页bean
package com.sms.tenantexamineserver.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
/**
* @Description TODO
**/
public class PageBean {
//页码,默认页面是1
private int pageNo = 1;
//总页数
private int totalPage = 0;
//起始位置
private int startIndex=0;
//本页实际展示数
private int pageSize = 0;
//请求每页显示数
private int reqPageSize;
private int totalSize;
public PageBean(int size, int page, int totalSize){
reqPageSize = size <= 0 ? NumberUtils.toInt("10") : size;
this.pageNo = page;
this.totalSize = totalSize;
if(size > 0){
//计算总页数
if(totalSize % size == 0){
//整除,正好每页显示pageSize条数据,没有多余一页要显示少于pageSize条数据的
this.totalPage = totalSize/size;
}else{
//不整除,就要在加一页,来显示多余的数据。
this.totalPage = totalSize/size + 1;
}
// 计算当前页数
if(page <= 0){
this.pageNo = 1;
}
if(page > totalPage){
this.pageNo = totalPage;
}
this.pageSize = size;
//开始索引
this.startIndex = (pageNo - 1)*size;
// 设置需要展示的字数或者记录条数
int length = this.startIndex + size;
if(length > totalSize){
this.pageSize = totalSize - this.startIndex;
}else{
this.pageSize = size;
}
}
}
/**
* <一句话功能简述> <功能详细描述>
*
* @param orderType 排序方式
* @param total 总字数/总记录条数
* @param page 当前页码
* @param size 每页显示字数/每页显示记录条数
* @return
*/
public PageBean(String orderType, int total, String page, int size){
this.reqPageSize = size;
this.totalSize = total;
if (!"desc".equals(orderType)){
// 默认按正序排列
orderType = "asc";
}
if(size == 0){
return;
}
// 设置总页数
if(total == 0){
this.totalPage = 1;
}else if(0 == total/size){
this.totalPage = total/size;
}else{
this.totalPage = total/size + 1;
}
// 计算当前页码,输入的页面为空或非数字,则默认为1
if(StringUtils.isEmpty(page) || !page.matches("\\d+")){
page = "1";
}
//当前页
this.pageNo = Integer.parseInt(page);
// 页面大于总页数,则为总页数,少于1,则为1
if(pageNo > totalPage){
pageNo = totalPage;
}else if (pageNo <= 0){
pageNo = 1;
}
// 当前页图书起始阅读字节位置
if(StringUtils.equals(orderType,"desc")){
this.startIndex = total - ((pageNo - 1) * size);
if(startIndex < 0){
startIndex =pageNo*size - total;
}
startIndex = startIndex - 1;
// 设置需要展示的字数或者记录条数
int length = this.startIndex - size;
if(length < 0){
this.pageSize = startIndex + 1;
}else{
this.pageSize = size;
}
}else{
this.startIndex = (pageNo - 1)*size;
// 设置需要展示的字数或者记录条数
int length = this.startIndex + size;
if(length > total){
this.pageSize = total - this.startIndex;
}else{
this.pageSize = size;
}
}
}
public int getPageNo() {
return pageNo;
}
public int getTotalPage() {
return totalPage;
}
//起始位置,从0开始
public int getStartIndex() {
return startIndex;
}
//本页实际展示条数
public int getPageSize() {
return pageSize;
}
//分页请求,每页显示数
public int getReqPageSize() {
return reqPageSize;
}
//返回 totalSize
public int getTotalSize() {
return totalSize;
}
}