Adding equals(int[], int[]) style methods to NumberUtils as requested by Paul Benedict in LANG-238

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@467482 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2006-10-24 21:02:56 +00:00
parent 7a003412c6
commit 8fde2caf64
2 changed files with 352 additions and 0 deletions

View File

@ -668,6 +668,170 @@ public class NumberUtils {
return new BigDecimal(str);
}
// Equals in array
//--------------------------------------------------------------------
/**
* <p>Whether the contents of two byte[] arrays are equal.</p>
*
* @param array1 first array to compare
* @param array2 second array to compare
* @return whether the two arrays are equal
*/
public static boolean equals(byte[] array1, byte[] array2) {
if (array1 == array2) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array2.length) {
return false;
}
for (int i=0; i<array1.length; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
/**
* <p>Whether the contents of two short[] arrays are equal.</p>
*
* @param array1 first array to compare
* @param array2 second array to compare
* @return whether the two arrays are equal
*/
public static boolean equals(short[] array1, short[] array2) {
if (array1 == array2) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array2.length) {
return false;
}
for (int i=0; i<array1.length; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
/**
* <p>Whether the contents of two int[] arrays are equal.</p>
*
* @param array1 first array to compare
* @param array2 second array to compare
* @return whether the two arrays are equal
*/
public static boolean equals(int[] array1, int[] array2) {
if (array1 == array2) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array2.length) {
return false;
}
for (int i=0; i<array1.length; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
/**
* <p>Whether the contents of two long[] arrays are equal.</p>
*
* @param array1 first array to compare
* @param array2 second array to compare
* @return whether the two arrays are equal
*/
public static boolean equals(long[] array1, long[] array2) {
if (array1 == array2) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array2.length) {
return false;
}
for (int i=0; i<array1.length; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
/**
* <p>Whether the contents of two float[] arrays are equal.</p>
*
* @param array1 first array to compare
* @param array2 second array to compare
* @return whether the two arrays are equal
*/
public static boolean equals(float[] array1, float[] array2) {
if (array1 == array2) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array2.length) {
return false;
}
for (int i=0; i<array1.length; i++) {
if (compare(array1[i], array2[i]) != 0) {
return false;
}
}
return true;
}
/**
* <p>Whether the contents of two double[] arrays are equal.</p>
*
* @param array1 first array to compare
* @param array2 second array to compare
* @return whether the two arrays are equal
*/
public static boolean equals(double[] array1, double[] array2) {
if (array1 == array2) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array2.length) {
return false;
}
for (int i=0; i<array1.length; i++) {
if (compare(array1[i], array2[i]) != 0) {
return false;
}
}
return true;
}
// Min in array
//--------------------------------------------------------------------
/**

View File

@ -318,6 +318,194 @@ public class NumberUtilsTest extends TestCase {
}
}
// equals tests
// ----------------------------------------------------------------------
public void testEqualsByte() {
byte[] array1 = null;
byte[] array2 = null;
assertEquals( true, NumberUtils.equals(array1, array2) );
assertEquals( true, NumberUtils.equals(array2, array1) );
array1 = new byte[] { 50, 20 }; // array2 still null
assertEquals( false, NumberUtils.equals(array1, array2) );
assertEquals( false, NumberUtils.equals(array2, array1) );
// test same reference equivalence
array2 = array1;
assertEquals( true, NumberUtils.equals(array1, array2) );
// test object equivalence
array2 = new byte[] { 50, 20 };
assertEquals( true, NumberUtils.equals(array1, array2) );
// test symmetry is not equivalent
array2 = new byte[] { 20, 50 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test the whole length of rhs is tested against
array2 = new byte[] { 50, 20, 10 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test whole length of lhs is tested against
array2 = new byte[] { 50 };
assertEquals( false, NumberUtils.equals(array1, array2) );
}
public void testEqualsShort() {
short[] array1 = null;
short[] array2 = null;
assertEquals( true, NumberUtils.equals(array1, array2) );
assertEquals( true, NumberUtils.equals(array2, array1) );
array1 = new short[] { 50, 20 }; // array2 still null
assertEquals( false, NumberUtils.equals(array1, array2) );
assertEquals( false, NumberUtils.equals(array2, array1) );
// test same reference equivalence
array2 = array1;
assertEquals( true, NumberUtils.equals(array1, array2) );
// test object equivalence
array2 = new short[] { 50, 20 };
assertEquals( true, NumberUtils.equals(array1, array2) );
// test symmetry is not equivalent
array2 = new short[] { 20, 50 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test the whole length of rhs is tested against
array2 = new short[] { 50, 20, 10 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test whole length of lhs is tested against
array2 = new short[] { 50 };
assertEquals( false, NumberUtils.equals(array1, array2) );
}
public void testEqualsInt() {
int[] array1 = null;
int[] array2 = null;
assertEquals( true, NumberUtils.equals(array1, array2) );
assertEquals( true, NumberUtils.equals(array2, array1) );
array1 = new int[] { 50, 20 }; // array2 still null
assertEquals( false, NumberUtils.equals(array1, array2) );
assertEquals( false, NumberUtils.equals(array2, array1) );
// test same reference equivalence
array2 = array1;
assertEquals( true, NumberUtils.equals(array1, array2) );
// test object equivalence
array2 = new int[] { 50, 20 };
assertEquals( true, NumberUtils.equals(array1, array2) );
// test symmetry is not equivalent
array2 = new int[] { 20, 50 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test the whole length of rhs is tested against
array2 = new int[] { 50, 20, 10 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test whole length of lhs is tested against
array2 = new int[] { 50 };
assertEquals( false, NumberUtils.equals(array1, array2) );
}
public void testEqualsLong() {
long[] array1 = null;
long[] array2 = null;
assertEquals( true, NumberUtils.equals(array1, array2) );
assertEquals( true, NumberUtils.equals(array2, array1) );
array1 = new long[] { 50L, 20L }; // array2 still null
assertEquals( false, NumberUtils.equals(array1, array2) );
assertEquals( false, NumberUtils.equals(array2, array1) );
// test same reference equivalence
array2 = array1;
assertEquals( true, NumberUtils.equals(array1, array2) );
// test object equivalence
array2 = new long[] { 50L, 20L };
assertEquals( true, NumberUtils.equals(array1, array2) );
// test symmetry is not equivalent
array2 = new long[] { 20L, 50L };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test the whole length of rhs is tested against
array2 = new long[] { 50L, 20L, 10L };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test whole length of lhs is tested against
array2 = new long[] { 50L };
assertEquals( false, NumberUtils.equals(array1, array2) );
}
public void testEqualsFloat() {
float[] array1 = null;
float[] array2 = null;
assertEquals( true, NumberUtils.equals(array1, array2) );
assertEquals( true, NumberUtils.equals(array2, array1) );
array1 = new float[] { 50.6f, 20.6f }; // array2 still null
assertEquals( false, NumberUtils.equals(array1, array2) );
assertEquals( false, NumberUtils.equals(array2, array1) );
// test same reference equivalence
array2 = array1;
assertEquals( true, NumberUtils.equals(array1, array2) );
// test object equivalence
array2 = new float[] { 50.6f, 20.6f };
assertEquals( true, NumberUtils.equals(array1, array2) );
// test symmetry is not equivalent
array2 = new float[] { 20.6f, 50.6f };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test the whole length of rhs is tested against
array2 = new float[] { 50.6f, 20.6f, 10.6f };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test whole length of lhs is tested against
array2 = new float[] { 50.6f };
assertEquals( false, NumberUtils.equals(array1, array2) );
}
public void testEqualsDouble() {
double[] array1 = null;
double[] array2 = null;
assertEquals( true, NumberUtils.equals(array1, array2) );
assertEquals( true, NumberUtils.equals(array2, array1) );
array1 = new double[] { 50.6, 20.6 }; // array2 still null
assertEquals( false, NumberUtils.equals(array1, array2) );
assertEquals( false, NumberUtils.equals(array2, array1) );
// test same reference equivalence
array2 = array1;
assertEquals( true, NumberUtils.equals(array1, array2) );
// test object equivalence
array2 = new double[] { 50.6, 20.6 };
assertEquals( true, NumberUtils.equals(array1, array2) );
// test symmetry is not equivalent
array2 = new double[] { 20.6, 50.6 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test the whole length of rhs is tested against
array2 = new double[] { 50.6, 20.6, 10.6 };
assertEquals( false, NumberUtils.equals(array1, array2) );
// test whole length of lhs is tested against
array2 = new double[] { 50.6 };
assertEquals( false, NumberUtils.equals(array1, array2) );
}
// min/max tests
// ----------------------------------------------------------------------
public void testMinLong() {