Pager Base Class

package cn.newtouch.bean;

import java.util.List;

/**
 * The Class Pager.
 * 
 * @param <T>
 *            the generic type
 */
public class Pager<T> {

	/** The result list. */
	private List<T> resultList;

	/** The total record. */
	private int totalRecords = 0;

	/** The limit. */
	private int limit = 20;

	/** The total page. */
	private int totalPages = 1;

	/** The current page. */
	private int currentPage = 1;

	/** The is first page. */
	private boolean isFirstPage = false;

	/** The is last page. */
	private boolean isLastPage = false;

	/** The has previous page. */
	private boolean hasPreviousPage = false;

	/** The has next page. */
	private boolean hasNextPage = false;

	/** The navigate pages. */
	private int navigatePages = 8;

	/** The navigate page numbers. */
	private int[] navigatePageNumbers;

	/**
	 * Instantiates a new pager.
	 * 
	 * @param totalRecords
	 *            the total records
	 * @param currentPage
	 *            the current page
	 */
	public Pager(int totalRecords, int currentPage) {
		init(totalRecords, currentPage, limit);
	}

	/**
	 * Instantiates a new pager.
	 * 
	 * @param totalRecords
	 *            the total records
	 * @param currentPage
	 *            the current page
	 * @param limit
	 *            the limit
	 */
	public Pager(int totalRecords, int currentPage, int limit) {
		init(totalRecords, currentPage, limit);
	}

	/**
	 * Inits the.
	 * 
	 * @param totalRecords
	 *            the total records
	 * @param currentPage
	 *            the current page
	 * @param limit
	 *            the limit
	 */
	private void init(int totalRecords, int currentPage, int limit) {
		this.totalRecords = totalRecords;
		this.limit = limit;
		this.totalPages = (this.totalRecords - 1) / this.limit + 1;

		if (currentPage < 1) {
			this.currentPage = 1;
		} else if (currentPage > this.totalPages) {
			this.currentPage = this.totalPages;
		} else {
			this.currentPage = currentPage;
		}
		calcNavigatePageNumbers();

		judgePageBoundary();
	}

	/**
	 * Calc navigate page numbers.
	 */
	private void calcNavigatePageNumbers() {
		if (this.totalPages <= this.navigatePages) {
			this.navigatePageNumbers = new int[this.totalPages];
			for (int i = 0; i < this.totalPages; i++) {
				this.navigatePageNumbers[i] = i + 1;
			}
		} else {
			this.navigatePageNumbers = new int[this.navigatePages];
			int startNumber = this.currentPage - this.navigatePages / 2;
			int endNumber = this.currentPage + this.navigatePages / 2;
			if (startNumber < 1) {
				startNumber = 1;
				for (int i = 0; i < this.navigatePages; i++) {
					this.navigatePageNumbers[i] = startNumber++;
				}
			} else if (endNumber > this.totalPages) {
				endNumber = this.totalPages;
				for (int i = (this.navigatePages - 1); i >= 0; i--) {
					this.navigatePageNumbers[i] = endNumber--;
				}
			} else {
				for (int i = 0; i < this.navigatePages; i++) {
					this.navigatePageNumbers[i] = startNumber++;
				}
			}
		}
	}

	/**
	 * Judge page boundary.
	 */
	private void judgePageBoundary() {
		this.isFirstPage = (this.currentPage == 1);
		this.isLastPage = (this.currentPage == this.totalPages && this.currentPage != 1);
		this.hasPreviousPage = (this.currentPage != 1);
		this.hasNextPage = (this.currentPage != this.totalPages);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String resultStr = new String();
		resultStr = "[" + "totalPages=" + this.totalPages + ",totalPages=" + this.totalPages + ",currentPage=" + this.currentPage + ",limit="
				+ this.limit + ",isFirstPage=" + this.isFirstPage + ",isLastPage=" + this.isLastPage + ",hasPreviousPage=" + this.hasPreviousPage
				+ ",hasNextPage=" + this.hasNextPage + ",navigatePageNumbers=";
		int len = this.navigatePageNumbers.length;
		if (len > 0) {
			resultStr += (this.navigatePageNumbers[0]);
		}
		for (int i = 1; i < len; i++) {
			resultStr += (" " + this.navigatePageNumbers[i]);
		}
		resultStr += "]";
		return resultStr;
	}

	/**
	 * Getter resultList.
	 * 
	 * @return the resultList
	 */
	public List<T> getResultList() {
		return resultList;
	}

	/**
	 * Setter resultList.
	 * 
	 * @param resultList
	 *            the resultList to set
	 */
	public void setResultList(List<T> resultList) {
		this.resultList = resultList;
	}

	/**
	 * Getter totalPages.
	 * 
	 * @return the totalPages
	 */
	public int getTotalPages() {
		return totalPages;
	}

	/**
	 * Setter totalPages.
	 * 
	 * @param totalPages
	 *            the totalPages to set
	 */
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	/**
	 * Getter limit.
	 * 
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * Setter limit.
	 * 
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * Getter totalRecords.
	 * 
	 * @return the totalRecords
	 */
	public int getTotalRecords() {
		return totalRecords;
	}

	/**
	 * Setter totalRecords.
	 * 
	 * @param totalRecords
	 *            the totalRecords to set
	 */
	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

	/**
	 * Getter currentPage.
	 * 
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * Setter currentPage.
	 * 
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * Getter isFirstPage.
	 * 
	 * @return the isFirstPage
	 */
	public boolean isFirstPage() {
		return isFirstPage;
	}

	/**
	 * Setter isFirstPage.
	 * 
	 * @param isFirstPage
	 *            the isFirstPage to set
	 */
	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	/**
	 * Getter isLastPage.
	 * 
	 * @return the isLastPage
	 */
	public boolean isLastPage() {
		return isLastPage;
	}

	/**
	 * Setter isLastPage.
	 * 
	 * @param isLastPage
	 *            the isLastPage to set
	 */
	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	/**
	 * Getter hasPreviousPage.
	 * 
	 * @return the hasPreviousPage
	 */
	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	/**
	 * Setter hasPreviousPage.
	 * 
	 * @param hasPreviousPage
	 *            the hasPreviousPage to set
	 */
	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	/**
	 * Getter hasNextPage.
	 * 
	 * @return the hasNextPage
	 */
	public boolean isHasNextPage() {
		return hasNextPage;
	}

	/**
	 * Setter hasNextPage.
	 * 
	 * @param hasNextPage
	 *            the hasNextPage to set
	 */
	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	/**
	 * Getter navigatePages.
	 * 
	 * @return the navigatePages
	 */
	public int getNavigatePages() {
		return navigatePages;
	}

	/**
	 * Setter navigatePages.
	 * 
	 * @param navigatePages
	 *            the navigatePages to set
	 */
	public void setNavigatePages(int navigatePages) {
		this.navigatePages = navigatePages;
	}

	/**
	 * Getter navigatePageNumbers.
	 * 
	 * @return the navigatePageNumbers
	 */
	public int[] getNavigatePageNumbers() {
		return navigatePageNumbers;
	}

	/**
	 * Setter navigatePageNumbers.
	 * 
	 * @param navigatePageNumbers
	 *            the navigatePageNumbers to set
	 */
	public void setNavigatePageNumbers(int[] navigatePageNumbers) {
		this.navigatePageNumbers = navigatePageNumbers;
	}

}

 

你可能感兴趣的:(java)