Author: britter
Date: Wed Jul 9 19:21:48 2014
New Revision: 1609273
URL: http://svn.apache.org/r1609273
Log:
LANG-1016: NumberUtils#isParsable method(s). Thanks to Juan Pablo Santos
RodrÃguez.
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1609273&r1=1609272&r2=1609273&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Wed Jul 9
19:21:48 2014
@@ -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>
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1609273&r1=1609272&r2=1609273&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Wed Jul 9 19:21:48 2014
@@ -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
) );
+ }
}
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java?rev=1609273&r1=1609272&r2=1609273&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Wed Jul 9 19:21:48 2014
@@ -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 {