From f3ac86a292acd3fc57a2100978f3d9d2705238d2 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 25 Jun 2009 07:09:30 +0000 Subject: [PATCH] Applying patch from LANG-461 from Vincent Ricard to add toByte and toShort methods to NumberUtils git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@788276 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/math/NumberUtils.java | 95 +++++++++++++++++++ .../commons/lang/math/NumberUtilsTest.java | 36 +++++++ 2 files changed, 131 insertions(+) diff --git a/src/java/org/apache/commons/lang/math/NumberUtils.java b/src/java/org/apache/commons/lang/math/NumberUtils.java index 6de017c41..e66d05705 100644 --- a/src/java/org/apache/commons/lang/math/NumberUtils.java +++ b/src/java/org/apache/commons/lang/math/NumberUtils.java @@ -288,6 +288,101 @@ public class NumberUtils { } } + //----------------------------------------------------------------------- + /** + *

Convert a String to a byte, returning + * zero if the conversion fails.

+ * + *

If the string is null, zero is returned.

+ * + *
+     *   NumberUtils.toByte(null) = 0
+     *   NumberUtils.toByte("")   = 0
+     *   NumberUtils.toByte("1")  = 1
+     * 
+ * + * @param str the string to convert, may be null + * @return the byte represented by the string, or zero if + * conversion fails + */ + public static byte toByte(String str) { + return toByte(str, (byte) 0); + } + + /** + *

Convert a String to a byte, returning a + * default value if the conversion fails.

+ * + *

If the string is null, the default value is returned.

+ * + *
+     *   NumberUtils.toByte(null, 1) = 1
+     *   NumberUtils.toByte("", 1)   = 1
+     *   NumberUtils.toByte("1", 0)  = 1
+     * 
+ * + * @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; + } + } + + /** + *

Convert a String to a short, returning + * zero if the conversion fails.

+ * + *

If the string is null, zero is returned.

+ * + *
+     *   NumberUtils.toShort(null) = 0
+     *   NumberUtils.toShort("")   = 0
+     *   NumberUtils.toShort("1")  = 1
+     * 
+ * + * @param str the string to convert, may be null + * @return the short represented by the string, or zero if + * conversion fails + */ + public static short toShort(String str) { + return toShort(str, (short) 0); + } + + /** + *

Convert a String to an short, returning a + * default value if the conversion fails.

+ * + *

If the string is null, the default value is returned.

+ * + *
+     *   NumberUtils.toShort(null, 1) = 1
+     *   NumberUtils.toShort("", 1)   = 1
+     *   NumberUtils.toShort("1", 0)  = 1
+     * 
+ * + * @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 diff --git a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java index 2cc255342..3a4287abb 100644 --- a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java +++ b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java @@ -150,6 +150,42 @@ public class NumberUtilsTest extends TestCase { 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"));