[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
This commit is contained in:
Stephen Colebourne 2011-03-04 12:31:24 +00:00
parent c7f93ca309
commit 3ac961a314
2 changed files with 30 additions and 0 deletions

View File

@ -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();
}
/**
* <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>

View File

@ -70,6 +70,23 @@ public class ArrayUtilsTest extends TestCase {
assertEquals("{pink,blue}", ArrayUtils.toString(new String[] {"pink","blue"}, "<empty>"));
}
//-----------------------------------------------------------------------
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));