Adding NumberUtils.max(byte[]) and NumberUtils.min(byte[]) as noted in LANG-289

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@467477 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2006-10-24 20:51:08 +00:00
parent c1fad46c4c
commit 7a003412c6
2 changed files with 110 additions and 0 deletions

View File

@ -751,6 +751,33 @@ public class NumberUtils {
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>
*
@ -888,6 +915,33 @@ public class NumberUtils {
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];
}
}
return max;
}
/**
* <p>Returns the maximum value in an array.</p>
*

View File

@ -398,6 +398,32 @@ public class NumberUtilsTest extends TestCase {
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 {
@ -554,6 +580,36 @@ public class NumberUtilsTest extends TestCase {
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() {
final double[] d = null;
try {