You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2020/08/20 08:40:01 UTC

[commons-collections] 01/04: [COLLECTIONS-708] Add hashcode method to CollectionUtils that supports an equator parameter

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit d0551d01b3d7e97aa5d8fb1ec91e648a93dab929
Author: dota17 <ch...@163.com>
AuthorDate: Tue Oct 22 09:55:26 2019 +0800

    [COLLECTIONS-708]  Add hashcode method to CollectionUtils that supports an equator parameter
---
 .../commons/collections4/CollectionUtils.java      | 26 ++++++++++++
 .../commons/collections4/CollectionUtilsTest.java  | 49 +++++++++++++++++++++-
 2 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
index 2b73511..7f7d6bc 100644
--- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java
+++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java
@@ -644,6 +644,32 @@ public class CollectionUtils {
     }
 
     /**
+     * Returns the hash code the the input collection using the hash method of the equator.
+     * <p>
+     * If the input collection is null return 0.
+     * </p>
+     *
+     * @param <E>  the element type
+     * @param collection  the input collection
+     * @param equator  the equator used for generate hashCode
+     * @return the hashCode of Collection through the hash method of Equator
+     * @throws NullPointerException if the equator is null
+     * @since 4.5
+     */
+    public static <E> 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()}.
      * <p>
diff --git a/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java b/src/test/java/org/apache/commons/collections4/CollectionUtilsTest.java
index 7d1bd98..4922a91 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<Integer> e = new Equator<Integer>() {
+            @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<Integer> e = new Equator<Integer>() {
+            @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<Integer> 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<Integer> 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();