分享一个基本的分页模板(类似的分页都有工具,注意思想)

public class BasePage<E> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer pageNo;
    private Integer pageSize;
    private Long total;
    private Collection<E> list;

    public BasePage() {
    }

    public BasePage(Integer pageNo, Integer pageSize, Long total, Collection<E> list) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
    }

    public BasePage(Collection<E> coll) {
        if (coll != null && !(coll instanceof EmptyPage)) {
            if (coll instanceof Page) {
                Page<E> page = (Page)coll;
                this.pageNo = page.getPageNum() == 0 ? 1 : page.getPageNum();
                this.pageSize = page.getPageSize();
                this.total = page.getTotal();
                this.list = page;
            } else {
                this.pageNo = 1;
                this.pageSize = coll.size();
                this.total = (long)coll.size();
                this.list = coll;
            }

        } else {
            this.pageNo = 1;
            this.pageSize = coll == null ? 0 : ((EmptyPage)coll).getPageSize();
            this.total = 0L;
            this.list = new ArrayList(0);
        }
    }

    public <S> BasePage(Collection<S> coll, Function<Collection<S>, Collection<E>> mapper) {
        if (coll != null && !(coll instanceof EmptyPage)) {
            if (coll instanceof Page) {
                Page<E> page = (Page)coll;
                this.pageNo = page.getPageNum() == 0 ? 1 : page.getPageNum();
                this.pageSize = page.getPageSize();
                this.total = page.getTotal();
            } else {
                this.pageNo = 1;
                this.pageSize = coll.size();
                this.total = (long)coll.size();
            }

            this.list = (Collection)mapper.apply(coll);
        } else {
            this.pageNo = 1;
            this.pageSize = coll == null ? 0 : ((EmptyPage)coll).getPageSize();
            this.total = 0L;
            this.list = new ArrayList(0);
        }
    }

    public Integer getPageNo() {
        return this.pageNo;
    }

    public Integer getPageSize() {
        return this.pageSize;
    }

    public Long getTotal() {
        return this.total;
    }

    public Collection<E> getList() {
        return this.list;
    }

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

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

    public void setTotal(final Long total) {
        this.total = total;
    }

    public void setList(final Collection<E> list) {
        this.list = list;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BasePage)) {
            return false;
        } else {
            BasePage<?> other = (BasePage)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Integer pageSize;
                Integer otherPageSize;
                label51: {
                    pageSize = this.getPageNo();
                    otherPageSize = other.getPageNo();
                    if (pageSize == null) {
                        if (otherPageSize == null) {
                            break label51;
                        }
                    } else if (pageSize.equals(otherPageSize)) {
                        break label51;
                    }

                    return false;
                }

                pageSize = this.getPageSize();
                otherPageSize = other.getPageSize();
                if (pageSize == null) {
                    if (otherPageSize != null) {
                        return false;
                    }
                } else if (!pageSize.equals(otherPageSize)) {
                    return false;
                }

                Object total = this.getTotal();
                Object otherTotal = other.getTotal();
                if (total == null) {
                    if (otherTotal != null) {
                        return false;
                    }
                } else if (!total.equals(otherTotal)) {
                    return false;
                }

                Object list = this.getList();
                Object otherList = other.getList();
                if (list == null) {
                    if (otherList != null) {
                        return false;
                    }
                } else if (!list.equals(otherList)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BasePage;
    }

    @Override
    public int hashCode() {
        int result = 1;
        Object pageNo = this.getPageNo();
        result = result * 59 + (pageNo == null ? 43 : pageNo.hashCode());
        Object pageSize = this.getPageSize();
        result = result * 59 + (pageSize == null ? 43 : pageSize.hashCode());
        Object total = this.getTotal();
        result = result * 59 + (total == null ? 43 : total.hashCode());
        Object list = this.getList();
        result = result * 59 + (list == null ? 43 : list.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "BasePage(pageNo=" + this.getPageNo() + ", pageSize=" + this.getPageSize() + ", total=" + this.getTotal() + ", list=" + this.getList() + ")";
    }
}

 

posted @ 2023-02-24 17:24  小新超人  阅读(49)  评论(0)    收藏  举报