Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java47
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/CollectionToolsTests.java80
2 files changed, 127 insertions, 0 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java
index 48b93af4c8..4f5e27eadd 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java
@@ -9,6 +9,7 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.internal.collection;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
@@ -246,6 +247,52 @@ public final class CollectionTools {
}
+ // ********** partition **********
+
+ /**
+ * Divide the specified collection into the specified number of partitions.
+ * If the collection does not split evenly into the specified number of
+ * partitions, all the partitions will be one of two sizes; the first
+ * partions will be of size <code>collection.size()/count+1</code>,
+ * while the last partions will be of size <code>collection.size()/count</code>.
+ */
+ public static <E> ArrayList<ArrayList<E>> partition(Collection<? extends E> collection, int count) {
+ if (count <= 0) {
+ throw new IllegalArgumentException("count must be greater than zero: " + count); //$NON-NLS-1$
+ }
+ int collectionSize = collection.size();
+ if (collectionSize < count) {
+ throw new IllegalArgumentException("collection size (" + collectionSize + ") must be greater than or equal to count: " + count); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ int partitionSize = collectionSize / count;
+ int remainder = collectionSize % count;
+
+ Iterator<? extends E> stream = collection.iterator();
+ ArrayList<ArrayList<E>> result = new ArrayList<ArrayList<E>>(count);
+ if (remainder != 0) {
+ // spread the remainder elements across the first partitions
+ partitionSize++;
+ for (int i = remainder; i-- > 0; ) {
+ ArrayList<E> partition = new ArrayList<E>(partitionSize);
+ for (int j = partitionSize; j-- > 0;) {
+ partition.add(stream.next());
+ }
+ result.add(partition);
+ }
+ partitionSize--;
+ count = count - remainder;
+ }
+ for (int i = count; i-- > 0; ) {
+ ArrayList<E> partition = new ArrayList<E>(partitionSize);
+ for (int j = partitionSize; j-- > 0;) {
+ partition.add(stream.next());
+ }
+ result.add(partition);
+ }
+ return result;
+ }
+
+
// ********** remove all **********
/**
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/CollectionToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/CollectionToolsTests.java
index bb724e9240..ca3682beb6 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/CollectionToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/CollectionToolsTests.java
@@ -362,6 +362,86 @@ public class CollectionToolsTests
assertNotSame(expected, actual);
}
+ // ********** partition **********
+
+ public void testPartitionCollectionInt_1() {
+ Collection<String> c = Arrays.asList(new String[] { "zero", "one", "two", "three", "four" });
+ ArrayList<ArrayList<String>> actual = CollectionTools.partition(c, 1);
+ List<String> p0 = Arrays.asList(new String[] { "zero", "one", "two", "three", "four" });
+ assertEquals(1, actual.size());
+ assertEquals(p0, actual.get(0));
+ }
+
+ public void testPartitionCollectionInt_2() {
+ Collection<String> c = Arrays.asList(new String[] { "zero", "one", "two", "three", "four" });
+ ArrayList<ArrayList<String>> actual = CollectionTools.partition(c, 2);
+ List<String> p0 = Arrays.asList(new String[] { "zero", "one", "two" });
+ List<String> p1 = Arrays.asList(new String[] { "three", "four" });
+ assertEquals(2, actual.size());
+ assertEquals(p0, actual.get(0));
+ assertEquals(p1, actual.get(1));
+ }
+
+ public void testPartitionCollectionInt_3() {
+ Collection<String> c = Arrays.asList(new String[] { "zero", "one", "two", "three", "four" });
+ ArrayList<ArrayList<String>> actual = CollectionTools.partition(c, 3);
+ List<String> p0 = Arrays.asList(new String[] { "zero", "one" });
+ List<String> p1 = Arrays.asList(new String[] { "two", "three" });
+ List<String> p2 = Arrays.asList(new String[] { "four" });
+ assertEquals(3, actual.size());
+ assertEquals(p0, actual.get(0));
+ assertEquals(p1, actual.get(1));
+ assertEquals(p2, actual.get(2));
+ }
+
+ public void testPartitionCollectionInt_4() {
+ Collection<String> c = Arrays.asList(new String[] { "zero", "one", "two", "three", "four" });
+ ArrayList<ArrayList<String>> actual = CollectionTools.partition(c, 4);
+ List<String> p0 = Arrays.asList(new String[] { "zero", "one" });
+ List<String> p1 = Arrays.asList(new String[] { "two" });
+ List<String> p2 = Arrays.asList(new String[] { "three" });
+ List<String> p3 = Arrays.asList(new String[] { "four" });
+ assertEquals(4, actual.size());
+ assertEquals(p0, actual.get(0));
+ assertEquals(p1, actual.get(1));
+ assertEquals(p2, actual.get(2));
+ assertEquals(p3, actual.get(3));
+ }
+
+ public void testPartitionCollectionInt_5() {
+ Collection<String> c = Arrays.asList(new String[] { "zero", "one", "two", "three", "four" });
+ ArrayList<ArrayList<String>> actual = CollectionTools.partition(c, 5);
+ List<String> p0 = Arrays.asList(new String[] { "zero" });
+ List<String> p1 = Arrays.asList(new String[] { "one" });
+ List<String> p2 = Arrays.asList(new String[] { "two" });
+ List<String> p3 = Arrays.asList(new String[] { "three" });
+ List<String> p4 = Arrays.asList(new String[] { "four" });
+ assertEquals(5, actual.size());
+ assertEquals(p0, actual.get(0));
+ assertEquals(p1, actual.get(1));
+ assertEquals(p2, actual.get(2));
+ assertEquals(p3, actual.get(3));
+ assertEquals(p4, actual.get(4));
+ }
+
+ public void testPartitionCollectionInt_6() {
+ Collection<String> c = Arrays.asList(new String[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" });
+ ArrayList<ArrayList<String>> actual = CollectionTools.partition(c, 6);
+ List<String> p0 = Arrays.asList(new String[] { "zero", "one" });
+ List<String> p1 = Arrays.asList(new String[] { "two", "three" });
+ List<String> p2 = Arrays.asList(new String[] { "four", "five" });
+ List<String> p3 = Arrays.asList(new String[] { "six", "seven" });
+ List<String> p4 = Arrays.asList(new String[] { "eight" });
+ List<String> p5 = Arrays.asList(new String[] { "nine" });
+ assertEquals(6, actual.size());
+ assertEquals(p0, actual.get(0));
+ assertEquals(p1, actual.get(1));
+ assertEquals(p2, actual.get(2));
+ assertEquals(p3, actual.get(3));
+ assertEquals(p4, actual.get(4));
+ assertEquals(p5, actual.get(5));
+ }
+
// ********** remove all **********
public void testRemoveAllCollectionIterable() {

Back to the top