All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.cliveyuan.robin.base.common.Pagination Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/*
 * Copyright (c) 2020  Clive Yuan ([email protected]).
 */

package cn.cliveyuan.robin.base.common;

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

/**
 * 分页返回对象
 *
 * @author Clive Yuan
 * @date 2020/10/29
 */
public class Pagination implements Serializable {

    public static final int DEFAULT_PAGE_NUM = 1;
    public static final int DEFAULT_PAGE_SIZE = 10;
    private static final long serialVersionUID = 1L;
    /**
     * 总条数
     */
    private int totalCount;

    /**
     * 数据列表
     */
    private List dataList;

    /**
     * 是否有下一页
     */
    private boolean hasNext;

    /**
     * 总页数
     */
    private int totalPages;

    /**
     * 页码
     */
    private int pageNo;

    /**
     * 页面大小
     */
    private int pageSize;

    public Pagination() {
    }

    /**
     * 构建空分页
     *
     * @return 返回空分页
     */
    public static Pagination buildEmpty() {
        return new Pagination(0, new ArrayList<>(), DEFAULT_PAGE_NUM, DEFAULT_PAGE_SIZE);
    }

    /**
     * 通过分页构建分页
     *
     * @param dataList 数据列表
     * @param pagination 旧分页
     * @param  新分页对象
     * @param  旧分页对象
     * @return
     */
    public static  Pagination buildByPagination(List dataList, Pagination pagination) {
        return new Pagination(pagination.getTotalCount(), dataList, pagination.getPageNo(), pagination.getPageSize());
    }

    public Pagination(int totalCount, List dataList, int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.dataList = dataList;
        this.totalPages = (totalCount / pageSize) + ((totalCount % pageSize == 0) ? 0 : 1);
        this.hasNext = pageNo < this.totalPages;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public List getDataList() {
        return dataList;
    }

    public void setDataList(List dataList) {
        this.dataList = dataList;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalPages() {
        return totalPages;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pagination that = (Pagination) o;
        return totalCount == that.totalCount &&
                hasNext == that.hasNext &&
                Objects.equals(dataList, that.dataList);
    }

    @Override
    public int hashCode() {
        return Objects.hash(totalCount, dataList, hasNext);
    }

    @Override
    public String toString() {
        return "Pagination{" +
                "totalCount=" + totalCount +
                ", dataList=" + dataList +
                ", hasNext=" + hasNext +
                '}';
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy