-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathCollectionUtils.java
More file actions
86 lines (72 loc) · 2.74 KB
/
Copy pathCollectionUtils.java
File metadata and controls
86 lines (72 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: MIT
package lermitage.intellij.extra.icons.utils;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/** To avoid using Apache Commons Collection 3 (no longer bundled with IDE) or 4 just for a few operations. */
public class CollectionUtils {
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean isEqualCollection(final Collection<?> a, final Collection<?> 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;
}
//
// The rest of the code has been duplicated from org.apache.commons:commons-collections4, then applied quick-fixes by IDE
//
private static class CardinalityHelper<O> {
/** Contains the cardinality for each object in collection A. */
final Map<O, Integer> cardinalityA;
/** Contains the cardinality for each object in collection B. */
final Map<O, Integer> cardinalityB;
/**
* Create a new CardinalityHelper for two collections.
* @param a the first collection
* @param b the second collection
*/
public CardinalityHelper(final Iterable<? extends O> a, final Iterable<? extends O> b) {
cardinalityA = getCardinalityMap(a);
cardinalityB = getCardinalityMap(b);
}
/**
* Returns the frequency of this object in collection A.
* @param obj the object
* @return the frequency of the object in collection A
*/
public int freqA(final Object obj) {
return getFreq(obj, cardinalityA);
}
/**
* Returns the frequency of this object in collection B.
* @param obj the object
* @return the frequency of the object in collection B
*/
public int freqB(final Object obj) {
return getFreq(obj, cardinalityB);
}
private int getFreq(final Object obj, final Map<?, Integer> freqMap) {
final Integer count = freqMap.get(obj);
if (count != null) {
return count;
}
return 0;
}
}
public static <O> Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll) {
final Map<O, Integer> count = new HashMap<>();
for (final O obj : coll) {
count.merge(obj, 1, Integer::sum);
}
return count;
}
}