LANG-1016: NumberUtils#isParsable method(s). Thanks to Juan Pablo Santos Rodríguez.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1609273 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2014-07-09 19:21:48 +00:00
parent 4fcfad957d
commit 7022c194e0
3 changed files with 40 additions and 0 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-1016" type="add" dev="britter" due-to="Juan Pablo Santos Rodríguez">NumberUtils#isParsable method(s)</action>
<action issue="LANG-1017" type="update" dev="britter" due-to="Christoph Schneegans">Use non-ASCII digits in Javadoc examples for StringUtils.isNumeric</action>
<action issue="LANG-1008" type="update" dev="britter" due-to="Thiago Andrade">Change min/max methods in NumberUtils/IEEE754rUtils from array input parameters to varargs</action>
<action issue="LANG-999" type="add" dev="britter" due-to="Ben Ripkens">Add fuzzy String matching logic to StringUtils</action>

View File

@ -1466,5 +1466,28 @@ public class NumberUtils {
// found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
return !allowSigns && foundDigit;
}
/**
* <p>Checks whether the String is a parseable number.</p>
*
* <p>Parseable numbers include those Strings understood by <code>Integer.parseInt(String)</code>,
* <code>Long.parseLong(String)</code>, <code>Float.parseFloat(String)</code> or
* <code>Double.parseDouble(String)</code>.</p>
*
* <p>Hexadecimal and scientific notations are <strong>not</strong> considered parseable.
* See {@link #isNumber(String)} on those cases.</p>
*
* <p><code>Null</code> and empty String will return <code>false</code>.</p>
*
* @param str the <code>String</code> to check.
* @return <code>true</code> if the string is a parseable number.
* @since 3.4
*/
public static boolean isParsable(final String str) {
if( StringUtils.endsWith( str, "." ) ) {
return false;
}
return isDigits( StringUtils.replaceOnce( str, ".", StringUtils.EMPTY ) );
}
}

View File

@ -1254,6 +1254,22 @@ public class NumberUtilsTest {
}
fail("Expecting "+ expected + " for isNumber/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
}
@Test
public void testIsParsable() {
assertFalse( NumberUtils.isParsable(null) );
assertFalse( NumberUtils.isParsable("") );
assertFalse( NumberUtils.isParsable("0xC1AB") );
assertFalse( NumberUtils.isParsable("65CBA2") );
assertFalse( NumberUtils.isParsable("pendro") );
assertFalse( NumberUtils.isParsable("64,2") );
assertFalse( NumberUtils.isParsable("64.2.2") );
assertFalse( NumberUtils.isParsable("64.") );
assertFalse( NumberUtils.isParsable("64L") );
assertTrue( NumberUtils.isParsable("64.2") );
assertTrue( NumberUtils.isParsable("64") );
assertTrue(NumberUtils.isParsable("018"));
}
private boolean checkCreateNumber(final String val) {
try {