commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r1091448 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ObjectUtils.java test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Date Tue, 12 Apr 2011 15:13:37 GMT
Author: scolebourne
Date: Tue Apr 12 15:13:36 2011
New Revision: 1091448

URL: http://svn.apache.org/viewvc?rev=1091448&view=rev
Log:
LANG-692 - Add hashCodeMulti varargs method

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java?rev=1091448&r1=1091447&r2=1091448&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java Tue
Apr 12 15:13:36 2011
@@ -189,9 +189,41 @@ public class ObjectUtils {
      * @since 2.1
      */
     public static int hashCode(Object obj) {
+        // hashCode(Object) retained for performance, as hash code is often critical
         return (obj == null) ? 0 : obj.hashCode();
     }
 
+    /**
+     * <p>Gets the hash code for multiple objects.</p>
+     * 
+     * <p>This allows a hash code to be rapidly calculated for a number of objects.
+     * The hash code for a single object is the <em>not</em> same as {@link #hashCode(Object)}.
+     * The hash code for multiple objects is the same as that calculated by an
+     * {@code ArrayList} containing the specified objects.</p>
+     *
+     * <pre>
+     * ObjectUtils.hashCodeMulti()                 = 1
+     * ObjectUtils.hashCodeMulti((Object[]) null)  = 1
+     * ObjectUtils.hashCodeMulti(a)                = 31 + a.hashCode()
+     * ObjectUtils.hashCodeMulti(a,b)              = (31 + a.hashCode()) * 31 + b.hashCode()
+     * ObjectUtils.hashCodeMulti(a,b,c)            = ((31 + a.hashCode()) * 31 + b.hashCode())
* 31 + c.hashCode()
+     * </pre>
+     *
+     * @param first  the first object, may be {@code null}
+     * @param objects  the objects to obtain the hash code of, may be {@code null}
+     * @return the hash code of the objects, or zero if null
+     * @since 3.0
+     */
+    public static int hashCodeMulti(Object... objects) {
+        int hash = 1;
+        if (objects != null) {
+            for (int i = 0; i < objects.length; i++) {
+                hash = hash * 31 + ObjectUtils.hashCode(objects[i]);
+            }
+        }
+        return hash;
+    }
+
     // Identity ToString
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java?rev=1091448&r1=1091447&r2=1091448&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Tue Apr 12 15:13:36 2011
@@ -18,9 +18,11 @@ package org.apache.commons.lang3;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.List;
 
 import junit.framework.TestCase;
 
@@ -97,6 +99,30 @@ public class ObjectUtilsTest extends Tes
         assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
     }
 
+    public void testHashCodeMulti_multiple_emptyArray() {
+        Object[] array = new Object[0];
+        assertEquals(1, ObjectUtils.hashCodeMulti(array));
+    }
+
+    public void testHashCodeMulti_multiple_nullArray() {
+        Object[] array = null;
+        assertEquals(1, ObjectUtils.hashCodeMulti(array));
+    }
+
+    public void testHashCodeMulti_multiple_likeList() {
+        List<Object> list0 = new ArrayList<Object>(Arrays.asList());
+        assertEquals(list0.hashCode(), ObjectUtils.hashCodeMulti());
+        
+        List<Object> list1 = new ArrayList<Object>(Arrays.asList("a"));
+        assertEquals(list1.hashCode(), ObjectUtils.hashCodeMulti("a"));
+        
+        List<Object> list2 = new ArrayList<Object>(Arrays.asList("a", "b"));
+        assertEquals(list2.hashCode(), ObjectUtils.hashCodeMulti("a", "b"));
+        
+        List<Object> list3 = new ArrayList<Object>(Arrays.asList("a", "b", "c"));
+        assertEquals(list3.hashCode(), ObjectUtils.hashCodeMulti("a", "b", "c"));
+    }
+
 //    /**
 //     * Show that java.util.Date and java.sql.Timestamp are apples and oranges.
 //     * Prompted by an email discussion. 



Mime
View raw message