commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r467482 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/math/NumberUtils.java test/org/apache/commons/lang/math/NumberUtilsTest.java
Date Tue, 24 Oct 2006 21:02:57 GMT
Author: bayard
Date: Tue Oct 24 14:02:56 2006
New Revision: 467482

URL: http://svn.apache.org/viewvc?view=rev&rev=467482
Log:
Adding equals(int[], int[]) style methods to NumberUtils as requested by Paul Benedict in
LANG-238

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java?view=diff&rev=467482&r1=467481&r2=467482
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java
Tue Oct 24 14:02:56 2006
@@ -668,6 +668,170 @@
         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
     //--------------------------------------------------------------------
     /**

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java?view=diff&rev=467482&r1=467481&r2=467482
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
Tue Oct 24 14:02:56 2006
@@ -318,6 +318,194 @@
         }
     }
 
+    // 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() {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message