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:
mdabrowski-eu
2022-06-30 11:26:34 +02:00
committed by GitHub
parent e7dd9de149
commit 4a7de9f1d0
5 changed files with 155 additions and 4 deletions

View File

@@ -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;
}
}

View File

@@ -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));
}
}