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

com.github.sseserver.util.PageInfo Maven / Gradle / Ivy

package com.github.sseserver.util;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PageInfo implements Serializable, Iterable {
    private static final long serialVersionUID = 1L;

    private long total;
    private int pageNum;
    private int pageSize;
    private List list;
    private boolean timeout;

    public PageInfo() {
        this(new ArrayList<>());
    }

    public PageInfo(List list) {
        this.list = Objects.requireNonNull(list);
        this.total = list.size();
    }

    public static  PageInfo empty() {
        return PageInfo.of(Collections.emptyList());
    }

    public static  PageInfo timeout() {
        PageInfo info = PageInfo.of(Collections.emptyList());
        info.setTimeout(true);
        return info;
    }

    public static  PageInfo of(List list, int pageNum, int pageSize) {
        return of(list, pageNum, pageSize, false);
    }

    /**
     * 分页
     *
     * @param list
     * @param pageNum
     * @param pageSize
     * @param copy     是否复制. true=返回复制的集合. false=不复制(引用原始对象)
     * @param 
     * @return 分好页的数据
     */
    public static  PageInfo of(List list, int pageNum, int pageSize, boolean copy) {
        int total = list.size();
        List sublist;
        if (pageSize <= 0) {
            sublist = Collections.emptyList();
        } else {
            int offsetBegin = (Math.max(pageNum, 1) - 1) * pageSize;
            int offsetEnd = Math.min(offsetBegin + pageSize, total);
            if (offsetBegin == 1 && offsetEnd == total) {
                sublist = list;
            } else {
                sublist = list.subList(Math.min(offsetBegin, total), offsetEnd);
                if (sublist.isEmpty()) {
                    sublist = Collections.emptyList();
                }
            }
            if (copy) {
                sublist = new ArrayList<>(sublist);
            }
        }

        PageInfo pageInfo = new PageInfo<>();
        pageInfo.setList(sublist);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    public static  PageInfo of(List source, Function map) {
        PageInfo target = new PageInfo<>(source);
        target.setList(source.stream()
                .map(map)
                .collect(Collectors.toList()));
        return target;
    }

    public static  PageInfo of(List list) {
        return new PageInfo<>(list);
    }

    public static  PageInfo of(PageInfo source, Function map) {
        PageInfo target = new PageInfo<>();
        target.pageNum = source.pageNum;
        target.pageSize = source.pageSize;
        target.total = source.total;
        target.setList(source.list.stream().map(map).collect(Collectors.toList()));
        return target;
    }

    public  PageInfo map(Function map) {
        return of(this, map);
    }

    public long getTotal() {
        return total;
    }

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

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

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

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public boolean isTimeout() {
        return timeout;
    }

    public void setTimeout(boolean timeout) {
        this.timeout = timeout;
    }

    @Override
    public Iterator iterator() {
        if (list == null) {
            return Collections.emptyIterator();
        } else {
            return list.iterator();
        }
    }

    public Stream stream() {
        return list == null ? Stream.empty() : list.stream();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy