JAVA中List集合分页

package com.brit.os.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 = 20;

	/**
	 * 总记录数
	 */
	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;
	}
	
	/***
	 * 获取总页数
	 * @return
	 */
	public int getTotalPage()
	{
		return totalPage;
	}
	
	/**
	 * 获取List集合中的总条数
	 * @return
	 */
	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;
	}
	
	/**
	 * 获取当前页页数
	 * @return
	 */
	public int getCurrentPage()
	{
		return currentPage;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值