通用分页工具类
2015-09-18 14:14
288 查看
package com.util;
import java.util.ArrayList;
import java.util.List;
/**
* 通用分页工具类,该工具类分页指定泛型的List对集合
*
* @author evan
* @time 2010-01-12
* @param <E>
* 指定的泛型
*/
public class Page<E>
{
/**
* 每页显示的记录数
*/
private int pageRecords = AttributeNameConstant.RECORD_NUMBER;
/**
* 总记录数
*/
private int totalRecord;
/**
* 分页切割的启始点
*/
private int startIndex;
/**
* 分页切割的结束点
*/
private int endIndex;
/**
* 总页数
*/
private int totalPage;
/**
* 当前页数
*/
private int currentPage = 1;
/**
* 总记录集合
*/
private List<E> totalList;
public Page(List<E> totalList)
{
super();
this.totalList = totalList;
innit();
}
/**
* 初始化该分页对象
*/
private void innit()
{
if (null != totalList)
{
totalRecord = totalList.size();
if (totalRecord % this.pageRecords == 0)
{
this.totalPage = totalRecord / this.pageRecords;
}
else
{
this.totalPage = totalRecord / this.pageRecords + 1;
}
}
}
/**
* 得到分页后的数据
*
* @return 分页数据
*/
public List<E> getPage(int currentPage)
{
this.currentPage = currentPage;
if (currentPage <= 0)
{
this.currentPage = 1;
}
if (currentPage >= this.totalPage)
{
this.currentPage = this.totalPage;
}
List<E> subList = new ArrayList<E>();
if (null != this.totalList)
{
subList.addAll(this.totalList.subList(getStartIndex(), getEndIndex()));
}
return subList;
}
/**
* 设置每页显示的记录条数,如果不设置则默认为每页显示30条记录
*
* @param pageRecords
* 每页显示的记录条数(值必需介于10~100之间)
*/
public void setPageRecords(int pageRecords)
{
if (pageRecords >= 10 && pageRecords <= 100)
{
this.pageRecords = pageRecords;
innit();
}
}
public int getStartIndex()
{
if (null == this.totalList)
{
return 0;
}
this.startIndex = (getCurrentPage() - 1) * this.pageRecords;
if (startIndex > totalRecord)
{
startIndex = totalRecord;
}
if (startIndex < 0)
{
startIndex = 0;
}
return startIndex;
}
public int getEndIndex()
{
if (null == this.totalList)
{
return 0;
}
endIndex = getStartIndex() + this.pageRecords;
if (endIndex < 0)
{
endIndex = 0;
}
if (endIndex < getStartIndex())
{
endIndex = getStartIndex();
}
if (endIndex > this.totalRecord)
{
endIndex = this.totalRecord;
}
return endIndex;
}
public int getTotalPage()
{
return totalPage;
}
public int getTotalRecord()
{
return totalRecord;
}
public boolean isEndPage()
{
return this.currentPage == this.totalPage;
}
/**
* 获取下一页的页数
*
* @return 下一页的页数
*/
public int getNextPage()
{
int nextPage = this.currentPage + 1;
if (nextPage > this.totalPage)
{
nextPage = this.totalPage;
}
if (nextPage <= 0)
{
nextPage = 1;
}
return nextPage;
}
/**
* 获取上一页的页数
*
* @return 上一页的页数
*/
public int getPrivyPage()
{
int privyPage = this.currentPage - 1;
if (privyPage > this.totalPage)
{
privyPage = this.totalPage;
}
if (privyPage <= 0)
{
privyPage = 1;
}
return privyPage;
}
public int getCurrentPage()
{
return currentPage;
}
}
import java.util.ArrayList;
import java.util.List;
/**
* 通用分页工具类,该工具类分页指定泛型的List对集合
*
* @author evan
* @time 2010-01-12
* @param <E>
* 指定的泛型
*/
public class Page<E>
{
/**
* 每页显示的记录数
*/
private int pageRecords = AttributeNameConstant.RECORD_NUMBER;
/**
* 总记录数
*/
private int totalRecord;
/**
* 分页切割的启始点
*/
private int startIndex;
/**
* 分页切割的结束点
*/
private int endIndex;
/**
* 总页数
*/
private int totalPage;
/**
* 当前页数
*/
private int currentPage = 1;
/**
* 总记录集合
*/
private List<E> totalList;
public Page(List<E> totalList)
{
super();
this.totalList = totalList;
innit();
}
/**
* 初始化该分页对象
*/
private void innit()
{
if (null != totalList)
{
totalRecord = totalList.size();
if (totalRecord % this.pageRecords == 0)
{
this.totalPage = totalRecord / this.pageRecords;
}
else
{
this.totalPage = totalRecord / this.pageRecords + 1;
}
}
}
/**
* 得到分页后的数据
*
* @return 分页数据
*/
public List<E> getPage(int currentPage)
{
this.currentPage = currentPage;
if (currentPage <= 0)
{
this.currentPage = 1;
}
if (currentPage >= this.totalPage)
{
this.currentPage = this.totalPage;
}
List<E> subList = new ArrayList<E>();
if (null != this.totalList)
{
subList.addAll(this.totalList.subList(getStartIndex(), getEndIndex()));
}
return subList;
}
/**
* 设置每页显示的记录条数,如果不设置则默认为每页显示30条记录
*
* @param pageRecords
* 每页显示的记录条数(值必需介于10~100之间)
*/
public void setPageRecords(int pageRecords)
{
if (pageRecords >= 10 && pageRecords <= 100)
{
this.pageRecords = pageRecords;
innit();
}
}
public int getStartIndex()
{
if (null == this.totalList)
{
return 0;
}
this.startIndex = (getCurrentPage() - 1) * this.pageRecords;
if (startIndex > totalRecord)
{
startIndex = totalRecord;
}
if (startIndex < 0)
{
startIndex = 0;
}
return startIndex;
}
public int getEndIndex()
{
if (null == this.totalList)
{
return 0;
}
endIndex = getStartIndex() + this.pageRecords;
if (endIndex < 0)
{
endIndex = 0;
}
if (endIndex < getStartIndex())
{
endIndex = getStartIndex();
}
if (endIndex > this.totalRecord)
{
endIndex = this.totalRecord;
}
return endIndex;
}
public int getTotalPage()
{
return totalPage;
}
public int getTotalRecord()
{
return totalRecord;
}
public boolean isEndPage()
{
return this.currentPage == this.totalPage;
}
/**
* 获取下一页的页数
*
* @return 下一页的页数
*/
public int getNextPage()
{
int nextPage = this.currentPage + 1;
if (nextPage > this.totalPage)
{
nextPage = this.totalPage;
}
if (nextPage <= 0)
{
nextPage = 1;
}
return nextPage;
}
/**
* 获取上一页的页数
*
* @return 上一页的页数
*/
public int getPrivyPage()
{
int privyPage = this.currentPage - 1;
if (privyPage > this.totalPage)
{
privyPage = this.totalPage;
}
if (privyPage <= 0)
{
privyPage = 1;
}
return privyPage;
}
public int getCurrentPage()
{
return currentPage;
}
}
相关文章推荐
- 把字符串转换成字典
- 飞鸽协议分析之上下线报文分析之二
- 二维码保存时看到是一张纯黑色图片问题
- LoadRunner中常用的C语言函数
- Error:IntelliSense: cannot open source "stdafx.h"
- android 美化zxing二维码扫描框
- 安卓(android)开发应该怎么学?需要哪些基础知识?
- Linux-CenOS7 Samba的安装与配置
- javascript 异步模块加载 简易实现
- bash编程之:case语句、read与用户交互
- 新人学ruby---方法的分类
- cocos2d-x 3.4 sprite3d c3b 模型无法显示的问题
- 压缩图片
- WebView基本使用
- 在Mesos上创建Rancher容器服务
- 身份通
- 程序员的噩梦,我的漏洞伤害了他人
- Ref和Out
- bash编程之:if语句
- IOS9之Xcode7允许Http