From 3ac961a31479db54aa89c883e2a26314ffe24e3e Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 4 Mar 2011 12:31:24 +0000 Subject: [PATCH] [LANG-492] Revert deletion of ArrayUtils.hashCode() git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1077910 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang3/ArrayUtils.java | 13 +++++++++++++ .../apache/commons/lang3/ArrayUtilsTest.java | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 873285373..f4dd64082 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -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; @@ -181,6 +182,18 @@ public class ArrayUtils { return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString(); } + /** + *

Get a hash code for an array handling multi-dimensional arrays correctly.

+ * + *

Multi-dimensional primitive arrays are also handled correctly by this method.

+ * + * @param array the array to get a hash code for, null returns zero + * @return a hash code for the array + */ + public static int hashCode(Object array) { + return new HashCodeBuilder().append(array).toHashCode(); + } + /** *

Compares two arrays, using equals(), handling multi-dimensional arrays * correctly.

diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index e9bdae288..6baa6cd69 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -70,6 +70,23 @@ public class ArrayUtilsTest extends TestCase { assertEquals("{pink,blue}", ArrayUtils.toString(new String[] {"pink","blue"}, "")); } + //----------------------------------------------------------------------- + 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));