阿翔编程学-Struts分页信息设置

package com.jxsafe.source.common.applications.source.classinfo.pageinfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 分页信息设置
 * @author CaoXiang
 */
public class PageInformation implements Serializable, Cloneable {

  /**
   * The serialVersionUID
   */
  private static final long serialVersionUID = 7960850923665483933L;

  /**
   * Default span
   */
  protected static final int DEFAULT_SPAN = 10;

  /**
   * Page number (the first page is 1)
   */
  private int pageNo;

  /**
   * Page size
   */
  private int pageSize;

  /**
   * Count of total pages
   */
  private int totalPage;

  /**
   * Max record num;
   */
  private int maxRecordNum;

  /**
   * Creates a new <code>PageInformation</code> object.
   */
  public PageInformation() {
  }

  /**
   * Retrieves the pageNo.
   *
   * @return Returns the pageNo.
   */
  public int getPageNo() {
    return pageNo;
  }

  /**
   * Sets the pageNo to the given value.
   *
   * @param pageNo
   *          The pageNo to set.
   */
  public void setPageNo(int pageNo) {
    this.pageNo = pageNo;
  }

  /**
   * Retrieves the pageSize.
   *
   * @return Returns the pageSize.
   */
  public int getPageSize() {
    return pageSize;
  }

  /**
   * Sets the pageSize to the given value.
   *
   * @param pageSize
   *          The pageSize to set.
   */
  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  /**
   * Retrieves the totalPage.
   *
   * @return Returns the totalPage.
   */
  public int getTotalPage() {
    return totalPage;
  }

  /**
   * Sets the totalPage to the given value.
   *
   * @param totalPage
   *          The totalPage to set.
   */
  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }

  /**
   * Reset method.
   */
  public void reset() {
    pageNo = 1;
    totalPage = 0;
  }

  /**
   * Retrieves the maxRecordNum.
   *
   * @return Returns the maxRecordNum.
   */
  public int getMaxRecordNum() {
    return maxRecordNum;
  }

  /**
   * Sets the maximum record number to the given value
   *
   * @param maxRecordNum
   */
  public void setMaxRecordNum(int maxRecordNum) {
    if (maxRecordNum < 0) {
      throw new IllegalArgumentException("Negative maxRecordNum!");
    } else {
      this.maxRecordNum = maxRecordNum;
      if (maxRecordNum == Integer.MAX_VALUE) {
        totalPage = 0x7fffffff;
      } else {
        totalPage = (maxRecordNum + pageSize - 1) / pageSize;
      }
      if (pageNo > totalPage) {
        pageNo = totalPage;
      }
    }
  }

  /**
   * Retrieves the offset (zero-based) of the first row of this page.
   *
   * @return the page data offset
   */
  public int getOffset() {
    return (pageNo - 1) * pageSize;
  }

  /**
   * Retrieves the default page span.
   *
   * @return the page numbers in the specified span
   */
  public List<Integer> getPageSpan() {
    return getPageSpan(DEFAULT_SPAN);
  }

  /**
   * Retrieves the page span.
   *
   * @param span
   *          the number of span of pages in either direction
   * @return the page numbers in the specified span
   */
  public List<Integer> getPageSpan(int span) {
    return getPageSpan(span, span);
  }

  /**
   * Retrieves the page span.
   *
   * @param leftSpan
   *          the number of span of the previous pages
   * @param rightSpan
   *          the number of span of the next pages
   * @return the page numbers in the specified span
   */
  public List<Integer> getPageSpan(int leftSpan, int rightSpan) {
    if (leftSpan <= 0) {
      throw new IllegalArgumentException("Left span must be positive!");
    }

    if (rightSpan <= 0) {
      throw new IllegalArgumentException("Right span must be positive!");
    }

    List<Integer> pages = new ArrayList<Integer>(leftSpan << 1);

    // Computes the start and end page number.
    int start = Math.max(pageNo - leftSpan, 1);
    int end = Math.min(pageNo + rightSpan, totalPage);

    for (int i = start; i <= end; i++) {
      pages.add(i);
    }

    return pages;
  }

  /**
   * Returns a hash code value for the object.
   *
   * @return a hash code value for this object.
   */
  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(371295, 137, this);
  }

  /**
   * Indicates whether some other object is "equal to" this one.
   *
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   */
  @Override
  public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj);
  }

  /**
   * Retrieves a text representation for this object.
   *
   * @return the text representation
   */
  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this);
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值