Skip to content

Commit

Permalink
Add HashBag.HashBag(Iterable)
Browse files Browse the repository at this point in the history
- Add TreeBag.TreeBag(Iterable)
  • Loading branch information
garydgregory committed Sep 1, 2024
1 parent 7e81d81 commit 3f7cb39
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateList(Collection).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateSet(Collection).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateSequencedSet(Collection).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add HashBag.HashBag(Iterable).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add TreeBag.TreeBag(Iterable).</action>
<!-- UPDATE -->
<action issue="COLLECTIONS-857" type="update" dev="ggregory" due-to="Claude Warren">Update bloom filter documentation #508.</action>
<action issue="COLLECTIONS-857" type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump commons-codec:commons-codec from 1.17.0 to 1.17.1 #514.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.collections4.Bag;
Expand Down Expand Up @@ -165,7 +166,19 @@ protected AbstractMapBag() {
* @param map the map to assign
*/
protected AbstractMapBag(final Map<E, MutableInteger> map) {
this.map = map;
this.map = Objects.requireNonNull(map, "map");
}

/**
* Constructs a new instance that assigns the specified Map as the backing store. The map
* must be empty and non-null. The bag is filled from the iterable elements.
*
* @param map the map to assign.
* @param iterable The bag is filled from these iterable elements.
*/
protected AbstractMapBag(final Map<E, MutableInteger> map, final Iterable<? extends E> iterable) {
this(map);
iterable.forEach(this::add);
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/apache/commons/collections4/bag/HashBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,23 @@ public HashBag() {
}

/**
* Constructs a bag containing all the members of the given collection.
* Constructs a bag containing all the members of the given Collection.
*
* @param coll a collection to copy into this bag
* @param collection a collection to copy into this bag.
*/
public HashBag(final Collection<? extends E> coll) {
public HashBag(final Collection<? extends E> collection) {
this();
addAll(coll);
addAll(collection);
}

/**
* Constructs a bag containing all the members of the given Iterable.
*
* @param iterable an iterable to copy into this bag.
* @since 4.5.0-M3
*/
public HashBag(final Iterable<? extends E> iterable) {
super(new HashMap<>(), iterable);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/apache/commons/collections4/bag/TreeBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public TreeBag(final Collection<? extends E> coll) {
addAll(coll);
}

/**
* Constructs a bag containing all the members of the given Iterable.
*
* @param iterable an iterable to copy into this bag.
* @since 4.5.0-M3
*/
public TreeBag(final Iterable<? extends E> iterable) {
super(new TreeMap<>(), iterable);
}

/**
* Constructs an empty bag that maintains order on its unique representative
* members according to the given {@link Comparator}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ public void testKeysBagContainsAll() {
}

@Test
public void testKeysBagIterator() {
public void testKeysBagIterator1() {
final MultiValuedMap<K, V> map = makeFullMap();
final Collection<K> col = new ArrayList<>(map.keys());
final Bag<K> bag = new HashBag<>(col);
Expand All @@ -942,6 +942,18 @@ public void testKeysBagIterator() {
assertEquals(getSampleTotalValueCount(), bag.size());
}

@Test
public void testKeysBagIterator2() {
final MultiValuedMap<K, V> map = makeFullMap();
final Iterable<K> iterable = new ArrayList<>(map.keys());
final Bag<K> bag = new HashBag<>(iterable);
final int maxK = getSampleKeySize();
for (int k = 0; k < maxK; k++) {
assertEquals(getSampleCountPerKey(), bag.getCount(makeKey(k)));
}
assertEquals(getSampleTotalValueCount(), bag.size());
}

@Test
public void testKeySetSize() {
final MultiValuedMap<K, V> map = makeFullMap();
Expand Down

0 comments on commit 3f7cb39

Please sign in to comment.