Java List Slice sliceList(final List list, final long offset, final int limit)

Here you can find the source of sliceList(final List list, final long offset, final int limit)

Description

slice List

License

Open Source License

Declaration

public static final List<Long> sliceList(final List<Long> list, final long offset, final int limit)
            throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

import java.util.ArrayList;

public class Main {
    public static final List<Long> sliceList(final List<Long> list, final long offset, final int limit)
            throws IllegalArgumentException {
        checkList(list, Integer.MAX_VALUE);
        int len = list.size();

        if (offset > len) {
            return new ArrayList<Long>(0);
        }/*from w  w w. j  a v a2  s .  c o  m*/
        return list.subList((int) offset, min((int) (offset + limit), len));
    }

    public static final void checkList(List list, int limit) {
        if (list == null) {
            throw new IllegalArgumentException("Null list");
        }

        int len = list.size();
        if (len == 0) {
            throw new IllegalArgumentException("Empty list (size=0)");
        }
        if (len > limit) {
            throw new IllegalArgumentException("List too large " + len + "; greater than limit: " + limit);
        }
    }

    public static final int min(final int i, final int j) {
        if (j > i) {
            return i;
        }
        return j;
    }
}

Related

  1. slice(List as, int start, int end)
  2. slice(List c, int fromIndex, int toIndex)
  3. slice(List list, int index, int count)
  4. slice(List list, int start, int end)
  5. slice(List list, Integer start, Integer stop)
  6. sliceListByIndices(final List indices, final List list)
  7. slices(List list, int step)