JAVA分页机制

package netctoss.util;


import java.util.Collection;




public class PageController {
//总行数
private int totalRowsAmount;
//每页行数
private int pageSize = 10;
//总页数
private int totalPages;
//当前页码
private int currentPage = 1;
//下一页
private int nextPage;
//上一页
private int previousPage;
//是否有下一页
private boolean hasNext;
//是否有上一页
private boolean hasPrevious;
//当前页开始行
private int pageStartRow;
//当前页结束行
private int pageEndRow;
//数据存储
@SuppressWarnings("unchecked")
private Collection data;

/**
  * 构造函数。
  * @param totalRows 总行数
  */
 public PageController(int totalRows,int currentPage) {
   setPageController(totalRows,currentPage);
 }


 public PageController(int totalRows,int currentPage,int pageSize){
     this.pageSize = pageSize;
     this.setPageController(totalRows,currentPage);
 }


 public void setPageController(int totalRows,int currentPage){


   setTotalRowsAmount(totalRows);
   setCurrentPage(currentPage);
 }
 
 /**
  * 设置总行数。
  *  i 总行数。
  */
 private void setTotalRowsAmount(int rows) {


   if(rows <0 ){
     totalRowsAmount =  0;
   }else{
     totalRowsAmount = rows;
   }


   if(totalRowsAmount%pageSize==0){
     totalPages = totalRowsAmount / pageSize;
   }else{
     totalPages = totalRowsAmount / pageSize + 1;
   }
 }


 /**
  * 设置当前页数。
  * @param i
  */
 private void setCurrentPage( int curPage) {


   if(curPage <= 0){
     currentPage = 1;
   }else if(curPage > totalPages){
     currentPage = totalPages;
   }else{
     currentPage = curPage;
   }


   if(currentPage == 1){
     hasPrevious = false;
   }else{
     hasPrevious = true;
   }


   if(currentPage == totalPages){
     hasNext = false;
   }else{
     hasNext = true;
   }




   nextPage = currentPage + 1;
   previousPage = currentPage - 1;


   //计算当前页开始行和结束行
   if(currentPage != totalPages){


     pageStartRow = (currentPage-1)*pageSize +1;


   }else{
     pageStartRow = (currentPage - 1)*pageSize+1;
   }


   //记录索引从0开始
   pageStartRow -= 1;
   pageEndRow = pageStartRow + pageSize;


 }


 public int getCurrentPage() {
   return currentPage;
 }


 public boolean isHasNext() {
   return hasNext;
 }


 public boolean isHasPrevious() {
   return hasPrevious;
 }


 public int getNextPage() {
   return nextPage;
 }


 public int getPageSize() {
   return pageSize;
 }


 public int getPreviousPage() {
   return previousPage;
 }


 public int getTotalPages() {
   return totalPages;
 }


 public int getTotalRowsAmount() {
   return totalRowsAmount;
 }


 public int getPageStartRow() {
   return pageStartRow;
 }


 public int getPageEndRow(){
     return pageEndRow;
 }


//  public String description() {
//    String description = "Total:" + this.getTotalRowsAmount() +
//        " items " + this.getTotalPages() + " pages,Current page:" +
//        this.currentPage + " Previous " + this.hasPrevious +
//        " Next:" + this.hasNext +
//        " start row:" + this.pageStartRow +
//        " end row:" + this.pageEndRow;
//    return description;
//  }


 @SuppressWarnings("unchecked")
public void setData(Collection data) {
   this.data = data;
 }
 @SuppressWarnings("unchecked")
public Collection getData() {
   return data;
 }


//  public static void main(String args[]){
//
//    Collection c = null;
//    PageController pc = new PageController(0,2);
//    System.out.println(pc.description());
//  }


}



你可能感兴趣的:(java,c,String,存储,Class)