BAEL-5557 Split a stream into parts (#12238)
* BAEL-5557 Split a stream into parts * BAEL-5557 fix main pom.xml * BAEL-5557 fix mvn profiles * BAEL-5557 articles with equals and hashcode * BAEL-5557 migrate assertions to assertj * BAEL-5557 better assertions
This commit is contained in:
parent
103a374ba1
commit
40e42f8d45
@ -15,6 +15,16 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.reactivex</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>${rx.java.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.reactivex.rxjava2</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>${rx.java2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
@ -27,6 +37,13 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.23.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -54,8 +71,10 @@
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>12</maven.compiler.source>
|
||||
<maven.compiler.target>12</maven.compiler.target>
|
||||
<rx.java.version>1.2.5</rx.java.version>
|
||||
<rx.java2.version>2.2.2</rx.java2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,37 @@
|
||||
package splitting;
|
||||
|
||||
class Article {
|
||||
private final String target;
|
||||
private final boolean featured;
|
||||
|
||||
public Article(String target, boolean featured) {
|
||||
this.target = target;
|
||||
this.featured = featured;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public boolean isFeatured() {
|
||||
return featured;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Article article = (Article) o;
|
||||
|
||||
if (featured != article.featured) return false;
|
||||
return target.equals(article.target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = target.hashCode();
|
||||
result = 31 * result + (featured ? 1 : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package splitting;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Test;
|
||||
import rx.Observable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StreamSplittingUnitTest {
|
||||
private final static List<Article> articles = Lists.newArrayList(
|
||||
new Article("Baeldung", true),
|
||||
new Article("Baeldung", false),
|
||||
new Article("Programming Daily", false),
|
||||
new Article("The Code", false)
|
||||
);
|
||||
|
||||
@Test
|
||||
public void givenListOfArticles_shouldSplitListInTwoCategories_usingPartitioningBy() {
|
||||
Map<Boolean, List<Article>> groupedArticles = articles.stream().collect(Collectors.partitioningBy(a -> a.getTarget().equals("Baeldung")));
|
||||
|
||||
assertThat(groupedArticles.get(true)).containsExactly(
|
||||
new Article("Baeldung", true),
|
||||
new Article("Baeldung", false));
|
||||
assertThat(groupedArticles.get(false)).containsExactly(
|
||||
new Article("Programming Daily", false),
|
||||
new Article("The Code", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfArticles_shouldSplitListInMultipleCategories_usingGroupingBy() {
|
||||
Map<String, List<Article>> groupedArticles = articles.stream().collect(Collectors.groupingBy(Article::getTarget));
|
||||
|
||||
assertThat(groupedArticles.get("Baeldung")).hasSize(2);
|
||||
assertThat(groupedArticles.get("Programming Daily")).hasSize(1);
|
||||
assertThat(groupedArticles.get("The Code")).hasSize(1);
|
||||
|
||||
assertThat(groupedArticles.get("Baeldung")).containsExactly(
|
||||
new Article("Baeldung", true),
|
||||
new Article("Baeldung", false));
|
||||
assertThat(groupedArticles.get("Programming Daily")).containsExactly(new Article("Programming Daily", false));
|
||||
assertThat(groupedArticles.get("The Code")).containsExactly(new Article("The Code", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfArticles_shouldSplitListInTwoCategories_usingTeeing() {
|
||||
List<Long> countedArticles = articles.stream().collect(Collectors.teeing(
|
||||
Collectors.filtering(article -> article.getTarget().equals("Baeldung"), Collectors.counting()),
|
||||
Collectors.filtering(article -> !article.getTarget().equals("Baeldung"), Collectors.counting()),
|
||||
List::of));
|
||||
|
||||
assertThat(countedArticles.get(0)).isEqualTo(2);
|
||||
assertThat(countedArticles.get(1)).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfArticles_shouldSplitListInTwoOVerlappingCategories_usingTeeing() {
|
||||
List<List<Article>> groupedArticles = articles.stream().collect(Collectors.teeing(
|
||||
Collectors.filtering(article -> article.getTarget().equals("Baeldung"), Collectors.toList()),
|
||||
Collectors.filtering(Article::isFeatured, Collectors.toList()),
|
||||
List::of));
|
||||
|
||||
assertThat(groupedArticles.get(0)).hasSize(2);
|
||||
assertThat(groupedArticles.get(1)).hasSize(1);
|
||||
|
||||
assertThat(groupedArticles.get(0)).containsExactly(
|
||||
new Article("Baeldung", true),
|
||||
new Article("Baeldung", false));
|
||||
assertThat(groupedArticles.get(1)).containsExactly(new Article("Baeldung", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfArticles_shouldSplitStreamInMultipleCategories_usingRxJava() {
|
||||
Observable<Article> observableArticles = Observable.from(articles);
|
||||
|
||||
Observable<Article> baeldungObservable = observableArticles.filter(article -> article.getTarget().equals("Baeldung"));
|
||||
Observable<Article> featuredObservable = observableArticles.filter(Article::isFeatured);
|
||||
List<Article> baeldungArticles = new ArrayList<>();
|
||||
List<Article> featuredArticles = new ArrayList<>();
|
||||
baeldungObservable.subscribe(baeldungArticles::add);
|
||||
featuredObservable.subscribe(featuredArticles::add);
|
||||
|
||||
assertThat(baeldungArticles).hasSize(2);
|
||||
assertThat(featuredArticles).hasSize(1);
|
||||
|
||||
assertThat(baeldungArticles).containsExactly(
|
||||
new Article("Baeldung", true),
|
||||
new Article("Baeldung", false));
|
||||
assertThat(featuredArticles).containsExactly(new Article("Baeldung", true));
|
||||
}
|
||||
}
|
@ -115,7 +115,6 @@
|
||||
<module>core-java-streams</module>
|
||||
<module>core-java-streams-2</module>
|
||||
<module>core-java-streams-3</module>
|
||||
<module>core-java-streams-4</module>
|
||||
<module>core-java-string-algorithms</module>
|
||||
<module>core-java-string-algorithms-2</module>
|
||||
<module>core-java-string-apis</module>
|
||||
@ -147,4 +146,4 @@
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
1
pom.xml
1
pom.xml
@ -1238,6 +1238,7 @@
|
||||
<module>core-java-modules/core-java-io-conversions-2</module>
|
||||
<module>core-java-modules/core-java-jpms</module>
|
||||
<module>core-java-modules/core-java-os</module>
|
||||
<module>core-java-modules/core-java-streams-4</module>
|
||||
<module>core-java-modules/core-java-string-algorithms-3</module>
|
||||
<module>core-java-modules/core-java-string-operations-3</module>
|
||||
<module>core-java-modules/core-java-string-operations-4</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user