//-------start 分页service
package cn.*.util.bean.page;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import cn.migu.newportal.util.constants.ParamKeys;
import cn.migu.newportal.util.other.Util;
/**
* 分页相关处理功能
*
* @author
* @version
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@SuppressWarnings("unchecked")
public class PaginTools
{
/**
* 将数组分页,返回请求的页码的内容
*
* @param bookIds
* @param pageNo
* @param pageSize
* @return
* @see [类、类#方法、类#成员]
*/
public static String[] getPagedList(String[] all, int pageNo, int pageSize)
{
if (Util.isEmpty(all))
{
return null;
}
int length = all.length;
// 分页处理
PaginationInfo pageInfo = new PaginationInfo(pageSize, pageNo, length);
int page = pageInfo.getPageNo();
int start = (page - 1) * pageSize;
int end = page * pageSize > length ? length : page * pageSize;
return Arrays.copyOfRange(all, start, end);
}
/**
* 将数组分页,返回请求的页码的内容
*
* @param bookIds
* @param pageNo
* @param pageSize
* @return
* @see [类、类#方法、类#成员]
*/
public static Object[] getPagedArray(Object[] all, int pageNo, int pageSize)
{
if (Util.isEmpty(all))
{
return null;
}
int length = all.length;
// 分页处理
PaginationInfo pageInfo = new PaginationInfo(pageSize, pageNo, length);
int page = pageInfo.getPageNo();
int start = (page - 1) * pageSize;
int end = page * pageSize > length ? length : page * pageSize;
return Arrays.copyOfRange(all, start, end);
}
/**
* 将列表分页,返回请求的页码的内容
*
* @param bookIds
* @param pageNo
* @param pageSize
* @return
* @see [类、类#方法、类#成员]
*/
public static <T> List<T> getPagedLs(List<T> all, int pageNo, int pageSize)
{
if (Util.isEmpty(all))
{
return null;
}
int length = all.size();
// 分页处理
PaginationInfo pageInfo = new PaginationInfo(pageSize, pageNo, length);
int page = pageInfo.getPageNo();
int start = (page - 1) * pageSize;
int end = page * pageSize > length ? length : page * pageSize;
List<T> ls = new ArrayList();
for (int i = start; i < end; i++)
{
ls.add(all.get(i));
}
return ls;
}
/**
* 获取倒序数组
*
* @param all
* @param pageNo
* @param pageSize
* @return
* @see [类、类#方法、类#成员]
*/
public static String[] getDescPagedList(String[] all, int pageNo,
int pageSize)
{
if (Util.isEmpty(all))
{
return null;
}
int length = all.length;
PaginationInfo pageInfo =
new PaginationInfo(ParamKeys.DESC/**倒叙:"desc"*/, length, pageNo + "", pageSize);
int end = pageInfo.getStart() + 1;
int start = end - pageInfo.getPageSize();
return getDescArray(Arrays.copyOfRange(all, start, end));
}
/**
* 数组倒序
*
* @param array
* @return
* @see [类、类#方法、类#成员]
*/
public static String[] getDescArray(String[] array)
{
if (Util.isEmpty(array))
{
return null;
}
String[] arrays = new String[array.length];
for (int i = 0; i < arrays.length; i++)
{
arrays[i] = array[array.length - 1 - i];
}
return arrays;
}
@SuppressWarnings("rawtypes")
public static <T> List<T> getPagedLists(List<T> all, int pageNo, int pageSize)
{
if (Util.isEmpty(all))
{
return null;
}
int length = all.size();
// 分页处理
PaginationInfo pageInfo = new PaginationInfo(pageSize, pageNo, length);
int page = pageInfo.getPageNo();
int start = (page - 1) * pageSize;
int end = page * pageSize > length ? length : page * pageSize;
List ls = new ArrayList();
for (int i = start; i < end; i++)
{
ls.add(all.get(i));
}
return ls;
}
}
// ------end 分页sercie
// ----start 分页 javaBean
package cn.*.util.bean.page;
import org.apache.commons.lang3.math.NumberUtils;
import cn.*.constants.ParamConstants;
import cn.*.util.exception.PortalException;
public class PaginationInfo
{
/** 页码,默认页面为1 */
private int pageNo = 1;
/** 总页数 */
private int totalPage = 0;
/** 起始位置 */
private int start = 0;
/**本页实际展示数*/
private int pageSize = 0;
/** 分页请求每页显示数 */
private int reqPageSize;
private int totalSize;
public PaginationInfo(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)
{
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.start = (pageNo - 1) * size;
// 设置需要展示的字数或者记录条数
int length = this.start + size;
if(length > totalSize)
{
this.pageSize = totalSize - this.start;
}
else
{
this.pageSize = size;
}
}
}
/**
* <一句话功能简述> <功能详细描述>
*
* @param orderType 排序方式
* @param total 总字数/总记录条数
* @param page 当前页码
* @param size 每页显示字数/每页显示记录条数
* @return
* @throws PortalException [参数说明]
*
* @return String [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public PaginationInfo(String orderType, int total, String page, int size)
{
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 (null == page || "".equals(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 ("desc".equals(orderType))
{
this.start = total - ((pageNo - 1) * size);
if (start < 0)
{
start = (pageNo * size) - total;
}
start = start - 1;
// 设置需要展示的字数或者记录条数
int length = this.start - size;
if (length < 0)
{
this.pageSize = start + 1;
}
else
{
this.pageSize = size;
}
}
else
{
this.start = (pageNo - 1) * size;
// 设置需要展示的字数或者记录条数
int length = this.start + size;
if (length > total)
{
this.pageSize = total - this.start;
}
else
{
this.pageSize = size;
}
}
}
public int getPageNo()
{
return pageNo;
}
public int getTotalPage()
{
return totalPage;
}
/**
* 起始位置,从0开始
* @return
* @see [类、类#方法、类#成员]
*/
public int getStart()
{
return start;
}
/**
* 本页实际展示条数
* @return
* @see [类、类#方法、类#成员]
*/
public int getPageSize()
{
return pageSize;
}
/**
* 分页请求,每页显示数
* @return
* @see [类、类#方法、类#成员]
*/
public int getReqPageSize()
{
return reqPageSize;
}
/**
* @return 返回 totalSize
*/
public int getTotalSize()
{
return totalSize;
}
}
// ---------end 分页 javaBean