Skip to content

Instantly share code, notes, and snippets.

@JosePaumard
Created September 15, 2019 16:35
Show Gist options
  • Save JosePaumard/8d22c34f515213939545750253574b3a to your computer and use it in GitHub Desktop.
Save JosePaumard/8d22c34f515213939545750253574b3a to your computer and use it in GitHub Desktop.
import org.assertj.core.api.SoftAssertions;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.multimap.list.ListMultimap;
import org.eclipse.collections.impl.factory.Lists;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.*;
public class BagMultimapDemoTest {
// Quotes sourced from: https://time.com/4607327/harry-potter-inspirational-quotes/
private MutableList<HarryPotterQuotes> harryPotterQuotes;
@Before
public void setup() {
harryPotterQuotes = Lists.mutable.with(
new HarryPotterQuotes("It does not do to dwell on dreams and forget to live", "Sorcerer's Stone", "Albus Dumbledore"),
new HarryPotterQuotes("It takes a great deal of bravery to stand up to our enemies, but just as much to stand up to our friends", "Sorcerer's Stone", "Albus Dumbledore"),
new HarryPotterQuotes("It is our choices, Harry, that show what we truly are, far more than our abilities", "Chamber of Secrets", "Albus Dumbledore"),
new HarryPotterQuotes("I am what I am an’ I’m not ashamed", "Goblet of Fire", "Rubeus Hagrid"),
new HarryPotterQuotes("If you want to know what a man’s like, take a good look at how he treats his inferiors, not his equals", "Goblet of Fire", "Sirius Black"),
new HarryPotterQuotes("Differences of habit and language are nothing at all if our aims are identical and our hearts are open", "Goblet of Fire", "Albus Dumbledore"),
new HarryPotterQuotes("Things we lose have a way of coming back to us in the end, if not always in the way we expect", "Order of the Phoenix", "Luna Lovegood"),
new HarryPotterQuotes("I am not worried, Harry...I am with you", "Half-Blood Prince", "Albus Dumbledore"),
new HarryPotterQuotes("Always", "Deathly Hallows", "Severus Snape"),
new HarryPotterQuotes("Of course it is happening inside your head, Harry, but why on earth should that mean it is not real?", "Deathly Hallows", "Albus Dumbledore"));
}
@Test
public void harry_potter_quotes_per_book_with_this_awesome_framework_called_Ecipse_Collections() {
ListMultimap<String, HarryPotterQuotes> bookwiseHarryPotterQuotes = harryPotterQuotes.groupBy(HarryPotterQuotes::getBook);
SoftAssertions soft = new SoftAssertions();
soft.assertThat(bookwiseHarryPotterQuotes.get("Sorcerer's Stone")).hasSize(2);
soft.assertThat(bookwiseHarryPotterQuotes.get("Chamber of Secrets")).hasSize(1);
soft.assertThat(bookwiseHarryPotterQuotes.get("Prizoner of Azkaban")).hasSize(0);
soft.assertThat(bookwiseHarryPotterQuotes.get("Goblet of Fire")).hasSize(3);
soft.assertThat(bookwiseHarryPotterQuotes.get("Order of the Phoenix")).hasSize(1);
soft.assertThat(bookwiseHarryPotterQuotes.get("Half-Blood Prince")).hasSize(1);
soft.assertThat(bookwiseHarryPotterQuotes.get("Deathly Hallows")).hasSize(2);
soft.assertAll();
}
@Test
public void harry_potter_quotes_per_book_with_this_amazing_Stream_API() {
var bookwiseHarryPotterQuotes =
harryPotterQuotes.stream().collect(groupingBy(HarryPotterQuotes::getBook, counting()));
SoftAssertions soft = new SoftAssertions();
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Sorcerer's Stone", 0L)).isEqualTo(2L);
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Chamber of Secrets", 0L)).isEqualTo(1L);
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Prizoner of Azkaban", 0L)).isEqualTo(0L);
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Goblet of Fire", 0L)).isEqualTo(3L);
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Order of the Phoenix", 0L)).isEqualTo(1L);
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Half-Blood Prince", 0L)).isEqualTo(1L);
soft.assertThat(bookwiseHarryPotterQuotes.getOrDefault("Deathly Hallows", 0L)).isEqualTo(2L);
soft.assertAll();
}
@Test
public void harry_potter_quotes_per_book_character_with_this_awesome_framework_called_Ecipse_Collections() {
ListMultimap<String, HarryPotterQuotes> bookCharacterwiseHarryPotterQuotes = harryPotterQuotes.groupBy(HarryPotterQuotes::getBookCharacter);
SoftAssertions soft = new SoftAssertions();
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Albus Dumbledore")).hasSize(6);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Rubeus Hagrid")).hasSize(1);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Sirius Black")).hasSize(1);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Luna Lovegood")).hasSize(1);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Severus Snape")).hasSize(1);
soft.assertAll();
}
@Test
public void harry_potter_quotes_per_book_character_with_this_amazing_Stream_API() {
var bookCharacterwiseHarryPotterQuotes =
harryPotterQuotes.stream().collect(groupingBy(HarryPotterQuotes::getBookCharacter, counting()));
SoftAssertions soft = new SoftAssertions();
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Albus Dumbledore")).isEqualTo(6L);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Rubeus Hagrid")).isEqualTo(1L);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Sirius Black")).isEqualTo(1L);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Luna Lovegood")).isEqualTo(1L);
soft.assertThat(bookCharacterwiseHarryPotterQuotes.get("Severus Snape")).isEqualTo(1L);
soft.assertAll();
}
@Test
public void harry_potter_quotes_top_book_with_this_awesome_framework_called_Ecipse_Collections() {
Bag<String> bookwiseHarryPotterQuotes = harryPotterQuotes.countBy(HarryPotterQuotes::getBook);
SoftAssertions soft = new SoftAssertions();
soft.assertThat(bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getOne()).isEqualTo("Goblet of Fire");
soft.assertThat(bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getTwo()).isEqualTo(3);
soft.assertAll();
}
@Test
public void harry_potter_quotes_top_book_with_this_amazing_Stream_API() {
var bookwiseHarryPotterQuotes =
harryPotterQuotes.stream().collect(groupingBy(HarryPotterQuotes::getBook, counting()));
var titleOccurence = bookwiseHarryPotterQuotes.entrySet().stream()
.max(Map.Entry.comparingByValue()).orElseThrow();
SoftAssertions soft = new SoftAssertions();
soft.assertThat(titleOccurence.getKey()).isEqualTo("Goblet of Fire");
soft.assertThat(titleOccurence.getValue()).isEqualTo(3L);
soft.assertAll();
}
@Test
public void harry_potter_quotes_top_book_character_with_this_awesome_framework_called_Ecipse_Collections() {
Bag<String> bookwiseHarryPotterQuotes = harryPotterQuotes.countBy(HarryPotterQuotes::getBookCharacter);
SoftAssertions soft = new SoftAssertions();
soft.assertThat(bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getOne()).isEqualTo("Albus Dumbledore");
soft.assertThat(bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getTwo()).isEqualTo(6);
soft.assertAll();
}
@Test
public void harry_potter_quotes_top_book_character_with_this_amazing_Stream_API() {
var bookwiseHarryPotterCharacter =
harryPotterQuotes.stream().collect(groupingBy(HarryPotterQuotes::getBookCharacter, counting()));
var characterOccurences = bookwiseHarryPotterCharacter.entrySet().stream()
.max(Map.Entry.comparingByValue()).orElseThrow();
SoftAssertions soft = new SoftAssertions();
soft.assertThat(characterOccurences.getKey()).isEqualTo("Albus Dumbledore");
soft.assertThat(characterOccurences.getValue()).isEqualTo(6L);
soft.assertAll();
}
@Test
public void harry_potter_quotes_least_book_characters_with_this_amazing_Stream_API() {
var bookwiseHarryPotterCharacter =
harryPotterQuotes.stream().collect(groupingBy(HarryPotterQuotes::getBookCharacter, counting()));
var characterOccurences = bookwiseHarryPotterCharacter.entrySet().stream()
.collect(groupingBy(Map.Entry::getValue, mapping(Map.Entry::getKey, toList())));
var leastCharacterOccurences =
characterOccurences.entrySet().stream()
.min(Map.Entry.comparingByKey()).orElseThrow();
SoftAssertions soft = new SoftAssertions();
soft.assertThat(leastCharacterOccurences.getKey()).isEqualTo(1L);
soft.assertThat(leastCharacterOccurences.getValue())
.containsExactlyInAnyOrder("Rubeus Hagrid", "Luna Lovegood", "Sirius Black", "Severus Snape");
soft.assertAll();
}
private class HarryPotterQuotes {
private final String quote;
private final String book;
private final String bookCharacter;
public HarryPotterQuotes(String quote, String book, String bookCharacter) {
this.quote = quote;
this.book = book;
this.bookCharacter = bookCharacter;
}
public String getQuote() {
return quote;
}
public String getBook() {
return book;
}
public String getBookCharacter() {
return bookCharacter;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment