Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main/java/org/apache/commons/collections4/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,21 @@ public static boolean isProperSubCollection(final Collection<?> a, final Collect
* That is, iff the cardinality of <i>e</i> in <i>a</i> is
* equal to the cardinality of <i>e</i> in <i>b</i>,
* for each element <i>e</i> in <i>a</i> or <i>b</i>.
* <p>
* {@code null}s are handled without exceptions. Two {@code null}
* references are considered to be equal.
*
* @param a the first collection, must not be null
* @param b the second collection, must not be null
* @return <code>true</code> iff the collections contain the same elements with the same cardinalities.
*/
public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) {
if(a == null && b == null) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.size() != b.size()) {
return false;
}
Expand All @@ -532,6 +541,9 @@ public static boolean isEqualCollection(final Collection<?> a, final Collection<
* equal to the cardinality of <i>e</i> in <i>b</i>,
* for each element <i>e</i> in <i>a</i> or <i>b</i>.
* <p>
* {@code null}s are handled without exceptions. Two {@code null}
* references are considered to be equal.
* <p>
* <b>Note:</b> from version 4.1 onwards this method requires the input
* collections and equator to be of compatible type (using bounded wildcards).
* Providing incompatible arguments (e.g. by casting to their rawtypes)
Expand All @@ -552,10 +564,6 @@ public static <E> boolean isEqualCollection(final Collection<? extends E> a,
throw new NullPointerException("Equator must not be null.");
}

if(a.size() != b.size()) {
return false;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
final Transformer<E, ?> transformer = new Transformer() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@ public void testIsEqualCollection2() {
assertTrue(CollectionUtils.isEqualCollection(b, a));
}

@Test
public void testIsEqualCollectionWithNulls() {
assertTrue(CollectionUtils.isEqualCollection(null, null));
assertFalse(CollectionUtils.isEqualCollection(collectionA, null));
assertFalse(CollectionUtils.isEqualCollection(null, collectionA));
}

@Test
public void testIsEqualCollectionEquator() {
final Collection<Integer> collB = CollectionUtils.collect(collectionB, TRANSFORM_TO_INTEGER);
Expand Down Expand Up @@ -594,6 +601,25 @@ public void testIsEqualCollectionNullEquator() {
CollectionUtils.isEqualCollection(collectionA, collectionA, null);
}

@Test
public void testIsEqualCollectionEquatorWithNulls() {
Equator<Object> equator = new Equator<Object>() {
@Override
public boolean equate(Object o1, Object o2) {
return false;
}

@Override
public int hash(Object o) {
return 0;
}
};

assertTrue(CollectionUtils.isEqualCollection(null, null, equator));
assertFalse(CollectionUtils.isEqualCollection(collectionA, null, equator));
assertFalse(CollectionUtils.isEqualCollection(null, collectionA, equator));
}

@Test
public void testIsProperSubCollection() {
final Collection<String> a = new ArrayList<String>();
Expand Down