Skip to content

Commit

Permalink
Merge branch 'apache:master' into feature/findDuplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth0525 committed Sep 2, 2024
2 parents c455e6a + 03e5b0e commit f5c7e46
Show file tree
Hide file tree
Showing 23 changed files with 510 additions and 104 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>5.3.0</version>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
10 changes: 7 additions & 3 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@
<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>
<action type="add" issue="COLLECTIONS-858" dev="ggregory" due-to="Alexey Pelykh, Alex Herbert, Gary Gregory">Add CartesianProductIterator #509.</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>
<action issue="COLLECTIONS-857" type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-lang3 from 3.14.0 to 3.17.0 #516, #525, #535.</action>
<action type="update" issue="COLLECTIONS-857" dev="ggregory" due-to="Claude Warren">Update bloom filter documentation #508.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump commons-codec:commons-codec from 1.17.0 to 1.17.1 #514.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-lang3 from 3.14.0 to 3.17.0 #516, #525, #535.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 71 to 74 #534.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.hamcrest:hamcrest from 2.2 to 3.0 #522, #532.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump com.google.guava:guava-testlib from 33.2.1-jre to 33.3.0-jre #531.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump MathJax from 2.7.2 to 2.7.9.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump org.easymock:easymock from 5.3.0 to 5.4.0 #521.</action>
</release>
<release version="4.5.0-M2" date="2024-06-15" description="This milestone release requires Java 8 and adds the package `org.apache.commons.collections4.bloomfilter`.">
<!-- ADD -->
Expand Down
63 changes: 27 additions & 36 deletions src/main/java/org/apache/commons/collections4/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,54 @@ public class CollectionUtils {
*/
private static class CardinalityHelper<O> {

static boolean equals(final Collection<?> a, final Collection<?> b) {
return new HashBag<>(a).equals(new HashBag<>(b));
}

/** Contains the cardinality for each object in collection A. */
final Map<O, Integer> cardinalityA;
final Bag<O> cardinalityA;

/** Contains the cardinality for each object in collection B. */
final Map<O, Integer> cardinalityB;
final Bag<O> cardinalityB;

/**
* Create a new CardinalityHelper for two collections.
* Creates a new CardinalityHelper for two collections.
*
* @param a the first collection
* @param b the second collection
*/
CardinalityHelper(final Iterable<? extends O> a, final Iterable<? extends O> b) {
cardinalityA = getCardinalityMap(a);
cardinalityB = getCardinalityMap(b);
cardinalityA = new HashBag<>(a);
cardinalityB = new HashBag<>(b);
}

/**
* Returns the frequency of this object in collection A.
* @param obj the object
* Gets the frequency of this object in collection A.
*
* @param key the key whose associated frequency is to be returned.
* @return the frequency of the object in collection A
*/
public int freqA(final Object obj) {
return getFreq(obj, cardinalityA);
public int freqA(final Object key) {
return getFreq(key, cardinalityA);
}

/**
* Returns the frequency of this object in collection B.
* @param obj the object
* Gets the frequency of this object in collection B.
*
* @param key the key whose associated frequency is to be returned.
* @return the frequency of the object in collection B
*/
public int freqB(final Object obj) {
return getFreq(obj, cardinalityB);
public int freqB(final Object key) {
return getFreq(key, cardinalityB);
}

private int getFreq(final Object obj, final Map<?, Integer> freqMap) {
final Integer count = freqMap.get(obj);
if (count != null) {
return count.intValue();
}
return 0;
private int getFreq(final Object key, final Bag<?> freqMap) {
return freqMap.getCount(key);
}

/**
* Returns the maximum frequency of an object.
* Gets the maximum frequency of an object.
*
* @param obj the object
* @return the maximum frequency of the object
*/
Expand All @@ -115,7 +119,8 @@ public final int max(final Object obj) {
}

/**
* Returns the minimum frequency of an object.
* Gets the minimum frequency of an object.
*
* @param obj the object
* @return the minimum frequency of the object
*/
Expand Down Expand Up @@ -1257,21 +1262,7 @@ public static boolean isEmpty(final Collection<?> coll) {
* @throws NullPointerException if either collection is null
*/
public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) {
Objects.requireNonNull(a, "a");
Objects.requireNonNull(b, "b");
if (a.size() != b.size()) {
return false;
}
final CardinalityHelper<Object> helper = new CardinalityHelper<>(a, b);
if (helper.cardinalityA.size() != helper.cardinalityB.size()) {
return false;
}
for (final Object obj : helper.cardinalityA.keySet()) {
if (helper.freqA(obj) != helper.freqB(obj)) {
return false;
}
}
return true;
return CardinalityHelper.equals(a, b);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static <T> FluentIterable<T> empty() {
* @throws NullPointerException if iterable is null
*/
public static <T> FluentIterable<T> of(final Iterable<T> iterable) {
IterableUtils.checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
if (iterable instanceof FluentIterable<?>) {
return (FluentIterable<T>) iterable;
}
Expand Down
34 changes: 12 additions & 22 deletions src/main/java/org/apache/commons/collections4/IterableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Iterator<Object> iterator() {
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> boundedIterable(final Iterable<E> iterable, final long maxSize) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
if (maxSize < 0) {
throw new IllegalArgumentException("MaxSize parameter must not be negative.");
}
Expand Down Expand Up @@ -219,16 +219,6 @@ public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a,
return chainedIterable(new Iterable[] {a, b, c, d});
}

/**
* Fail-fast check for null arguments.
*
* @param iterable the iterable to check
* @throws NullPointerException if iterable is null
*/
static void checkNotNull(final Iterable<?> iterable) {
Objects.requireNonNull(iterable, "iterable");
}

/**
* Fail-fast check for null arguments.
*
Expand All @@ -238,7 +228,7 @@ static void checkNotNull(final Iterable<?> iterable) {
static void checkNotNull(final Iterable<?>... iterables) {
Objects.requireNonNull(iterables, "iterables");
for (final Iterable<?> iterable : iterables) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
}
}

Expand Down Expand Up @@ -409,7 +399,7 @@ private static <E> Iterator<E> emptyIteratorIfNull(final Iterable<E> iterable) {
*/
public static <E> Iterable<E> filteredIterable(final Iterable<E> iterable,
final Predicate<? super E> predicate) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
Objects.requireNonNull(predicate, "predicate");
return new FluentIterable<E>() {
@Override
Expand Down Expand Up @@ -572,7 +562,7 @@ public static boolean isEmpty(final Iterable<?> iterable) {
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> loopingIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
Expand Down Expand Up @@ -818,7 +808,7 @@ public static <O> List<List<O>> partition(final Iterable<? extends O> iterable,
* @see ReverseListIterator
*/
public static <E> Iterable<E> reversedIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
Expand Down Expand Up @@ -864,7 +854,7 @@ public static int size(final Iterable<?> iterable) {
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> skippingIterable(final Iterable<E> iterable, final long elementsToSkip) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
if (elementsToSkip < 0) {
throw new IllegalArgumentException("ElementsToSkip parameter must not be negative.");
}
Expand Down Expand Up @@ -970,7 +960,7 @@ public static <E> String toString(final Iterable<E> iterable,
*/
public static <I, O> Iterable<O> transformedIterable(final Iterable<I> iterable,
final Transformer<? super I, ? extends O> transformer) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
Objects.requireNonNull(transformer, "transformer");
return new FluentIterable<O>() {
@Override
Expand All @@ -994,7 +984,7 @@ public Iterator<O> iterator() {
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> uniqueIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
Expand All @@ -1015,7 +1005,7 @@ public Iterator<E> iterator() {
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> unmodifiableIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
Objects.requireNonNull(iterable, "iterable");
if (iterable instanceof UnmodifiableIterable<?>) {
return iterable;
}
Expand All @@ -1042,8 +1032,8 @@ public static <E> Iterable<E> unmodifiableIterable(final Iterable<E> iterable) {
*/
public static <E> Iterable<E> zippingIterable(final Iterable<? extends E> a,
final Iterable<? extends E> b) {
checkNotNull(a);
checkNotNull(b);
Objects.requireNonNull(a, "iterable");
Objects.requireNonNull(b, "iterable");
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
Expand All @@ -1069,7 +1059,7 @@ public Iterator<E> iterator() {
* @throws NullPointerException if either of the provided iterables is null
*/
public static <E> Iterable<E> zippingIterable(final Iterable<? extends E> first, final Iterable<? extends E>... others) {
checkNotNull(first);
Objects.requireNonNull(first, "iterable");
checkNotNull(others);
return new FluentIterable<E>() {
@Override
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 @@ -109,10 +110,12 @@ public void remove() {
canRemove = false;
}
}

/**
* Mutable integer class for storing the data.
*/
protected static class MutableInteger {

/** The value of this mutable. */
protected int value;

Expand All @@ -137,8 +140,10 @@ public int hashCode() {
return value;
}
}

/** The map to use to store the data */
private transient Map<E, MutableInteger> map;

/** The current total size of the bag */
private int size;

Expand All @@ -161,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
Loading

0 comments on commit f5c7e46

Please sign in to comment.