From 55be165435e580d169f6b4e9bc54f3da9cfc4f64 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 28 Jun 2003 18:42:04 +0000 Subject: [PATCH] Add min/max by array functions bug 21068, from Matthew Hawthorne git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137385 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/math/NumberUtils.java | 285 ++++++++++++++++- .../commons/lang/math/NumberUtilsTest.java | 302 +++++++++++++++++- 2 files changed, 583 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/lang/math/NumberUtils.java b/src/java/org/apache/commons/lang/math/NumberUtils.java index aa879fbb7..f1017dfce 100644 --- a/src/java/org/apache/commons/lang/math/NumberUtils.java +++ b/src/java/org/apache/commons/lang/math/NumberUtils.java @@ -53,8 +53,10 @@ */ package org.apache.commons.lang.math; -import java.math.BigInteger; import java.math.BigDecimal; +import java.math.BigInteger; + +import org.apache.commons.lang.NullArgumentException; /** *

Provides extra functionality for Java Number classes.

@@ -65,8 +67,9 @@ import java.math.BigDecimal; * @author Steve Downey * @author Eric Pugh * @author Phil Steitz - * @since 1.0 - * @version $Id: NumberUtils.java,v 1.1 2003/06/24 21:14:50 scolebourne Exp $ + * @author Matthew Hawthorne + * @since 2.0 + * @version $Id: NumberUtils.java,v 1.2 2003/06/28 18:42:03 scolebourne Exp $ */ public final class NumberUtils { @@ -437,6 +440,281 @@ public final class NumberUtils { return bd; } + // Min in array + //-------------------------------------------------------------------- + /** + * Returns the minimum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static long min(long[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns min + long min = array[0]; + for (int i = 1; i < array.length; i++) { + if (array[i] < min) { + min = array[i]; + } + } + + return min; + } + + /** + * Returns the minimum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static int min(int[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns min + int min = array[0]; + for (int j = 1; j < array.length; j++) { + if (array[j] < min) { + min = array[j]; + } + } + + return min; + } + + /** + * Returns the minimum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static short min(short[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns min + short min = array[0]; + for (int i = 1; i < array.length; i++) { + if (array[i] < min) { + min = array[i]; + } + } + + return min; + } + + /** + * Returns the minimum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static double min(double[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns min + double min = array[0]; + for (int i = 1; i < array.length; i++) { + if (array[i] < min) { + min = array[i]; + } + } + + return min; + } + + /** + * Returns the minimum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static float min(float[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns min + float min = array[0]; + for (int i = 1; i < array.length; i++) { + if (array[i] < min) { + min = array[i]; + } + } + + return min; + } + + // Max in array + //-------------------------------------------------------------------- + /** + * Returns the maximum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static long max(long[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns max + long max = array[0]; + for (int j = 1; j < array.length; j++) { + if (array[j] > max) { + max = array[j]; + } + } + + return max; + } + + /** + * Returns the maximum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static int max(int[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns max + int max = array[0]; + for (int j = 1; j < array.length; j++) { + if (array[j] > max) { + max = array[j]; + } + } + + return max; + } + + /** + * Returns the maximum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static short max(short[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // 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; + } + + /** + * Returns the maximum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static double max(double[] array) { + // Validates input + if (array== null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns max + double max = array[0]; + for (int j = 1; j < array.length; j++) { + if (array[j] > max) { + max = array[j]; + } + } + + return max; + } + + /** + * Returns the maximum value in an array. + * + * @param array an array + * @return the minimum value in the array + * @throws NullArgumentException if array is null + * @throws IllegalArgumentException if array is empty + */ + public static float max(float[] array) { + // Validates input + if (array == null) { + throw new NullArgumentException("Array"); + } else if (array.length == 0) { + throw new IllegalArgumentException("Array cannot be empty."); + } + + // Finds and returns max + float max = array[0]; + for (int j = 1; j < array.length; j++) { + if (array[j] > max) { + max = array[j]; + } + } + + return max; + } + + // 3 param min //----------------------------------------------------------------------- /** *

Gets the minimum of three long values.

@@ -538,6 +816,7 @@ public final class NumberUtils { return Math.min(Math.min(a, b), c); } + // 3 param max //----------------------------------------------------------------------- /** *

Gets the maximum of three long values.

diff --git a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java index d3f702cf9..dfa4397ce 100644 --- a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java +++ b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java @@ -59,6 +59,7 @@ import java.math.BigInteger; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import junit.textui.TestRunner; import org.apache.commons.lang.SystemUtils; @@ -70,7 +71,8 @@ import org.apache.commons.lang.SystemUtils; * @author Eric Pugh * @author Phil Steitz * @author Stephen Colebourne - * @version $Id: NumberUtilsTest.java,v 1.1 2003/06/24 21:14:51 scolebourne Exp $ + * @author Matthew Hawthorne + * @version $Id: NumberUtilsTest.java,v 1.2 2003/06/28 18:42:04 scolebourne Exp $ */ public class NumberUtilsTest extends TestCase { @@ -78,6 +80,10 @@ public class NumberUtilsTest extends TestCase { super(name); } + public static void main(String[] args) { + TestRunner.run(suite()); + } + public static Test suite() { TestSuite suite = new TestSuite(NumberUtilsTest.class); suite.setName("NumberUtils Tests"); @@ -162,6 +168,300 @@ public class NumberUtilsTest extends TestCase { assertEquals("createBigDecimal(String) failed", new BigDecimal("1234.5"), NumberUtils.createBigDecimal("1234.5")); } + // min/max tests + // ---------------------------------------------------------------------- + public void testMinLong() { + final long[] l = null; + try { + NumberUtils.min(l); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.min(new long[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "min(long[]) failed for array length 1", + 5, + NumberUtils.min(new long[] { 5 })); + + assertEquals( + "min(long[]) failed for array length 2", + 6, + NumberUtils.min(new long[] { 6, 9 })); + + assertEquals( + "min(long[]) failed for array length 5", + -10, + NumberUtils.min(new long[] { -10, -5, 0, 5, 10 })); + } + + public void testMinInt() { + final int[] i = null; + try { + NumberUtils.min(i); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.min(new int[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "min(int[]) failed for array length 1", + 5, + NumberUtils.min(new int[] { 5 })); + + assertEquals( + "min(int[]) failed for array length 2", + 6, + NumberUtils.min(new int[] { 6, 9 })); + + assertEquals( + "min(int[]) failed for array length 5", + -10, + NumberUtils.min(new int[] { -10, -5, 0, 5, 10 })); + } + + public void testMinShort() { + final short[] s = null; + try { + NumberUtils.min(s); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.min(new short[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "min(short[]) failed for array length 1", + 5, + NumberUtils.min(new short[] { 5 })); + + assertEquals( + "min(short[]) failed for array length 2", + 6, + NumberUtils.min(new short[] { 6, 9 })); + + assertEquals( + "min(short[]) failed for array length 5", + -10, + NumberUtils.min(new short[] { -10, -5, 0, 5, 10 })); + } + + public void testMinDouble() { + final double[] d = null; + try { + NumberUtils.min(d); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.min(new double[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "min(double[]) failed for array length 1", + 5.12, + NumberUtils.min(new double[] { 5.12 }), + 0); + + assertEquals( + "min(double[]) failed for array length 2", + 6.23, + NumberUtils.min(new double[] { 6.23, 9.34 }), + 0); + + assertEquals( + "min(double[]) failed for array length 5", + -10.45, + NumberUtils.min(new double[] { -10.45, -5.56, 0, 5.67, 10.78 }), + 0); + } + + public void testMinFloat() { + final float[] f = null; + try { + NumberUtils.min(f); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.min(new float[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "min(float[]) failed for array length 1", + 5.9f, + NumberUtils.min(new float[] { 5.9f }), + 0); + + assertEquals( + "min(float[]) failed for array length 2", + 6.8f, + NumberUtils.min(new float[] { 6.8f, 9.7f }), + 0); + + assertEquals( + "min(float[]) failed for array length 5", + -10.6f, + NumberUtils.min(new float[] { -10.6f, -5.5f, 0, 5.4f, 10.3f }), + 0); + } + + public void testMaxLong() { + final long[] l = null; + try { + NumberUtils.max(l); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.max(new long[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "max(long[]) failed for array length 1", + 5, + NumberUtils.max(new long[] { 5 })); + + assertEquals( + "max(long[]) failed for array length 2", + 9, + NumberUtils.max(new long[] { 6, 9 })); + + assertEquals( + "max(long[]) failed for array length 5", + 10, + NumberUtils.max(new long[] { -10, -5, 0, 5, 10 })); + } + + public void testMaxInt() { + final int[] i = null; + try { + NumberUtils.max(i); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.max(new int[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "max(int[]) failed for array length 1", + 5, + NumberUtils.max(new int[] { 5 })); + + assertEquals( + "max(int[]) failed for array length 2", + 9, + NumberUtils.max(new int[] { 6, 9 })); + + assertEquals( + "max(int[]) failed for array length 5", + 10, + NumberUtils.max(new int[] { -10, -5, 0, 5, 10 })); + } + + public void testMaxShort() { + final short[] s = null; + try { + NumberUtils.max(s); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.max(new short[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "max(short[]) failed for array length 1", + 5, + NumberUtils.max(new short[] { 5 })); + + assertEquals( + "max(short[]) failed for array length 2", + 9, + NumberUtils.max(new short[] { 6, 9 })); + + assertEquals( + "max(short[]) failed for array length 5", + 10, + NumberUtils.max(new short[] { -10, -5, 0, 5, 10 })); + } + + public void testMaxDouble() { + final double[] d = null; + try { + NumberUtils.max(d); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.max(new double[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "max(double[]) failed for array length 1", + 5.1f, + NumberUtils.max(new double[] { 5.1f }), + 0); + + assertEquals( + "max(double[]) failed for array length 2", + 9.2f, + NumberUtils.max(new double[] { 6.3f, 9.2f }), + 0); + + assertEquals( + "max(double[]) failed for float length 5", + 10.4f, + NumberUtils.max(new double[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }), + 0); + } + + public void testMaxFloat() { + final float[] f = null; + try { + NumberUtils.max(f); + fail("No exception was thrown for null input."); + } catch (IllegalArgumentException ex) {} + + try { + NumberUtils.max(new float[0]); + fail("No exception was thrown for empty input."); + } catch (IllegalArgumentException ex) {} + + assertEquals( + "max(float[]) failed for array length 1", + 5.1f, + NumberUtils.max(new float[] { 5.1f }), + 0); + + assertEquals( + "max(float[]) failed for array length 2", + 9.2f, + NumberUtils.max(new float[] { 6.3f, 9.2f }), + 0); + + assertEquals( + "max(float[]) failed for float length 5", + 10.4f, + NumberUtils.max(new float[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }), + 0); + } + public void testMinimumLong() { assertEquals("minimum(long,long,long) 1 failed", 12345L, NumberUtils.min(12345L, 12345L + 1L, 12345L + 2L)); assertEquals("minimum(long,long,long) 2 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345 + 2L));