commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r1077910 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ArrayUtils.java test/java/org/apache/commons/lang3/ArrayUtilsTest.java
Date Fri, 04 Mar 2011 12:31:25 GMT
Author: scolebourne
Date: Fri Mar  4 12:31:24 2011
New Revision: 1077910

URL: http://svn.apache.org/viewvc?rev=1077910&view=rev
Log:
[LANG-492] Revert deletion of ArrayUtils.hashCode()

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java?rev=1077910&r1=1077909&r2=1077910&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java Fri Mar
 4 12:31:24 2011
@@ -21,6 +21,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
 
@@ -182,6 +183,18 @@ public class ArrayUtils {
     }
 
     /**
+     * <p>Get a hash code for an array handling multi-dimensional arrays correctly.</p>
+     *
+     * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
+     *
+     * @param array  the array to get a hash code for, <code>null</code> returns
zero
+     * @return a hash code for the array
+     */
+    public static int hashCode(Object array) {
+        return new HashCodeBuilder().append(array).toHashCode();
+    }
+
+    /**
      * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
      * correctly.</p>
      *

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java?rev=1077910&r1=1077909&r2=1077910&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java Fri
Mar  4 12:31:24 2011
@@ -71,6 +71,23 @@ public class ArrayUtilsTest extends Test
     }
 
     //-----------------------------------------------------------------------
+    public void testHashCode() {
+        long[][] array1 = new long[][] {{2,5}, {4,5}};
+        long[][] array2 = new long[][] {{2,5}, {4,6}};
+        assertEquals(true, ArrayUtils.hashCode(array1) == ArrayUtils.hashCode(array1));
+        assertEquals(false, ArrayUtils.hashCode(array1) == ArrayUtils.hashCode(array2));
+        
+        Object[] array3 = new Object[] {new String(new char[] {'A', 'B'})};
+        Object[] array4 = new Object[] {"AB"};
+        assertEquals(true, ArrayUtils.hashCode(array3) == ArrayUtils.hashCode(array3));
+        assertEquals(true, ArrayUtils.hashCode(array3) == ArrayUtils.hashCode(array4));
+        
+        Object[] arrayA = new Object[] {new boolean[] {true, false}, new int[] {6, 7}};
+        Object[] arrayB = new Object[] {new boolean[] {true, false}, new int[] {6, 7}};
+        assertEquals(true, ArrayUtils.hashCode(arrayB) == ArrayUtils.hashCode(arrayA));
+    }
+
+    //-----------------------------------------------------------------------
     private void assertIsEquals(Object array1, Object array2, Object array3) {
         assertEquals(true, ArrayUtils.isEquals(array1, array1));
         assertEquals(true, ArrayUtils.isEquals(array2, array2));



Mime
View raw message