JAVA-2154: Create guava-parent module and organize guava modules (#9782)

* JAVA-2154: Removed module, now split into guava-utilities and guava-core

* JAVA-2154: Removed module, articles moved to new module guava-core

* JAVA-2154: Moved module inside guava-modules

* JAVA-2154: Moved module inside guava-modules

* JAVA-2154: Moved module inside guava-modules

* JAVA-2154: Moved module inside guava-modules

* JAVA-2154: Moved 1 article to guava-collections

* JAVA-2154: New module guava-collections-list

* JAVA-2154: New module guava-core

* JAVA-2154: New module guava-utilities

* JAVA-2154: Updated README, removed extra article reference

* JAVA-2154: parent module pom changes

* JAVA-2154: main pom changes - removed guava related modules as they have
been shifted inside guava-modules

* JAVA-2154: rearranged and moved module inside guava-modules folder
This commit is contained in:
Sampada
2020-07-28 17:34:46 +05:30
committed by GitHub
parent 0423536cf4
commit 0cade733ca
100 changed files with 184 additions and 79 deletions
@@ -0,0 +1,8 @@
## Guava Collections List examples
This module contains articles about list collections in Guava
### Relevant Articles:
- [Partition a List in Java](https://www.baeldung.com/java-list-split)
- [Guava Lists](https://www.baeldung.com/guava-lists)
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>guava-collections-list</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>guava-collections-list</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>guava-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<dependencies>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>${java-hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>guava-collections</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<properties>
<!-- util -->
<commons-collections4.version>4.1</commons-collections4.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
<junit-jupiter.version>5.6.2</junit-jupiter.version>
</properties>
</project>
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,82 @@
package com.baeldung.guava.lists;
import com.google.common.base.Predicates;
import com.google.common.collect.*;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.contains;
public class GuavaListsUnitTest {
@Test
public void whenCreateList_thenCreated() {
final List<String> names = Lists.newArrayList("John", "Adam", "Jane");
names.add("Tom");
assertEquals(4, names.size());
names.remove("Adam");
assertThat(names, contains("John", "Jane", "Tom"));
}
@Test
public void whenReverseList_thenReversed() {
final List<String> names = Lists.newArrayList("John", "Adam", "Jane");
final List<String> reversed = Lists.reverse(names);
assertThat(reversed, contains("Jane", "Adam", "John"));
}
@Test
public void whenCreateCharacterListFromString_thenCreated() {
final List<Character> chars = Lists.charactersOf("John");
assertEquals(4, chars.size());
assertThat(chars, contains('J', 'o', 'h', 'n'));
}
@Test
public void whenPartitionList_thenPartitioned() {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom", "Viki", "Tyler");
final List<List<String>> result = Lists.partition(names, 2);
assertEquals(3, result.size());
assertThat(result.get(0), contains("John", "Jane"));
assertThat(result.get(1), contains("Adam", "Tom"));
assertThat(result.get(2), contains("Viki", "Tyler"));
}
@Test
public void whenRemoveDuplicatesFromList_thenRemoved() {
final List<Character> chars = Lists.newArrayList('h', 'e', 'l', 'l', 'o');
assertEquals(5, chars.size());
final List<Character> result = ImmutableSet.copyOf(chars).asList();
assertThat(result, contains('h', 'e', 'l', 'o'));
}
@Test
public void whenRemoveNullFromList_thenRemoved() {
final List<String> names = Lists.newArrayList("John", null, "Adam", null, "Jane");
Iterables.removeIf(names, Predicates.isNull());
assertEquals(3, names.size());
assertThat(names, contains("John", "Adam", "Jane"));
}
@Test
public void whenCreateImmutableList_thenCreated() {
final List<String> names = Lists.newArrayList("John", "Adam", "Jane");
names.add("Tom");
assertEquals(4, names.size());
final ImmutableList<String> immutable = ImmutableList.copyOf(names);
assertThat(immutable, contains("John", "Adam", "Jane", "Tom"));
}
}
@@ -0,0 +1,43 @@
package com.baeldung.guava.partition;
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<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = ListUtils.partition(intList, 3);
// When
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
@Test
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChange() {
// Given
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = ListUtils.partition(intList, 3);
// When
intList.add(9);
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
assertThat(lastPartition, equalTo(expectedLastPartition));
}
}
@@ -0,0 +1,56 @@
package com.baeldung.guava.partition;
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<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = Lists.partition(intList, 3);
// When
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
@Test
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChangeAsWell() {
// Given
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final List<List<Integer>> subSets = Lists.partition(intList, 3);
// When
intList.add(9);
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
assertThat(lastPartition, equalTo(expectedLastPartition));
}
@Test
public final void givenCollection_whenParitioningIntoNSublists_thenCorrect() {
final Collection<Integer> intCollection = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final Iterable<List<Integer>> subSets = Iterables.partition(intCollection, 3);
// When
final List<Integer> firstPartition = subSets.iterator().next();
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(1, 2, 3);
assertThat(firstPartition, equalTo(expectedLastPartition));
}
}
@@ -0,0 +1,70 @@
package com.baeldung.guava.partition;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Test;
import com.google.common.collect.Lists;
public class CollectionJavaPartitionUnitTest {
// java8 groupBy
@Test
public final void givenList_whenParitioningIntoNSublistsUsingGroupingBy_thenCorrect() {
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final Map<Integer, List<Integer>> groups = intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3));
final List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
// When
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
// intList.add(9);
// System.out.println(groups.values());
}
// java8 partitionBy
@Test
public final void givenList_whenParitioningIntoSublistsUsingPartitionBy_thenCorrect() {
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
final Map<Boolean, List<Integer>> groups = intList.stream().collect(Collectors.partitioningBy(s -> s > 6));
final List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
// When
final List<Integer> lastPartition = subSets.get(1);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(2));
assertThat(lastPartition, equalTo(expectedLastPartition));
// intList.add(9);
// System.out.println(groups.values());
}
// java8 split by separator
@Test
public final void givenList_whenSplittingBySeparator_thenCorrect() {
final List<Integer> intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8);
final int[] indexes = Stream.of(IntStream.of(-1), IntStream.range(0, intList.size()).filter(i -> intList.get(i) == 0), IntStream.of(intList.size())).flatMapToInt(s -> s).toArray();
final List<List<Integer>> subSets = IntStream.range(0, indexes.length - 1).mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1])).collect(Collectors.toList());
// When
final List<Integer> lastPartition = subSets.get(2);
final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
assertThat(subSets.size(), equalTo(3));
assertThat(lastPartition, equalTo(expectedLastPartition));
}
}