Skip to content

Commit 8bf825d

Browse files
adamd1985pivovarit
authored andcommitted
Code examples for the articl on Apache bags (eugenp#2671)
1 parent 2beb5f0 commit 8bf825d

1 file changed

Lines changed: 103 additions & 77 deletions

File tree

  • libraries/src/test/java/com/baeldung/commons/collections4
Lines changed: 103 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,111 @@
11
package com.baeldung.commons.collections4;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
import org.apache.commons.collections4.Bag;
7-
import org.apache.commons.collections4.bag.CollectionBag;
8-
import org.apache.commons.collections4.bag.HashBag;
9-
import org.apache.commons.collections4.bag.TreeBag;
10-
import org.junit.Assert;
11-
import org.junit.Before;
4+
import org.apache.commons.collections4.SortedBag;
5+
import org.apache.commons.collections4.bag.*;
126
import org.junit.Test;
137

14-
public class BagTests {
15-
16-
Bag<String> baseBag;
17-
TreeBag<String> treeBag;
18-
19-
@Before
20-
public void before() {
21-
baseBag = new HashBag<String>();
22-
treeBag = new TreeBag<String>();
23-
treeBag = new TreeBag<String>();
24-
}
25-
26-
@Test
27-
public void whenAdd_thenRemoveFromBaseBag_thenContainsCorrect() {
28-
baseBag.add("apple", 2);
29-
baseBag.add("lemon", 6);
30-
baseBag.add("lime");
31-
32-
baseBag.remove("lemon");
33-
Assert.assertEquals(3, baseBag.size());
34-
Assert.assertFalse(baseBag.contains("lemon"));
35-
36-
Assert.assertTrue(baseBag.uniqueSet().contains("apple"));
37-
38-
List<String> containList = new ArrayList<String>();
39-
containList.add("apple");
40-
containList.add("lemon");
41-
containList.add("lime");
42-
Assert.assertFalse(baseBag.containsAll(containList));
43-
}
44-
45-
@Test
46-
public void whenAdd_thenRemoveFromBaseCollectionBag_thenContainsCorrect() {
47-
baseBag.add("apple", 2);
48-
baseBag.add("lemon", 6);
49-
baseBag.add("lime");
50-
51-
CollectionBag<String> baseCollectionBag = new CollectionBag<String>(
52-
baseBag);
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Collection;
5311

54-
baseCollectionBag.remove("lemon");
55-
Assert.assertEquals(8, baseCollectionBag.size());
56-
Assert.assertTrue(baseCollectionBag.contains("lemon"));
57-
58-
baseCollectionBag.remove("lemon",1);
59-
Assert.assertEquals(7, baseCollectionBag.size());
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.not;
14+
import static org.hamcrest.core.Is.is;
15+
import static org.hamcrest.core.IsEqual.equalTo;
6016

61-
Assert.assertTrue(baseBag.uniqueSet().contains("apple"));
62-
63-
List<String> containList = new ArrayList<String>();
64-
containList.add("apple");
65-
containList.add("lemon");
66-
containList.add("lime");
67-
Assert.assertTrue(baseBag.containsAll(containList));
68-
}
69-
70-
@Test
71-
public void whenAddtoTreeBag_thenRemove_thenContainsCorrect() {
72-
treeBag.add("banana", 8);
73-
treeBag.add("apple", 2);
74-
treeBag.add("lime");
75-
76-
Assert.assertEquals(11, treeBag.size());
77-
Assert.assertEquals("apple", treeBag.first());
78-
Assert.assertEquals("lime", treeBag.last());
79-
80-
treeBag.remove("apple");
81-
Assert.assertEquals(9, treeBag.size());
82-
Assert.assertEquals("banana", treeBag.first());
83-
84-
}
17+
public class BagTests {
18+
19+
@Test
20+
public void givenMultipleCopies_whenAdded_theCountIsKept() {
21+
Bag<Integer> bag = new HashBag<>(
22+
Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 }));
23+
24+
assertThat(bag.getCount(1), equalTo(2));
25+
}
26+
27+
@Test
28+
public void givenBag_whenBagAddAPILikeCollectionAPI_thenFalse() {
29+
Collection<Integer> collection = new ArrayList<>();
30+
31+
// Collection contract defines that add() should return true
32+
assertThat(collection.add(9), is(true));
33+
34+
// Even when element is already in the collection
35+
collection.add(1);
36+
assertThat(collection.add(1), is(true));
37+
38+
Bag<Integer> bag = new HashBag<>();
39+
40+
// Bag returns true on adding a new element
41+
assertThat(bag.add(9), is(true));
42+
43+
bag.add(1);
44+
// But breaks the contract with false when it has to increment the count
45+
assertThat(bag.add(1), is(not(true)));
46+
}
47+
48+
@Test
49+
public void givenDecoratedBag_whenBagAddAPILikeCollectionAPI_thenTrue() {
50+
Bag<Integer> bag = CollectionBag.collectionBag(new HashBag<>());
51+
52+
bag.add(1);
53+
// This time the behavior is compliant to the Java Collection
54+
assertThat(bag.add(1), is((true)));
55+
}
56+
57+
@Test
58+
public void givenAdd_whenCountOfElementsDefined_thenCountAreAdded() {
59+
Bag<Integer> bag = new HashBag<>();
60+
61+
// Adding 1 for 5 times
62+
bag.add(1, 5);
63+
assertThat(bag.getCount(1), equalTo(5));
64+
}
65+
66+
@Test
67+
public void givenMultipleCopies_whenRemove_allAreRemoved() {
68+
Bag<Integer> bag = new HashBag<>(
69+
Arrays.asList(new Integer[] { 1, 2, 3, 3, 3, 1, 4 }));
70+
71+
// From 3 we delete 1, 2 remain
72+
bag.remove(3, 1);
73+
assertThat(bag.getCount(3), equalTo(2));
74+
75+
// From 2 we delete all
76+
bag.remove(1);
77+
assertThat(bag.getCount(1), equalTo(0));
78+
}
79+
80+
@Test
81+
public void givenTree_whenDuplicateElementsAdded_thenSort() {
82+
TreeBag<Integer> bag = new TreeBag<>(
83+
Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 }));
84+
85+
assertThat(bag.first(), equalTo(1));
86+
assertThat(bag.getCount(bag.first()), equalTo(2));
87+
assertThat(bag.last(), equalTo(7));
88+
assertThat(bag.getCount(bag.last()), equalTo(3));
89+
}
90+
91+
@Test
92+
public void givenDecoratedTree_whenTreeAddAPILikeCollectionAPI_thenTrue() {
93+
SortedBag<Integer> bag = CollectionSortedBag
94+
.collectionSortedBag(new TreeBag<>());
95+
96+
bag.add(1);
97+
assertThat(bag.add(1), is((true)));
98+
}
99+
100+
@Test
101+
public void givenSortedBag_whenDuplicateElementsAdded_thenSort() {
102+
SynchronizedSortedBag<Integer> bag = SynchronizedSortedBag
103+
.synchronizedSortedBag(new TreeBag<>(
104+
Arrays.asList(new Integer[] { 7, 5, 1, 7, 2, 3, 3, 3, 1, 4, 7 })));
105+
106+
assertThat(bag.first(), equalTo(1));
107+
assertThat(bag.getCount(bag.first()), equalTo(2));
108+
assertThat(bag.last(), equalTo(7));
109+
assertThat(bag.getCount(bag.last()), equalTo(3));
110+
}
85111
}

0 commit comments

Comments
 (0)