diff --git a/guava/pom.xml b/guava/pom.xml index f853a4d36a..f9994defec 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -16,7 +16,13 @@ guava 15.0 - + + + org.apache.commons + commons-collections4 + 4.0 + + @@ -110,7 +116,7 @@ 4.3.1 2.1.0 - + 3.1 2.4 diff --git a/guava/src/test/java/org/baeldung/java/CollectionApachePartitionUnitTest.java b/guava/src/test/java/org/baeldung/java/CollectionApachePartitionUnitTest.java new file mode 100644 index 0000000000..1071d4a5fb --- /dev/null +++ b/guava/src/test/java/org/baeldung/java/CollectionApachePartitionUnitTest.java @@ -0,0 +1,43 @@ +package org.baeldung.java; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.apache.commons.collections4.ListUtils; +import org.junit.Test; + +import com.google.common.collect.Lists; + +public class CollectionApachePartitionUnitTest { + + // tests - apache common collections + + @Test + public final void givenList_whenParitioningIntoNSublists_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final List> subSets = ListUtils.partition(intList, 3); + + // When + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(3)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + } + + @Test + public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChange() { + // Given + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + final List> subSets = ListUtils.partition(intList, 3); + + // When + intList.add(9); + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8, 9); + assertThat(lastPartition, equalTo(expectedLastPartition)); + } + +} diff --git a/guava/src/test/java/org/baeldung/java/CollectionGuavaPartitionUnitTest.java b/guava/src/test/java/org/baeldung/java/CollectionGuavaPartitionUnitTest.java new file mode 100644 index 0000000000..c4ddb35da2 --- /dev/null +++ b/guava/src/test/java/org/baeldung/java/CollectionGuavaPartitionUnitTest.java @@ -0,0 +1,56 @@ +package org.baeldung.java; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.util.Collection; +import java.util.List; + +import org.junit.Test; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; + +public class CollectionGuavaPartitionUnitTest { + + // tests - guava + + @Test + public final void givenList_whenParitioningIntoNSublists_thenCorrect() { + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final List> subSets = Lists.partition(intList, 3); + + // When + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8); + assertThat(subSets.size(), equalTo(3)); + assertThat(lastPartition, equalTo(expectedLastPartition)); + } + + @Test + public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChangeAsWell() { + // Given + final List intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + final List> subSets = Lists.partition(intList, 3); + + // When + intList.add(9); + final List lastPartition = subSets.get(2); + final List expectedLastPartition = Lists. newArrayList(7, 8, 9); + assertThat(lastPartition, equalTo(expectedLastPartition)); + } + + @Test + public final void givenCollection_whenParitioningIntoNSublists_thenCorrect() { + final Collection intCollection = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); + + final Iterable> subSets = Iterables.partition(intCollection, 3); + + // When + final List firstPartition = subSets.iterator().next(); + final List expectedLastPartition = Lists. newArrayList(1, 2, 3); + assertThat(firstPartition, equalTo(expectedLastPartition)); + } + +}