
org.tinygroup.tinysqldsl.Pager Maven / Gradle / Ivy
/**
* Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tinygroup.tinysqldsl;
import java.io.Serializable;
import java.util.List;
/**
* 分页对象
* @author renhui
*
*/
public class Pager implements Serializable {
private static final int DEFAULT_LIMIT = 10;
private List records;//当前页的记录
private int totalCount;//总记录数
private int currentPage;//当前页
private int limit;//每页记录数
private int start;//开始记录数
private int totalPages;//总页数
public Pager(int totalCount, int start, List records) {
this(totalCount, start, DEFAULT_LIMIT, records);
}
public Pager(int totalCount, int start, int limit, List records) {
this.records = records;
init(totalCount, start, limit);
}
private void init(int totalCount, int start, int limit) {
//设置基本参数
this.totalCount = totalCount;
if (limit == 0) {
limit = DEFAULT_LIMIT;
}
if (start < 0) {//如果传人参数是小于0,那么设置为0
start = 0;
}
this.limit = limit;
this.start = start;
this.totalPages = totalCount % limit == 0 ? totalCount / limit : totalCount / limit + 1;
this.currentPage = start / limit == 0 ? 1 : start / limit + 1;
if (currentPage > totalPages) {
currentPage = totalPages;
}
}
public List getRecords() {
return records;
}
public void setRecords(List records) {
this.records = records;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
}