JAVA分页实例

package cn.gov.songjiang.help.common;
/**
 * This class is an util for split page in java web applications.
 *
 * <p>
 * @author <a href=" [email protected]">Johnson</a>
 * </p>
 * @version 1.0
 *
 */
public class SplitPage
{
 //Total record amount.
 private int recordCount = 0;
 
 //Record per page.
 private int pageSize = 0;
 //Start record number for current page.
 private int startRecordNumber;
 //Last record number for current page.
 private int lastRecordNumber;
 /**
  * Constructor
  *
  * @param recordCount Total record amount.
  * @param pageSize  Record per page.
  */
 public SplitPage(int recordCount,int pageSize)
 {
  this.recordCount = recordCount;
  this.pageSize = pageSize;
  
  startRecordNumber = 1;
  lastRecordNumber = pageSize;
  if(lastRecordNumber>recordCount)
   lastRecordNumber = recordCount;
 }
 
 /**
  * Set total record amount.
  *
  * @param recordCount Total record amount
  */
 public void setRecordCount(int recordCount)
 {
  this.recordCount = recordCount;
 }
 /**
  * Set record per page.
  *
  * @param pageSize Record per page
  */
 public void setPageSize(int pageSize)
 {
  this.pageSize = pageSize;
 }
 
 /**
  * Test if current page is the first page.
  *
  * @return Return true if current page is the first page,false otherwise.
  */
 public boolean isFirstPage()
 {
  return (startRecordNumber==1)?true:false;
 }
 /**
  * Test if current page is last page.
  *
  * @return Return true if current page is the last page,false otherwise.
  */
 public boolean isLastPage()
 {
  return (lastRecordNumber==recordCount)?true:false;
 }
 /**
  * Move to next page.
  *
  * @exception IndexOutOfBoundsException If the current page is last page.
  */
 public void moveNext()   
 {
  if(!this.isLastPage())
  {
   startRecordNumber += pageSize;
   lastRecordNumber += pageSize;
   if(lastRecordNumber>recordCount)
    lastRecordNumber = recordCount;
  }
  else
  {
   throw new IndexOutOfBoundsException("Page index out of bounds!");
  }
 }
 
 /**
  * Move to previous page.
  *
  * @exception IndexOutOfBoundsException If the current page is first page.
  */
 public void movePrevious()  
 {
  if(!this.isFirstPage())
  {
   startRecordNumber -= pageSize;
   lastRecordNumber -= pageSize;
   if(lastRecordNumber>recordCount)
    lastRecordNumber = recordCount;
  }
  else
  {
   throw new IndexOutOfBoundsException("Page index out of bounds!");
  }
 }
 /**
  * Move to the first page.
  */
 public void moveFirst() 
 {
  startRecordNumber = 1;
  lastRecordNumber = pageSize;
  if(lastRecordNumber>recordCount)
   lastRecordNumber = recordCount;
 }
 /**
  * Move to the last page.
  *
  */
 public void moveLast()
 {
  int pageCount = recordCount / pageSize;
  if(recordCount % pageSize != 0)
  {
   pageCount++;
  }
  startRecordNumber = (pageCount - 1) * pageSize + 1;
  lastRecordNumber = recordCount;
 }
 
 /**
  * Move to specified page.
  *
  * @param pageNo the page no which will be move to.
  *
  * @exception IndexOutOfBoundsException if the page no is invalid.
  */
 public void move(int pageNo)
 {
  int pageCount = recordCount / pageSize;
  
  if(recordCount % pageSize != 0)
  {
   pageCount++;
  }
  if(pageNo > pageCount || pageNo < 1)
  {
      throw new IndexOutOfBoundsException("Page index out of Bounds!");
  }
  
  startRecordNumber = (pageNo - 1) * pageSize + 1;
  lastRecordNumber = startRecordNumber + pageSize - 1;
  if(lastRecordNumber>recordCount)
   lastRecordNumber = recordCount;
 }
 /**
  * Get the start record no of the current page.
  *
  * @return the start record no for current page.
  */
 public int getStartRecordNumber() 
 {
  return this.startRecordNumber;
 }
 /**
  * Get the last record no of the current page.
  *
  * @return the last record no for current page.
  */
 public int getLastRecordNumber()
 {
  return this.lastRecordNumber;
 }
 /**
  * Get the page count.
  *
  * @return the page count
  */
 public int getPageCount()
 {
  int pageCount = recordCount / pageSize;
  if(recordCount % pageSize != 0)
  {
   pageCount++;
  }
  return pageCount;
 }
 /**
  * Get the current page no.
  *
  * @return the current page no.
  */
 public int getCurrentPageNo()
 {
  int currentPageNo = (startRecordNumber / pageSize) + 1;
  return currentPageNo;
 }
 
 /**
  * Get record amount per page.
  *
  * @return the record amount per page.
  */
 public int getPageSize()
 {
  return this.pageSize;
 }
 
 /**
  * Get record amount.
  *
  * @return
  */
 public int getRecordCount()
 {
  return this.recordCount;
 }
}

你可能感兴趣的:(java,职场,实例,休闲)