From 8bf825d0462d45b4b8d0704cf8fe365420f69f9f Mon Sep 17 00:00:00 2001 From: adamd1985 Date: Mon, 25 Sep 2017 20:24:24 +0200 Subject: [PATCH] Code examples for the articl on Apache bags (#2671) --- .../commons/collections4/BagTests.java | 182 ++++++++++-------- 1 file changed, 104 insertions(+), 78 deletions(-) diff --git a/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java b/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java index 4ce250d979..55fadcbf85 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java +++ b/libraries/src/test/java/com/baeldung/commons/collections4/BagTests.java @@ -1,85 +1,111 @@ package com.baeldung.commons.collections4; -import java.util.ArrayList; -import java.util.List; - import org.apache.commons.collections4.Bag; -import org.apache.commons.collections4.bag.CollectionBag; -import org.apache.commons.collections4.bag.HashBag; -import org.apache.commons.collections4.bag.TreeBag; -import org.junit.Assert; -import org.junit.Before; +import org.apache.commons.collections4.SortedBag; +import org.apache.commons.collections4.bag.*; import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; + public class BagTests { - - Bag baseBag; - TreeBag treeBag; - - @Before - public void before() { - baseBag = new HashBag(); - treeBag = new TreeBag(); - treeBag = new TreeBag(); - } - - @Test - public void whenAdd_thenRemoveFromBaseBag_thenContainsCorrect() { - baseBag.add("apple", 2); - baseBag.add("lemon", 6); - baseBag.add("lime"); - - baseBag.remove("lemon"); - Assert.assertEquals(3, baseBag.size()); - Assert.assertFalse(baseBag.contains("lemon")); - - Assert.assertTrue(baseBag.uniqueSet().contains("apple")); - - List containList = new ArrayList(); - containList.add("apple"); - containList.add("lemon"); - containList.add("lime"); - Assert.assertFalse(baseBag.containsAll(containList)); - } - - @Test - public void whenAdd_thenRemoveFromBaseCollectionBag_thenContainsCorrect() { - baseBag.add("apple", 2); - baseBag.add("lemon", 6); - baseBag.add("lime"); - - CollectionBag baseCollectionBag = new CollectionBag( - baseBag); - - baseCollectionBag.remove("lemon"); - Assert.assertEquals(8, baseCollectionBag.size()); - Assert.assertTrue(baseCollectionBag.contains("lemon")); - - baseCollectionBag.remove("lemon",1); - Assert.assertEquals(7, baseCollectionBag.size()); - - Assert.assertTrue(baseBag.uniqueSet().contains("apple")); - - List containList = new ArrayList(); - containList.add("apple"); - containList.add("lemon"); - containList.add("lime"); - Assert.assertTrue(baseBag.containsAll(containList)); - } - - @Test - public void whenAddtoTreeBag_thenRemove_thenContainsCorrect() { - treeBag.add("banana", 8); - treeBag.add("apple", 2); - treeBag.add("lime"); - - Assert.assertEquals(11, treeBag.size()); - Assert.assertEquals("apple", treeBag.first()); - Assert.assertEquals("lime", treeBag.last()); - - treeBag.remove("apple"); - Assert.assertEquals(9, treeBag.size()); - Assert.assertEquals("banana", treeBag.first()); - - } + + @Test + public void givenMultipleCopies_whenAdded_theCountIsKept() { + Bag bag = new HashBag<>( + Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 })); + + assertThat(bag.getCount(1), equalTo(2)); + } + + @Test + public void givenBag_whenBagAddAPILikeCollectionAPI_thenFalse() { + Collection collection = new ArrayList<>(); + + // Collection contract defines that add() should return true + assertThat(collection.add(9), is(true)); + + // Even when element is already in the collection + collection.add(1); + assertThat(collection.add(1), is(true)); + + Bag bag = new HashBag<>(); + + // Bag returns true on adding a new element + assertThat(bag.add(9), is(true)); + + bag.add(1); + // But breaks the contract with false when it has to increment the count + assertThat(bag.add(1), is(not(true))); + } + + @Test + public void givenDecoratedBag_whenBagAddAPILikeCollectionAPI_thenTrue() { + Bag bag = CollectionBag.collectionBag(new HashBag<>()); + + bag.add(1); + // This time the behavior is compliant to the Java Collection + assertThat(bag.add(1), is((true))); + } + + @Test + public void givenAdd_whenCountOfElementsDefined_thenCountAreAdded() { + Bag bag = new HashBag<>(); + + // Adding 1 for 5 times + bag.add(1, 5); + assertThat(bag.getCount(1), equalTo(5)); + } + + @Test + public void givenMultipleCopies_whenRemove_allAreRemoved() { + Bag bag = new HashBag<>( + Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 })); + + // From 3 we delete 1, 2 remain + bag.remove(3, 1); + assertThat(bag.getCount(3), equalTo(2)); + + // From 2 we delete all + bag.remove(1); + assertThat(bag.getCount(1), equalTo(0)); + } + + @Test + public void givenTree_whenDuplicateElementsAdded_thenSort() { + TreeBag bag = new TreeBag<>( + Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 })); + + assertThat(bag.first(), equalTo(1)); + assertThat(bag.getCount(bag.first()), equalTo(2)); + assertThat(bag.last(), equalTo(7)); + assertThat(bag.getCount(bag.last()), equalTo(3)); + } + + @Test + public void givenDecoratedTree_whenTreeAddAPILikeCollectionAPI_thenTrue() { + SortedBag bag = CollectionSortedBag + .collectionSortedBag(new TreeBag<>()); + + bag.add(1); + assertThat(bag.add(1), is((true))); + } + + @Test + public void givenSortedBag_whenDuplicateElementsAdded_thenSort() { + SynchronizedSortedBag bag = SynchronizedSortedBag + .synchronizedSortedBag(new TreeBag<>( + Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 }))); + + assertThat(bag.first(), equalTo(1)); + assertThat(bag.getCount(bag.first()), equalTo(2)); + assertThat(bag.last(), equalTo(7)); + assertThat(bag.getCount(bag.last()), equalTo(3)); + } }