Skip to content

Instantly share code, notes, and snippets.

@nikhilnanivadekar
Last active September 15, 2019 17:00
Show Gist options
  • Save nikhilnanivadekar/89aed553101b9c06ec0e83234e491a74 to your computer and use it in GitHub Desktop.
Save nikhilnanivadekar/89aed553101b9c06ec0e83234e491a74 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.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.multimap.list.ListMultimap;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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()
{
ListMultimap<String, HarryPotterQuotes> bookwiseHarryPotterQuotes = harryPotterQuotes.groupBy(HarryPotterQuotes::getBook);
Verify.assertSize(2, bookwiseHarryPotterQuotes.get("Sorcerer's Stone"));
Verify.assertSize(1, bookwiseHarryPotterQuotes.get("Chamber of Secrets"));
Verify.assertSize(0, bookwiseHarryPotterQuotes.get("Prizoner of Azkaban"));
Verify.assertSize(3, bookwiseHarryPotterQuotes.get("Goblet of Fire"));
Verify.assertSize(1, bookwiseHarryPotterQuotes.get("Order of the Phoenix"));
Verify.assertSize(1, bookwiseHarryPotterQuotes.get("Half-Blood Prince"));
Verify.assertSize(2, bookwiseHarryPotterQuotes.get("Deathly Hallows"));
}
@Test
public void harry_potter_quotes_per_book_character()
{
ListMultimap<String, HarryPotterQuotes> bookCharacterwiseHarryPotterQuotes = harryPotterQuotes.groupBy(HarryPotterQuotes::getBookCharacter);
Verify.assertSize(6, bookCharacterwiseHarryPotterQuotes.get("Albus Dumbledore"));
Verify.assertSize(1, bookCharacterwiseHarryPotterQuotes.get("Rubeus Hagrid"));
Verify.assertSize(1, bookCharacterwiseHarryPotterQuotes.get("Sirius Black"));
Verify.assertSize(1, bookCharacterwiseHarryPotterQuotes.get("Luna Lovegood"));
Verify.assertSize(1, bookCharacterwiseHarryPotterQuotes.get("Severus Snape"));
}
@Test
public void harry_potter_quotes_top_book()
{
Bag<String> bookwiseHarryPotterQuotes = harryPotterQuotes.countBy(HarryPotterQuotes::getBook);
Assert.assertEquals(
"Book with most quotes",
"Goblet of Fire",
bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getOne());
Assert.assertEquals(
"Number of Quotes from the book",
3,
bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getTwo());
}
@Test
public void harry_potter_quotes_top_book_character()
{
Bag<String> bookwiseHarryPotterQuotes = harryPotterQuotes.countBy(HarryPotterQuotes::getBookCharacter);
Assert.assertEquals(
"Book character with most quotes",
"Albus Dumbledore",
bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getOne());
Assert.assertEquals(
"Number of quotes from the Book character",
6,
bookwiseHarryPotterQuotes.topOccurrences(1).getOnly().getTwo());
}
@Test
public void harry_potter_quotes_least_book_characters_with_this_awesome_framework_called_Ecipse_Collections()
{
Bag<String> bookwiseHarryPotterQuotes = harryPotterQuotes.countBy(HarryPotterQuotes::getBookCharacter);
ListIterable<ObjectIntPair<String>> leastCharacterOccurrences = bookwiseHarryPotterQuotes.bottomOccurrences(1);
SoftAssertions soft = new SoftAssertions();
soft.assertThat(leastCharacterOccurrences.collect(ObjectIntPair::getTwo).distinct().getOnly()).isEqualTo(1);
soft.assertThat(leastCharacterOccurrences.collect(ObjectIntPair::getOne))
.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