java 带有列表分页符的ListUtils

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 带有列表分页符的ListUtils相关的知识,希望对你有一定的参考价值。


package ***;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {

    private ListUtils() {
        // no instanciable
    }

     public static <T> List<T> paginateList(
            final List<T> sourceList, final Integer max,
            final Integer offset) {

         final List<T> filteredItems;

         if (sourceList != null || !sourceList.isEmpty()) {

             final int fromIndex =
                     calculateFromIndex(offset, sourceList.size());
             final int toIndex =
                     calculateToIndex(sourceList, max, fromIndex);

             // Getting filtered elements
             filteredItems = sourceList.subList(fromIndex, toIndex);

         } else if (sourceList != null) {
             filteredItems = new ArrayList<T>();
         } else {
             filteredItems = null;
         }

         return filteredItems;
    }

    private static int calculateFromIndex(final int offset, final int count) {
        final int fromIndex;

        if (offset > 0) {
            if (offset < count) {
                fromIndex = offset;
            } else {
                fromIndex = count;
            }
        } else {
            fromIndex = 0;
        }
        return fromIndex;
    }

    private static <T> int calculateToIndex(
                                 final List<? extends T> sourceList,
                                 final Integer max, final int fromIndex) {
        final int toIndex;

        if (max != null) {
            final int endCalculatedPosition = max + fromIndex;

            if (endCalculatedPosition < sourceList.size()) {
                toIndex = endCalculatedPosition;
            } else {
                toIndex = sourceList.size();
            }
        } else {
            toIndex = sourceList.size();
        }

        return toIndex > fromIndex ? toIndex : fromIndex;
    }


}

以上是关于java 带有列表分页符的ListUtils的主要内容,如果未能解决你的问题,请参考以下文章

打印带有强制分页符的表格

如何在Word中插入分页符

WORD中怎样设置分页符

页面设置中“分页符”和“下一页”的区别是啥?

如何避免报表中的分页:创建没有分页符的报表?

允许其接受分页符的 NETSuite PDF 模板都有哪些要求?