Reimplement the package-private CollectionUtils.CardinalityHelper using

Bags instead of custom Maps
This commit is contained in:
Gary Gregory 2024-09-01 17:18:05 -04:00
parent a91ae587a2
commit 7e81d81962
1 changed files with 18 additions and 27 deletions

View File

@ -63,26 +63,29 @@ public class CollectionUtils {
*/ */
private static class CardinalityHelper<O> { private static class CardinalityHelper<O> {
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. */ /** 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. */ /** 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 a the first collection
* @param b the second collection * @param b the second collection
*/ */
CardinalityHelper(final Iterable<? extends O> a, final Iterable<? extends O> b) { CardinalityHelper(final Iterable<? extends O> a, final Iterable<? extends O> b) {
cardinalityA = getCardinalityMap(a); cardinalityA = new HashBag<>(a);
cardinalityB = getCardinalityMap(b); 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. * @param key the key whose associated frequency is to be returned.
* @return the frequency of the object in collection A * @return the frequency of the object in collection A
@ -92,7 +95,7 @@ public class CollectionUtils {
} }
/** /**
* 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. * @param key the key whose associated frequency is to be returned.
* @return the frequency of the object in collection B * @return the frequency of the object in collection B
@ -101,12 +104,13 @@ public class CollectionUtils {
return getFreq(key, cardinalityB); return getFreq(key, cardinalityB);
} }
private int getFreq(final Object key, final Map<?, Integer> freqMap) { private int getFreq(final Object key, final Bag<?> freqMap) {
return freqMap.getOrDefault(key, ZERO).intValue(); return freqMap.getCount(key);
} }
/** /**
* Returns the maximum frequency of an object. * Gets the maximum frequency of an object.
*
* @param obj the object * @param obj the object
* @return the maximum frequency of the object * @return the maximum frequency of the object
*/ */
@ -115,7 +119,8 @@ public class CollectionUtils {
} }
/** /**
* Returns the minimum frequency of an object. * Gets the minimum frequency of an object.
*
* @param obj the object * @param obj the object
* @return the minimum frequency of the object * @return the minimum frequency of the object
*/ */
@ -1257,21 +1262,7 @@ public class CollectionUtils {
* @throws NullPointerException if either collection is null * @throws NullPointerException if either collection is null
*/ */
public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) { public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) {
Objects.requireNonNull(a, "a"); return CardinalityHelper.equals(a, b);
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;
} }
/** /**