From 7e81d81962a8d400cd908fdc5ce54d146292593c Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 1 Sep 2024 17:18:05 -0400 Subject: [PATCH] Reimplement the package-private CollectionUtils.CardinalityHelper using Bags instead of custom Maps --- .../commons/collections4/CollectionUtils.java | 45 ++++++++----------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index b5feffd720..ff53f36dca 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -63,26 +63,29 @@ public class CollectionUtils { */ private static class CardinalityHelper { - private static final Integer ZERO = Integer.valueOf(0); + 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 cardinalityA; + final Bag cardinalityA; /** Contains the cardinality for each object in collection B. */ - final Map cardinalityB; + final Bag 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 a, final Iterable b) { - cardinalityA = getCardinalityMap(a); - cardinalityB = getCardinalityMap(b); + cardinalityA = new HashBag<>(a); + cardinalityB = new HashBag<>(b); } /** - * Returns the frequency of this object in collection A. + * 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 @@ -92,7 +95,7 @@ public int freqA(final Object key) { } /** - * Returns the frequency of this object in collection B. + * 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 @@ -101,12 +104,13 @@ public int freqB(final Object key) { return getFreq(key, cardinalityB); } - private int getFreq(final Object key, final Map freqMap) { - return freqMap.getOrDefault(key, ZERO).intValue(); + 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 */ @@ -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 */ @@ -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 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); } /**