diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c7d8c988b..0da4fbd19 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -21,6 +21,9 @@
+
+ Add hashCode method to CollectionUtils that supports an equator parameter.
+
Fix checkstyle issues regarding missing newline at end of file, and CRLF vs LF.
diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
index 2b73511a7..4c2f4167f 100644
--- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java
+++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
@@ -643,6 +643,33 @@ public class CollectionUtils {
return isEqualCollection(collect(a, transformer), collect(b, transformer));
}
+ /**
+ * Returns the hash code of the input collection using the hash method of an equator.
+ *
+ *
+ * Returns 0 if the input collection is {@code null}.
+ *
+ *
+ * @param the element type
+ * @param collection the input collection
+ * @param equator the equator used for generate hashCode
+ * @return the hash code of the input collection using the hash method of an equator
+ * @throws NullPointerException if the equator is {@code null}
+ * @since 4.5
+ */
+ public static int hashCode(final Collection extends E> collection,
+ final Equator super E> equator) {
+ Objects.requireNonNull(equator, "equator");
+ if (null == collection) {
+ return 0;
+ }
+ int hashCode = 1;
+ for (final E e : collection) {
+ hashCode = 31 * hashCode + equator.hash(e);
+ }
+ return hashCode;
+ }
+
/**
* Wraps another object and uses the provided Equator to implement
* {@link #equals(Object)} and {@link #hashCode()}.
diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
index 7d1bd98f2..4922a9176 100644
--- a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
@@ -37,6 +37,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
@@ -741,6 +742,52 @@ public class CollectionUtilsTest extends MockTestCase {
CollectionUtils.isEqualCollection(collectionA, collectionA, null);
}
+ @Test
+ public void testHashCode() {
+ final Equator e = new Equator() {
+ @Override
+ public boolean equate(final Integer o1, final Integer o2) {
+ if (o1 % 2 == 0 ^ o2 % 2 == 0) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hash(final Integer o) {
+ return o == null ? 0 : Objects.hashCode(o);
+ }
+ };
+
+ assertEquals(collectionA.hashCode(), CollectionUtils.hashCode(collectionA, e));
+ }
+
+ @Test
+ public void testHashCodeNullCollection() {
+ final Equator e = new Equator() {
+ @Override
+ public boolean equate(final Integer o1, final Integer o2) {
+ if (o1 % 2 == 0 ^ o2 % 2 == 0) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hash(final Integer o) {
+ return o == null ? 0 : Objects.hashCode(o);
+ }
+ };
+
+ final Collection collection = null;
+ assertEquals(0, CollectionUtils.hashCode(collection, e));
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testHashCodeNullEquator() {
+ CollectionUtils.hashCode(collectionB, null);
+ }
+
@Test(expected = NullPointerException.class)
public void testIsEqualCollectionNullColl1() {
final Collection list = new ArrayList<>(1);
@@ -1073,7 +1120,7 @@ public class CollectionUtilsTest extends MockTestCase {
CollectionUtils.get(array, 2);
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void getFromObject() throws Exception {
// Invalid object
final Object obj = new Object();