package com.aperture_software.glados_wiki.support; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; import java.io.Serializable; /** * Created by jhyun on 13. 12. 21. */ public class Pagination implements Cloneable, Serializable { private static final long serialVersionUID = -5717010280390030616L; private long totalRows; private long currentOffsetRow; private long rowsPerPage; public Pagination(long totalRows, long currentOffsetRow, long rowsPerPage) { this.totalRows = totalRows; this.currentOffsetRow = currentOffsetRow; this.rowsPerPage = rowsPerPage; } public long getTotalRows() { return totalRows; } public void setTotalRows(long totalRows) { this.totalRows = totalRows; } public long getCurrentOffsetRow() { return currentOffsetRow; } public void setCurrentOffsetRow(long currentOffsetRow) { this.currentOffsetRow = currentOffsetRow; } public long getRowsPerPage() { return rowsPerPage; } public void setRowsPerPage(long rowsPerPage) { this.rowsPerPage = rowsPerPage; } /** * 전체 페이지 갯수. * * @return 전체 페이지 갯수. 최소 1. */ public long getMaxPage() { if (this.totalRows < this.rowsPerPage || this.rowsPerPage < 1) { return 1; } else { return new Double(Math.ceil((float) this.totalRows / (float) this.rowsPerPage)).longValue(); } } /** * 현재 offset, limit에 따라서 페이지 번호? * * @return 페이지 값. (0부터 시작.) */ public long getCurrentPage() { if (this.totalRows < this.rowsPerPage || this.rowsPerPage < 1) { return 0; } else { return new Double(Math.ceil((float) this.currentOffsetRow / (float) this.rowsPerPage)).longValue(); } } public long getNextOffsetRow() { if(this.currentOffsetRow >= this.totalRows || this.currentOffsetRow + this.rowsPerPage >= this.totalRows) { return -1; } else { return this.currentOffsetRow + this.rowsPerPage; } } public long getPrevOffsetRow() { if(this.currentOffsetRow <= 0) { return -1; } else { return this.currentOffsetRow - this.rowsPerPage; } } public long getMaxOffsetRow() { return this.getMaxPage() * this.rowsPerPage; } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } @Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }