aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/MiscUtils.java
blob: 122c4eb16fa18ae0ee3c1fe17b436d70cb25058b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cgeo.geocaching.utils;

import org.apache.commons.collections4.iterators.IteratorIterable;
import org.apache.commons.lang3.NotImplementedException;

import java.util.Iterator;
import java.util.List;

final public class MiscUtils {

    private MiscUtils() {}  // Do not instantiate

    public static <T> Iterable<List<T>> buffer(final List<T> original, final int n) {
        if (n <= 0) {
            throw new IllegalArgumentException("buffer size must be positive");
        }
        return new IteratorIterable<List<T>>(new Iterator<List<T>>() {
            final int size = original.size();
            int next = 0;

            @Override
            public boolean hasNext() {
                return next < size;
            }

            @Override
            public List<T> next() {
                final List<T> result = original.subList(next, Math.min(next + n, size));
                next += n;
                return result;
            }

            @Override
            public void remove() {
                throw new NotImplementedException("remove");
            }
        });
    }

}