commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r788276 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/math/NumberUtils.java test/org/apache/commons/lang/math/NumberUtilsTest.java
Date Thu, 25 Jun 2009 07:09:30 GMT
Author: bayard
Date: Thu Jun 25 07:09:30 2009
New Revision: 788276

URL: http://svn.apache.org/viewvc?rev=788276&view=rev
Log:
Applying patch from LANG-461 from Vincent Ricard to add toByte and toShort methods to NumberUtils

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java?rev=788276&r1=788275&r2=788276&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java Thu Jun
25 07:09:30 2009
@@ -288,6 +288,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/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java?rev=788276&r1=788275&r2=788276&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/math/NumberUtilsTest.java Thu
Jun 25 07:09:30 2009
@@ -150,6 +150,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"));



Mime
View raw message