package cn.ey.biz.module.system.util;

import cn.ey.biz.common.pojo.PageResult;

import java.util.List;
import java.util.Objects;

public class PageUtil {

    private final static int MAX_SIZE = 10000;

    public static <T> PageResult<T> getPageList(List<T> list, int pageSize, int pageNo) {
        Objects.requireNonNull(list, "list must not be null");
        if (pageSize <= 0) {
            throw new IllegalArgumentException("pageSize must greater than 0");
        }
        if (pageNo <= 0) {
            throw new IllegalArgumentException("pageNo must greater than 0");
        }
        if (pageSize > MAX_SIZE) {
            throw new IllegalArgumentException("pageSize too large: " + pageSize);
        }
        int totalCount = list.size();

        int startIndex = pageSize * (pageNo - 1);
        int endIndex = Math.min(totalCount, startIndex + pageSize);

        PageResult<T> result = new PageResult<T>(Integer.toUnsignedLong(totalCount));
        if (startIndex < totalCount) {
            result.getList().addAll(list.subList(startIndex, endIndex));
        }

        return result;
    }


}
