Author: bayard
Date: Tue Oct 24 13:51:08 2006
New Revision: 467477

URL: http://svn.apache.org/viewvc?view=rev&rev=467477
Log:
Adding NumberUtils.max(byte[]) and NumberUtils.min(byte[]) as noted in LANG-289

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=467477&r1=467476&r2=467477
==============================================================================
--- 
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 13:51:08 2006
@@ -751,6 +751,33 @@
         return min;
     }
 
+    /**
+     * <p>Returns the minimum value in an array.</p>
+     * 
+     * @param array  an array, must not be null or empty
+     * @return the minimum value in the array
+     * @throws IllegalArgumentException if <code>array</code> is 
<code>null</code>
+     * @throws IllegalArgumentException if <code>array</code> is empty
+     */
+    public static byte min(byte[] array) {
+        // Validates input
+        if (array == null) {
+            throw new IllegalArgumentException("The Array must not be null");
+        } else if (array.length == 0) {
+            throw new IllegalArgumentException("Array cannot be empty.");
+        }
+    
+        // Finds and returns min
+        byte min = array[0];
+        for (int i = 1; i < array.length; i++) {
+            if (array[i] < min) {
+                min = array[i];
+            }
+        }
+    
+        return min;
+    }
+
      /**
      * <p>Returns the minimum value in an array.</p>
      * 
@@ -879,6 +906,33 @@
     
         // Finds and returns max
         short max = array[0];
+        for (int i = 1; i < array.length; i++) {
+            if (array[i] > max) {
+                max = array[i];
+            }
+        }
+    
+        return max;
+    }
+
+    /**
+     * <p>Returns the maximum value in an array.</p>
+     * 
+     * @param array  an array, must not be null or empty
+     * @return the minimum value in the array
+     * @throws IllegalArgumentException if <code>array</code> is 
<code>null</code>
+     * @throws IllegalArgumentException if <code>array</code> is empty
+     */
+    public static byte max(byte[] array) {
+        // Validates input
+        if (array == null) {
+            throw new IllegalArgumentException("The Array must not be null");
+        } else if (array.length == 0) {
+            throw new IllegalArgumentException("Array cannot be empty.");
+        }
+    
+        // Finds and returns max
+        byte max = array[0];
         for (int i = 1; i < array.length; i++) {
             if (array[i] > max) {
                 max = array[i];

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=467477&r1=467476&r2=467477
==============================================================================
--- 
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 13:51:08 2006
@@ -398,6 +398,32 @@
         assertEquals(-10, NumberUtils.min(new short[] { -5, 0, -10, 5, 10 }));
     }
 
+    public void testMinByte() {
+        final byte[] b = null;
+        try {
+            NumberUtils.min(b);
+            fail("No exception was thrown for null input.");
+        } catch (IllegalArgumentException ex) {}
+
+        try {
+            NumberUtils.min(new byte[0]);
+            fail("No exception was thrown for empty input.");
+        } catch (IllegalArgumentException ex) {}
+
+        assertEquals(
+            "min(byte[]) failed for array length 1",
+            5,
+            NumberUtils.min(new byte[] { 5 }));
+
+        assertEquals(
+            "min(byte[]) failed for array length 2",
+            6,
+            NumberUtils.min(new byte[] { 6, 9 }));
+
+        assertEquals(-10, NumberUtils.min(new byte[] { -10, -5, 0, 5, 10 }));
+        assertEquals(-10, NumberUtils.min(new byte[] { -5, 0, -10, 5, 10 }));
+    }
+
     public void testMinDouble() {
         final double[] d = null;
         try {
@@ -552,6 +578,36 @@
             NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
         assertEquals(10, NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
         assertEquals(10, NumberUtils.max(new short[] { -5, 0, 10, 5, -10 }));
+    }
+
+    public void testMaxByte() {
+        final byte[] b = null;
+        try {
+            NumberUtils.max(b);
+            fail("No exception was thrown for null input.");
+        } catch (IllegalArgumentException ex) {}
+
+        try {
+            NumberUtils.max(new byte[0]);
+            fail("No exception was thrown for empty input.");
+        } catch (IllegalArgumentException ex) {}
+
+        assertEquals(
+            "max(byte[]) failed for array length 1",
+            5,
+            NumberUtils.max(new byte[] { 5 }));
+
+        assertEquals(
+            "max(byte[]) failed for array length 2",
+            9,
+            NumberUtils.max(new byte[] { 6, 9 }));
+
+        assertEquals(
+            "max(byte[]) failed for array length 5",
+            10,
+            NumberUtils.max(new byte[] { -10, -5, 0, 5, 10 }));
+        assertEquals(10, NumberUtils.max(new byte[] { -10, -5, 0, 5, 10 }));
+        assertEquals(10, NumberUtils.max(new byte[] { -5, 0, 10, 5, -10 }));
     }
 
     public void testMaxDouble() {



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to