cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java
psteitz 2003/09/05 08:55:09 Modified:lang/src/java/org/apache/commons/lang/math NumberUtils.java lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java Log: Added stringToDouble, stringToLong functions to NumberUtils. Patch supplied by Fredrik Westermarck Reviewd by Phil Steitz Revision ChangesPath 1.12 +76 -3 jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java Index: NumberUtils.java === RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- NumberUtils.java 4 Sep 2003 07:27:12 - 1.11 +++ NumberUtils.java 5 Sep 2003 15:55:09 - 1.12 @@ -156,6 +156,40 @@ } /** + * pConvert a codeString/code to a codelong/code, returning + * codezero/code if the conversion fails./p + * + * pIf the string is codenull/code, codezero/code is returned./p + * + * @param str the string to convert, may be null + * @return the long represented by the string, or code0/code if + * conversion fails + * @since 2.1 + */ +public static long stringToLong(String str) { +return stringToLong(str, 0L); +} + +/** + * pConvert a codeString/code to a codelong/code, returning a + * default value if the conversion fails./p + * + * pIf the string is codenull/code, the default value is returned./p + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the long represented by the string, or the default if conversion fails + * @since 2.1 + */ +public static long stringToLong(String str, long defaultValue) { +try { +return Long.parseLong(str); +} catch (NumberFormatException nfe) { +return defaultValue; +} +} + +/** * pConvert a codeString/code to a codefloat/code, returning * code0.0f/code if the conversion fails./p * @@ -187,10 +221,49 @@ public static float stringToFloat(String str, float defaultValue) { if(str==null) { return defaultValue; - } - + } try { return Float.parseFloat(str); + } catch (NumberFormatException nfe) { + return defaultValue; + } +} + +/** + * pConvert a codeString/code to a codedouble/code, returning + * code0.0d/code if the conversion fails./p + * + * pIf the string codestr/code is codenull/code, + * code0.0d/code is returned./p + * + * @param str the string to convert, may be codenull/code + * @return the double represented by the string, or code0.0d/code + * if conversion fails + * @since 2.1 + */ +public static double stringToDouble(String str) { +return stringToDouble(str, 0.0d); +} + +/** + * pConvert a codeString/code to a codedouble/code, returning a + * default value if the conversion fails./p + * + * pIf the string codestr/code is codenull/code, the default + * value is returned./p + * + * @param str the string to convert, may be codenull/code + * @param defaultValue the default value + * @return the double represented by the string, or defaultValue + * if conversion fails + * @since 2.1 + */ +public static double stringToDouble(String str, double defaultValue) { + if(str==null) { + return defaultValue; + } + try { + return Double.parseDouble(str); } catch (NumberFormatException nfe) { return defaultValue; } 1.9 +46 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java Index: NumberUtilsTest.java === RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- NumberUtilsTest.java 4 Sep 2003 07:27:12 - 1.8 +++ NumberUtilsTest.java 5 Sep 2003 15:55:09 - 1.9 @@ -124,12 +124,36 @@ } /** + * Test for long stringToLong(String) + */ +public void testStringToLongString() { +assertTrue(stringToLong(String) 1 failed, NumberUtils.stringToLong(12345) == 12345l); +assertTrue(stringToLong(String) 2 failed, NumberUtils.stringToLong(abc) == 0l); +assertTrue(stringToLong(String) 3 failed, NumberUtils.stringToLong(1L) == 0l); +assertTrue(stringToLong(String) 4
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java
psteitz 2003/09/04 00:27:12 Modified:lang/src/java/org/apache/commons/lang/math NumberUtils.java lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java Log: Added stringToFloat to NumberUtils Patch contributed by Fredrik Westermarck Reviewed by Phil Steitz Revision ChangesPath 1.11 +43 -1 jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java Index: NumberUtils.java === RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- NumberUtils.java 18 Aug 2003 02:22:24 - 1.10 +++ NumberUtils.java 4 Sep 2003 07:27:12 - 1.11 @@ -69,6 +69,7 @@ * @author Phil Steitz * @author Matthew Hawthorne * @author a href=mailto:[EMAIL PROTECTED]Gary Gregory/a + * @author a href=mailto:[EMAIL PROTECTED]Fredrik Westermarck/a * @since 2.0 * @version $Id$ */ @@ -152,6 +153,47 @@ } catch (NumberFormatException nfe) { return defaultValue; } +} + +/** + * pConvert a codeString/code to a codefloat/code, returning + * code0.0f/code if the conversion fails./p + * + * pIf the string codestr/code is codenull/code, + * code0.0f/code is returned./p + * + * @param str the string to convert, may be codenull/code + * @return the float represented by the string, or code0.0f/code + * if conversion fails + * @since 2.1 + */ +public static float stringToFloat(String str) { +return stringToFloat(str, 0.0f); +} + +/** + * pConvert a codeString/code to a codefloat/code, returning a + * default value if the conversion fails./p + * + * pIf the string codestr/code is codenull/code, the default + * value is returned./p + * + * @param str the string to convert, may be codenull/code + * @param defaultValue the default value + * @return the float represented by the string, or defaultValue + * if conversion fails + * @since 2.1 + */ +public static float stringToFloat(String str, float defaultValue) { + if(str==null) { + return defaultValue; + } + + try { + return Float.parseFloat(str); + } catch (NumberFormatException nfe) { + return defaultValue; + } } //--- 1.8 +20 -1 jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java Index: NumberUtilsTest.java === RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- NumberUtilsTest.java 18 Aug 2003 02:22:27 - 1.7 +++ NumberUtilsTest.java 4 Sep 2003 07:27:12 - 1.8 @@ -123,6 +123,25 @@ assertTrue(stringToInt(String,int) 2 failed, NumberUtils.stringToInt(1234.5, 5) == 5); } +/** + * Test for float stringToFloat(String) + */ +public void testStringToFloatString() { +assertTrue(stringToFloat(String) 1 failed, NumberUtils.stringToFloat(-1.2345) == -1.2345f); +assertTrue(stringToFloat(String) 2 failed, NumberUtils.stringToFloat(1.2345) == 1.2345f); +assertTrue(stringToFloat(String) 3 failed, NumberUtils.stringToFloat(abc) == 0.0f); +assertTrue(stringToFloat(empty) failed, NumberUtils.stringToFloat() == 0.0f); +assertTrue(stringToFloat(null) failed, NumberUtils.stringToFloat(null) == 0.0f); +} + +/** + * Test for float stringToFloat(String, float) + */ +public void testStringToFloatStringF() { +assertTrue(stringToFloat(String,int) 1 failed, NumberUtils.stringToFloat(1.2345, 5.1f) == 1.2345f); +assertTrue(stringToFloat(String,int) 2 failed, NumberUtils.stringToFloat(a, 5.0f) == 5.0f); +} + public void testCreateNumber() { //a lot of things can go wrong assertEquals(createNumber(String) 1 failed, new Float(1234.5), NumberUtils.createNumber(1234.5)); - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java
ggregory2003/07/26 12:12:30 Modified:lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java Log: Fix http://nagoya.apache.org/bugzilla/show_bug.cgi?id=21904 as suggested (but with the guard clause factored in a method.) Revision ChangesPath 1.5 +62 -13 jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java Index: NumberUtilsTest.java === RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- NumberUtilsTest.java 21 Jul 2003 19:42:12 - 1.4 +++ NumberUtilsTest.java 26 Jul 2003 19:12:30 - 1.5 @@ -72,6 +72,7 @@ * @author Phil Steitz * @author Stephen Colebourne * @author Matthew Hawthorne + * @author a href=mailto:[EMAIL PROTECTED]Gary Gregory/a * @version $Id$ */ public class NumberUtilsTest extends TestCase { @@ -150,9 +151,17 @@ public void testCreateFloat() { assertEquals(createFloat(String) failed, new Float(1234.5), NumberUtils.createFloat(1234.5)); assertEquals(createFloat(null) failed, null, NumberUtils.createFloat(null)); +this.testCreateFloatFailure(); +this.testCreateFloatFailure( ); +this.testCreateFloatFailure(\b\t\n\f\r); +// Funky whitespaces + this.testCreateFloatFailure(\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F); +} + +protected void testCreateFloatFailure(String str) { try { -Float f = NumberUtils.createFloat(); -fail(createFloat(empty) failed); +Float value = NumberUtils.createFloat(str); +fail(createFloat(blank) failed: + value); } catch (NumberFormatException ex) { // empty } @@ -161,9 +170,17 @@ public void testCreateDouble() { assertEquals(createDouble(String) failed, new Double(1234.5), NumberUtils.createDouble(1234.5)); assertEquals(createDouble(null) failed, null, NumberUtils.createDouble(null)); +this.testCreateDoubleFailure(); +this.testCreateDoubleFailure( ); +this.testCreateDoubleFailure(\b\t\n\f\r); +// Funky whitespaces + this.testCreateDoubleFailure(\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F); +} + +protected void testCreateDoubleFailure(String str) { try { -Double d = NumberUtils.createDouble(); -fail(createDouble(empty) failed); +Double value = NumberUtils.createDouble(str); +fail(createDouble(blank) failed: + value); } catch (NumberFormatException ex) { // empty } @@ -172,9 +189,17 @@ public void testCreateInteger() { assertEquals(createInteger(String) failed, new Integer(12345), NumberUtils.createInteger(12345)); assertEquals(createInteger(null) failed, null, NumberUtils.createInteger(null)); +this.testCreateIntegerFailure(); +this.testCreateIntegerFailure( ); +this.testCreateIntegerFailure(\b\t\n\f\r); +// Funky whitespaces + this.testCreateIntegerFailure(\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F); +} + +protected void testCreateIntegerFailure(String str) { try { -Integer i = NumberUtils.createInteger(); -fail(createInteger(empty) failed); +Integer value = NumberUtils.createInteger(str); +fail(createInteger(blank) failed: + value); } catch (NumberFormatException ex) { // empty } @@ -183,9 +208,17 @@ public void testCreateLong() { assertEquals(createLong(String) failed, new Long(12345), NumberUtils.createLong(12345)); assertEquals(createLong(null) failed, null, NumberUtils.createLong(null)); +this.testCreateLongFailure(); +this.testCreateLongFailure( ); +this.testCreateLongFailure(\b\t\n\f\r); +// Funky whitespaces + this.testCreateLongFailure(\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F); +} + +protected void testCreateLongFailure(String str) { try { -Long l = NumberUtils.createLong(); -fail(createLong(empty) failed); +Long value = NumberUtils.createLong(str); +fail(createLong(blank) failed: + value); } catch (NumberFormatException ex) { // empty } @@ -194,9 +227,17 @@ public void testCreateBigInteger() { assertEquals(createBigInteger(String) failed, new BigInteger(12345), NumberUtils.createBigInteger(12345)); assertEquals(createBigInteger(null) failed, null,
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java
scolebourne2003/06/28 11:42:04 Modified:lang/src/java/org/apache/commons/lang/math NumberUtils.java lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java Log: Add min/max by array functions bug 21068, from Matthew Hawthorne Revision ChangesPath 1.2 +282 -3 jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java Index: NumberUtils.java === RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- NumberUtils.java 24 Jun 2003 21:14:50 - 1.1 +++ NumberUtils.java 28 Jun 2003 18:42:03 - 1.2 @@ -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; /** * pProvides extra functionality for Java Number classes./p @@ -65,7 +67,8 @@ * @author a href=mailto:[EMAIL PROTECTED]Steve Downey/a * @author Eric Pugh * @author Phil Steitz - * @since 1.0 + * @author Matthew Hawthorne + * @since 2.0 * @version $Id$ */ public final class NumberUtils { @@ -437,6 +440,281 @@ 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 codearray/code is codenull/code + * @throws IllegalArgumentException if codearray/code 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 codearray/code is codenull/code + * @throws IllegalArgumentException if codearray/code 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 codearray/code is codenull/code + * @throws IllegalArgumentException if codearray/code 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 codearray/code is codenull/code + * @throws IllegalArgumentException if codearray/code 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]; +} +