package util;
import java.util.List;
public class PageManager
{
private List allRecords = null;//collection儲存同一類型的對象的集合
private int currentPage = 0;//當前頁碼
private int totalPage = 0;//總頁數(shù)
private int recordPerPage = -1;//每頁的對象數(shù)
private int totalCount=0;//總的對象數(shù)
//初始化
public PageManager(List allRecords, int recordPerPage)
{
if (allRecords == null || recordPerPage < 1) return;
this.allRecords = allRecords;
this.recordPerPage = recordPerPage;
this.totalCount=allRecords.size();
if (allRecords.size() % recordPerPage == 0)
this.totalPage = allRecords.size() / recordPerPage;
else
this.totalPage = allRecords.size() / recordPerPage + 1;
this.currentPage = 0;
}
//獲取所有對象集合
public List getAllRecords()
{
return this.allRecords;
}
//獲取當前頁的的對象集合
public List getCurrentPage()
{
return getPage(currentPage);
}
//根據(jù)序號獲取該對象所在的頁的對象集合
public List getThePage(int recordno)
{
if (this.allRecords == null || this.allRecords.size() == 0)
{
this.currentPage = 0;
return null;
}
int pageNo=1;
if (recordno < 1) pageNo = 1;
else if (recordno > this.allRecords.size())
pageNo = this.totalPage;
else
{
pageNo=recordno/this.recordPerPage+1;
}
this.currentPage = pageNo;
int pageStart = (pageNo - 1) * this.recordPerPage;
int pageEnd = pageStart + this.recordPerPage - 1;
if (pageEnd > this.allRecords.size() - 1) pageEnd = this.allRecords.size() - 1;
List result =this.allRecords.subList(pageStart, pageEnd+1);
return result;
}
//根據(jù)頁碼獲取改頁的對象集合
public List getPage(int pageNo)
{
if (this.allRecords == null || this.allRecords.size() == 0)
{
this.currentPage = 0;
return null;
}
if (pageNo < 1) pageNo = 1;
if (pageNo > this.totalPage) pageNo = this.totalPage;
this.currentPage = pageNo;
int pageStart = (pageNo - 1) * this.recordPerPage;
int pageEnd = pageStart + this.recordPerPage - 1;
if (pageEnd > this.allRecords.size() - 1) pageEnd = this.allRecords.size() - 1;
List result =this.allRecords.subList(pageStart, pageEnd+1);
return result;
}
//獲取下一頁的對象集合
public List getNextPage()
{
return getPage(this.currentPage + 1);
}
//獲取上一頁的對象集合
public List getPreviousPage()
{
return getPage(this.currentPage - 1);
}
//獲取第一頁的對象集合
public List getFirstPage()
{
return getPage(1);
}
//獲取最后一頁的對象集合
public List getLastPage()
{
return getPage(this.totalPage);
}
//獲取總頁數(shù)
public int getTotalPage() {
return totalPage;
}
//獲取當前頁碼
public int getCurrentPageCount()
{
return this.currentPage;
}
//獲取對象總數(shù)
public int getTotalCount() {
return totalCount;
}
}
Let life be beautiful like summer flowers and death like autumn leaves.