com.penglecode.common.support.PaginationUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
commons is a little java tool to make your development easier in your work.
The newest version!
package com.penglecode.common.support;
import java.util.ArrayList;
import java.util.List;
/**
* 分页页码生成工具类
*
* @author pengpeng
* @date 2013-10-28 上午11:03:09
* @version 1.0
*/
public class PaginationUtils {
/**
* 生成分页页码
* @param pageMargin - 当前页的前后margin, 例如currentPage = 5,pageMargin = 2,则3,4,5,6,7这个五个页码是必须显示的
* @param currentPage - 当前页码
* @param totalPageCount - 总页数
* @return
*/
public static List getPaginationItems(int pageMargin, int currentPage, int totalPageCount) {
List pageItems = new ArrayList();
if(pageMargin < 1){
throw new IllegalArgumentException("'pageMargin' can not less than 1!");
}
if(currentPage < 1){
throw new IllegalArgumentException("'currentPage' can not less than 1!");
}
if(totalPageCount < 0){
throw new IllegalArgumentException("'totalPageCount' can not less than 0!");
}
if(totalPageCount == 0 || totalPageCount < currentPage){
return pageItems;
}
int start = currentPage - pageMargin;
int end = currentPage + pageMargin;
if(start <= 0){
int offset = Math.abs(start) + 1;
start = start + offset;
end = end + offset;
}
if(end > totalPageCount){
int offset = totalPageCount - end;
end = end + offset;
start = start + offset;
start = start < 1 ? 1 : start;
}
for(int i = start; i <= end; i++){
if(i == start && i > 1){ //first
pageItems.add(1);
if(i - 1 > 2){
pageItems.add(null);
}else if(i - 1 == 2){
pageItems.add(i - 1);
}
}
pageItems.add(i);
if(i == end && i < totalPageCount){ //last
if(totalPageCount - end > 2){
pageItems.add(null);
}else if(totalPageCount - end == 2){
pageItems.add(totalPageCount - 1);
}
pageItems.add(totalPageCount);
}
}
return pageItems;
}
public static void setPageItems(Pager pager) {
pager.setPageItems(getPaginationItems(pager.getPageMargin(), pager.getCurrentPage(), pager.getTotalPageCount()));
}
}