com.github.dennisit.vplus.data.page.Pagination Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.page;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Elon.su on 17/9/30.
*/
@Data
public class Pagination {
/**
* 页码数
*/
private int page;
/**
* 每页大小
*/
private int size;
/**
* 总共页码数
*/
private int pageTotal;
/**
* 库表行数
*/
private long rowTotal;
/**
* 当前页数据集
*/
private List items = Lists.newArrayList();
/**
* 默认每页数据大小
*/
public static final Integer DEFAULT_PAGE_SIZE = 20;
/**
* 查询参数(用于透传)
*/
private Map query;
public static Pagination empty(){
return new Pagination<>();
}
/**
* 无参构造
*/
public Pagination() {
this(new ArrayList(), 0, 1);
}
public Pagination(List list, long rowTotal, int page) {
setPage(list, rowTotal, page, DEFAULT_PAGE_SIZE);
}
public Pagination(List list, long rowTotal, int page, int size) {
setPage(list, rowTotal, page, size);
}
private void setPage(List list, long rowTotal, int page, int size) {
page = Math.abs(page);
size = Math.abs(size);
int pageCount = (int)(rowTotal - 1) / size + 1;
if(page >= pageCount){
page = pageCount;
}
this.setItems(list);
this.setPageTotal(pageCount);
this.setPage(page);
this.setSize(size);
this.setRowTotal(rowTotal);
}
public long getPageTotal() {
if (this.getSize() > 0) {
return ((this.getRowTotal() - 1) / this.getSize() + 1);
}
return 0;
}
public boolean isFirstPage(){
return 1 == this.getPage();
}
public boolean isLastPage(){
return this.getPage() == this.getPageTotal();
}
public boolean isNextPage() {
return ((this.getPageTotal() > 1) && (this.getPage() < this.getPageTotal()));
}
public boolean isPrePage() {
return ((this.getPageTotal() > 1) && (this.getPage() > 1));
}
@Override
public String toString(){
return JSON.toJSONString(this);
}
}