Code examples for the articl on Apache bags (#2671)

This commit is contained in:
adamd1985 2017-09-25 20:24:24 +02:00 committed by Grzegorz Piwowarek
parent 2beb5f012c
commit 8bf825d046

View File

@ -1,85 +1,111 @@
package com.baeldung.commons.collections4; 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;
import org.apache.commons.collections4.bag.CollectionBag; import org.apache.commons.collections4.SortedBag;
import org.apache.commons.collections4.bag.HashBag; import org.apache.commons.collections4.bag.*;
import org.apache.commons.collections4.bag.TreeBag;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; 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 { public class BagTests {
Bag<String> baseBag; @Test
TreeBag<String> treeBag; public void givenMultipleCopies_whenAdded_theCountIsKept() {
Bag<Integer> bag = new HashBag<>(
@Before Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 }));
public void before() {
baseBag = new HashBag<String>(); assertThat(bag.getCount(1), equalTo(2));
treeBag = new TreeBag<String>(); }
treeBag = new TreeBag<String>();
} @Test
public void givenBag_whenBagAddAPILikeCollectionAPI_thenFalse() {
@Test Collection<Integer> collection = new ArrayList<>();
public void whenAdd_thenRemoveFromBaseBag_thenContainsCorrect() {
baseBag.add("apple", 2); // Collection contract defines that add() should return true
baseBag.add("lemon", 6); assertThat(collection.add(9), is(true));
baseBag.add("lime");
// Even when element is already in the collection
baseBag.remove("lemon"); collection.add(1);
Assert.assertEquals(3, baseBag.size()); assertThat(collection.add(1), is(true));
Assert.assertFalse(baseBag.contains("lemon"));
Bag<Integer> bag = new HashBag<>();
Assert.assertTrue(baseBag.uniqueSet().contains("apple"));
// Bag returns true on adding a new element
List<String> containList = new ArrayList<String>(); assertThat(bag.add(9), is(true));
containList.add("apple");
containList.add("lemon"); bag.add(1);
containList.add("lime"); // But breaks the contract with false when it has to increment the count
Assert.assertFalse(baseBag.containsAll(containList)); assertThat(bag.add(1), is(not(true)));
} }
@Test @Test
public void whenAdd_thenRemoveFromBaseCollectionBag_thenContainsCorrect() { public void givenDecoratedBag_whenBagAddAPILikeCollectionAPI_thenTrue() {
baseBag.add("apple", 2); Bag<Integer> bag = CollectionBag.collectionBag(new HashBag<>());
baseBag.add("lemon", 6);
baseBag.add("lime"); bag.add(1);
// This time the behavior is compliant to the Java Collection
CollectionBag<String> baseCollectionBag = new CollectionBag<String>( assertThat(bag.add(1), is((true)));
baseBag); }
baseCollectionBag.remove("lemon"); @Test
Assert.assertEquals(8, baseCollectionBag.size()); public void givenAdd_whenCountOfElementsDefined_thenCountAreAdded() {
Assert.assertTrue(baseCollectionBag.contains("lemon")); Bag<Integer> bag = new HashBag<>();
baseCollectionBag.remove("lemon",1); // Adding 1 for 5 times
Assert.assertEquals(7, baseCollectionBag.size()); bag.add(1, 5);
assertThat(bag.getCount(1), equalTo(5));
Assert.assertTrue(baseBag.uniqueSet().contains("apple")); }
List<String> containList = new ArrayList<String>(); @Test
containList.add("apple"); public void givenMultipleCopies_whenRemove_allAreRemoved() {
containList.add("lemon"); Bag<Integer> bag = new HashBag<>(
containList.add("lime"); Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 }));
Assert.assertTrue(baseBag.containsAll(containList));
} // From 3 we delete 1, 2 remain
bag.remove(3, 1);
@Test assertThat(bag.getCount(3), equalTo(2));
public void whenAddtoTreeBag_thenRemove_thenContainsCorrect() {
treeBag.add("banana", 8); // From 2 we delete all
treeBag.add("apple", 2); bag.remove(1);
treeBag.add("lime"); assertThat(bag.getCount(1), equalTo(0));
}
Assert.assertEquals(11, treeBag.size());
Assert.assertEquals("apple", treeBag.first()); @Test
Assert.assertEquals("lime", treeBag.last()); public void givenTree_whenDuplicateElementsAdded_thenSort() {
TreeBag<Integer> bag = new TreeBag<>(
treeBag.remove("apple"); Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 }));
Assert.assertEquals(9, treeBag.size());
Assert.assertEquals("banana", treeBag.first()); 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<Integer> bag = CollectionSortedBag
.collectionSortedBag(new TreeBag<>());
bag.add(1);
assertThat(bag.add(1), is((true)));
}
@Test
public void givenSortedBag_whenDuplicateElementsAdded_thenSort() {
SynchronizedSortedBag<Integer> 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));
}
} }