ggregory 2003/07/26 12:12:04 Modified: lang/src/java/org/apache/commons/lang/math NumberUtils.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 Changes Path 1.7 +31 -11 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.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- NumberUtils.java 26 Jul 2003 15:39:04 -0000 1.6 +++ NumberUtils.java 26 Jul 2003 19:12:03 -0000 1.7 @@ -57,6 +57,7 @@ import java.math.BigInteger; import org.apache.commons.lang.NullArgumentException; +import org.apache.commons.lang.StringUtils; /** * <p>Provides extra functionality for Java Number classes.</p> @@ -68,6 +69,7 @@ * @author Eric Pugh * @author Phil Steitz * @author Matthew Hawthorne + * @author <a href="mailto:[EMAIL PROTECTED]">Gary Gregory</a> * @since 2.0 * @version $Id$ */ @@ -216,12 +218,9 @@ * @throws NumberFormatException if the value cannot be converted */ public static Number createNumber(String str) throws NumberFormatException { - if (str == null) { + if (!validateNumber(str)) { return null; } - if (str.length() == 0) { - throw new NumberFormatException("\"\" is not a valid number."); - } if (str.startsWith("--")) { // this is protection for poorness in java.lang.BigDecimal. // it accepts this as a legal value, but it does not appear @@ -389,7 +388,7 @@ * @throws NumberFormatException if the value cannot be converted */ public static Float createFloat(String str) { - if (str == null) { + if (!validateNumber(str)) { return null; } return Float.valueOf(str); @@ -405,7 +404,7 @@ * @throws NumberFormatException if the value cannot be converted */ public static Double createDouble(String str) { - if (str == null) { + if (!validateNumber(str)) { return null; } return Double.valueOf(str); @@ -423,7 +422,7 @@ */ public static Integer createInteger(String str) { // decode() handles 0xAABD and 0777 (hex and octal) as well. - if (str == null) { + if (!validateNumber(str)) { return null; } return Integer.decode(str); @@ -439,7 +438,7 @@ * @throws NumberFormatException if the value cannot be converted */ public static Long createLong(String str) { - if (str == null) { + if (!validateNumber(str)) { return null; } return Long.valueOf(str); @@ -455,7 +454,7 @@ * @throws NumberFormatException if the value cannot be converted */ public static BigInteger createBigInteger(String str) { - if (str == null) { + if (!validateNumber(str)) { return null; } return new BigInteger(str); @@ -471,12 +470,33 @@ * @throws NumberFormatException if the value cannot be converted */ public static BigDecimal createBigDecimal(String str) { - if (str == null) { + if (!validateNumber(str)) { return null; } return new BigDecimal(str); } + /** + * Checks the validitiy of a <code>String</code> for conversion it to a number. + * <ol> + * <li>If <code>str</code> is <code>null</code>, return <code>false</code>;</li> + * <li>If <code>str</code> is <i>blank</i>, throw a <code>NumberFormatException</code>;</li> + * <li>Otherewise return <code>true</code>.</li> + * </ol> + * + * @param str The <code>String</code> to check. + * @return Whether or not the argument is suitable for conversion. + */ + protected static boolean validateNumber(String str) { + if (str == null) { + return false; + } + if (StringUtils.isBlank(str)) { + throw new NumberFormatException("A blank string is not a valid number."); + } + return true; + } + // Min in array //-------------------------------------------------------------------- /**
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
