aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-04-13 21:21:06 +0200
committerSamuel Tardieu <sam@rfc1149.net>2014-04-13 21:21:06 +0200
commit8cd9b2afb2f620bb3c2a0df565f8d9ff483fcfaf (patch)
treeb20d5077c0d5ab8e801fc3908ef8c0943366a36c /main/src/cgeo/geocaching
parente1832f8943f53a50dd6cd50482753779e12cd31a (diff)
downloadcgeo-8cd9b2afb2f620bb3c2a0df565f8d9ff483fcfaf.zip
cgeo-8cd9b2afb2f620bb3c2a0df565f8d9ff483fcfaf.tar.gz
cgeo-8cd9b2afb2f620bb3c2a0df565f8d9ff483fcfaf.tar.bz2
buffer lets you split a list into smaller max-size ones
Diffstat (limited to 'main/src/cgeo/geocaching')
-rw-r--r--main/src/cgeo/geocaching/utils/MiscUtils.java40
1 files changed, 40 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/utils/MiscUtils.java b/main/src/cgeo/geocaching/utils/MiscUtils.java
new file mode 100644
index 0000000..122c4eb
--- /dev/null
+++ b/main/src/cgeo/geocaching/utils/MiscUtils.java
@@ -0,0 +1,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");
+ }
+ });
+ }
+
+}