Author: niallp
Date: Fri Jan 29 22:59:02 2010
New Revision: 904655
URL: http://svn.apache.org/viewvc?rev=904655&view=rev
Log:
Port LANG-461 to 2.x branch - add toByte() and toShort() methods to NumberUtils
Modified:
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/NumberUtils.java
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/NumberUtilsTest.java
Modified:
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/NumberUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/NumberUtils.java?rev=904655&r1=904654&r2=904655&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/NumberUtils.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/math/NumberUtils.java
Fri Jan 29 22:59:02 2010
@@ -332,6 +332,101 @@
}
}
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Convert a <code>String</code> to a <code>byte</code>, returning
+ * <code>zero</code> if the conversion fails.</p>
+ *
+ * <p>If the string is <code>null</code>, <code>zero</code> is
returned.</p>
+ *
+ * <pre>
+ * NumberUtils.toByte(null) = 0
+ * NumberUtils.toByte("") = 0
+ * NumberUtils.toByte("1") = 1
+ * </pre>
+ *
+ * @param str the string to convert, may be null
+ * @return the byte represented by the string, or <code>zero</code> if
+ * conversion fails
+ */
+ public static byte toByte(String str) {
+ return toByte(str, (byte) 0);
+ }
+
+ /**
+ * <p>Convert a <code>String</code> to a <code>byte</code>, returning a
+ * default value if the conversion fails.</p>
+ *
+ * <p>If the string is <code>null</code>, the default value is
returned.</p>
+ *
+ * <pre>
+ * NumberUtils.toByte(null, 1) = 1
+ * NumberUtils.toByte("", 1) = 1
+ * NumberUtils.toByte("1", 0) = 1
+ * </pre>
+ *
+ * @param str the string to convert, may be null
+ * @param defaultValue the default value
+ * @return the byte represented by the string, or the default if
conversion fails
+ */
+ public static byte toByte(String str, byte defaultValue) {
+ if(str == null) {
+ return defaultValue;
+ }
+ try {
+ return Byte.parseByte(str);
+ } catch (NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * <p>Convert a <code>String</code> to a <code>short</code>, returning
+ * <code>zero</code> if the conversion fails.</p>
+ *
+ * <p>If the string is <code>null</code>, <code>zero</code> is
returned.</p>
+ *
+ * <pre>
+ * NumberUtils.toShort(null) = 0
+ * NumberUtils.toShort("") = 0
+ * NumberUtils.toShort("1") = 1
+ * </pre>
+ *
+ * @param str the string to convert, may be null
+ * @return the short represented by the string, or <code>zero</code> if
+ * conversion fails
+ */
+ public static short toShort(String str) {
+ return toShort(str, (short) 0);
+ }
+
+ /**
+ * <p>Convert a <code>String</code> to an <code>short</code>, returning a
+ * default value if the conversion fails.</p>
+ *
+ * <p>If the string is <code>null</code>, the default value is
returned.</p>
+ *
+ * <pre>
+ * NumberUtils.toShort(null, 1) = 1
+ * NumberUtils.toShort("", 1) = 1
+ * NumberUtils.toShort("1", 0) = 1
+ * </pre>
+ *
+ * @param str the string to convert, may be null
+ * @param defaultValue the default value
+ * @return the short represented by the string, or the default if
conversion fails
+ */
+ public static short toShort(String str, short defaultValue) {
+ if(str == null) {
+ return defaultValue;
+ }
+ try {
+ return Short.parseShort(str);
+ } catch (NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
//-----------------------------------------------------------------------
// must handle Long, Float, Integer, Float, Short,
// BigDecimal, BigInteger and Byte
Modified:
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/NumberUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/NumberUtilsTest.java?rev=904655&r1=904654&r2=904655&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/NumberUtilsTest.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/math/NumberUtilsTest.java
Fri Jan 29 22:59:02 2010
@@ -168,6 +168,42 @@
assertTrue("toDouble(String,int) 2 failed", NumberUtils.toDouble("a",
5.0d) == 5.0d);
}
+ /**
+ * Test for {...@link NumberUtils#toByte(String)}.
+ */
+ public void testToByteString() {
+ assertTrue("toByte(String) 1 failed", NumberUtils.toByte("123") ==
123);
+ assertTrue("toByte(String) 2 failed", NumberUtils.toByte("abc") == 0);
+ assertTrue("toByte(empty) failed", NumberUtils.toByte("") == 0);
+ assertTrue("toByte(null) failed", NumberUtils.toByte(null) == 0);
+ }
+
+ /**
+ * Test for {...@link NumberUtils#toByte(String, byte)}.
+ */
+ public void testToByteStringI() {
+ assertTrue("toByte(String,byte) 1 failed", NumberUtils.toByte("123",
(byte) 5) == 123);
+ assertTrue("toByte(String,byte) 2 failed", NumberUtils.toByte("12.3",
(byte) 5) == 5);
+ }
+
+ /**
+ * Test for {...@link NumberUtils#toShort(String)}.
+ */
+ public void testToShortString() {
+ assertTrue("toShort(String) 1 failed", NumberUtils.toShort("12345") ==
12345);
+ assertTrue("toShort(String) 2 failed", NumberUtils.toShort("abc") ==
0);
+ assertTrue("toShort(empty) failed", NumberUtils.toShort("") == 0);
+ assertTrue("toShort(null) failed", NumberUtils.toShort(null) == 0);
+ }
+
+ /**
+ * Test for {...@link NumberUtils#toShort(String, short)}.
+ */
+ public void testToShortStringI() {
+ assertTrue("toShort(String,short) 1 failed",
NumberUtils.toShort("12345", (short) 5) == 12345);
+ assertTrue("toShort(String,short) 2 failed",
NumberUtils.toShort("1234.5", (short) 5) == 5);
+ }
+
public void testCreateNumber() {
// a lot of things can go wrong
assertEquals("createNumber(String) 1 failed", new Float("1234.5"),
NumberUtils.createNumber("1234.5"));