psteitz     2003/09/05 08:55:09

  Modified:    lang/src/java/org/apache/commons/lang/math NumberUtils.java
               lang/src/test/org/apache/commons/lang/math
                        NumberUtilsTest.java
  Log:
  Added stringToDouble, stringToLong functions to NumberUtils.
  Patch supplied by Fredrik Westermarck
  Reviewd by Phil Steitz
  
  Revision  Changes    Path
  1.12      +76 -3     
jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java
  
  Index: NumberUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- NumberUtils.java  4 Sep 2003 07:27:12 -0000       1.11
  +++ NumberUtils.java  5 Sep 2003 15:55:09 -0000       1.12
  @@ -156,6 +156,40 @@
       }
   
       /**
  +     * <p>Convert a <code>String</code> to a <code>long</code>, returning
  +     * <code>zero</code> if the conversion fails.</p>
  +     *
  +     * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
  +     *
  +     * @param str  the string to convert, may be null
  +     * @return the long represented by the string, or <code>0</code> if
  +     *  conversion fails
  +     * @since 2.1
  +     */
  +    public static long stringToLong(String str) {
  +        return stringToLong(str, 0L);
  +    }
  +
  +    /**
  +     * <p>Convert a <code>String</code> to a <code>long</code>, returning a
  +     * default value if the conversion fails.</p>
  +     *
  +     * <p>If the string is <code>null</code>, the default value is returned.</p>
  +     *
  +     * @param str  the string to convert, may be null
  +     * @param defaultValue  the default value
  +     * @return the long represented by the string, or the default if conversion 
fails
  +     * @since 2.1
  +     */
  +    public static long stringToLong(String str, long defaultValue) {
  +        try {
  +            return Long.parseLong(str);
  +        } catch (NumberFormatException nfe) {
  +            return defaultValue;
  +        }
  +    }
  +
  +    /**
        * <p>Convert a <code>String</code> to a <code>float</code>, returning
        * <code>0.0f</code> if the conversion fails.</p>
        *
  @@ -187,10 +221,49 @@
       public static float stringToFloat(String str, float defaultValue) {
         if(str==null) {
             return defaultValue;
  -      }
  -      
  +      }     
         try {
             return Float.parseFloat(str);
  +      } catch (NumberFormatException nfe) {
  +          return defaultValue;
  +      }
  +    }
  +
  +    /**
  +     * <p>Convert a <code>String</code> to a <code>double</code>, returning
  +     * <code>0.0d</code> if the conversion fails.</p>
  +     *
  +     * <p>If the string <code>str</code> is <code>null</code>,
  +     * <code>0.0d</code> is returned.</p>
  +     *
  +     * @param str the string to convert, may be <code>null</code>
  +     * @return the double represented by the string, or <code>0.0d</code>
  +     *  if conversion fails
  +     * @since 2.1
  +     */
  +    public static double stringToDouble(String str) {
  +        return stringToDouble(str, 0.0d);
  +    }
  +
  +    /**
  +     * <p>Convert a <code>String</code> to a <code>double</code>, returning a
  +     * default value if the conversion fails.</p>
  +     *
  +     * <p>If the string <code>str</code> is <code>null</code>, the default
  +     * value is returned.</p>
  +     *
  +     * @param str the string to convert, may be <code>null</code>
  +     * @param defaultValue the default value
  +     * @return the double represented by the string, or defaultValue
  +     *  if conversion fails
  +     * @since 2.1
  +     */
  +    public static double stringToDouble(String str, double defaultValue) {
  +      if(str==null) {
  +          return defaultValue;
  +      }
  +      try {
  +          return Double.parseDouble(str);
         } catch (NumberFormatException nfe) {
             return defaultValue;
         }
  
  
  
  1.9       +46 -1     
jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
  
  Index: NumberUtilsTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- NumberUtilsTest.java      4 Sep 2003 07:27:12 -0000       1.8
  +++ NumberUtilsTest.java      5 Sep 2003 15:55:09 -0000       1.9
  @@ -124,12 +124,36 @@
       }
   
       /**
  +     * Test for long stringToLong(String)
  +     */
  +    public void testStringToLongString() {
  +        assertTrue("stringToLong(String) 1 failed", 
NumberUtils.stringToLong("12345") == 12345l);
  +        assertTrue("stringToLong(String) 2 failed", NumberUtils.stringToLong("abc") 
== 0l);
  +        assertTrue("stringToLong(String) 3 failed", NumberUtils.stringToLong("1L") 
== 0l);
  +        assertTrue("stringToLong(String) 4 failed", NumberUtils.stringToLong("1l") 
== 0l);
  +        assertTrue("stringToLong(Long.MAX_VALUE) failed", 
NumberUtils.stringToLong(Long.MAX_VALUE+"") == Long.MAX_VALUE);
  +        assertTrue("stringToLong(Long.MIN_VALUE) failed", 
NumberUtils.stringToLong(Long.MIN_VALUE+"") == Long.MIN_VALUE);
  +        assertTrue("stringToLong(empty) failed", NumberUtils.stringToLong("") == 
0l);
  +        assertTrue("stringToLong(null) failed", NumberUtils.stringToLong(null) == 
0l);
  +    }
  +
  +    /**
  +     * Test for long stringToLong(String, long)
  +     */
  +    public void testStringToLongStringL() {
  +        assertTrue("stringToLong(String,long) 1 failed", 
NumberUtils.stringToLong("12345", 5l) == 12345l);
  +        assertTrue("stringToLong(String,long) 2 failed", 
NumberUtils.stringToLong("1234.5", 5l) == 5l);
  +    }
  +
  +    /**
        * Test for float stringToFloat(String)
        */
       public void testStringToFloatString() {
           assertTrue("stringToFloat(String) 1 failed", 
NumberUtils.stringToFloat("-1.2345") == -1.2345f);
           assertTrue("stringToFloat(String) 2 failed", 
NumberUtils.stringToFloat("1.2345") == 1.2345f);
           assertTrue("stringToFloat(String) 3 failed", 
NumberUtils.stringToFloat("abc") == 0.0f);
  +        assertTrue("stringToFloat(Float.MAX_VALUE) failed", 
NumberUtils.stringToFloat(Float.MAX_VALUE+"") ==  Float.MAX_VALUE);
  +        assertTrue("stringToFloat(Float.MIN_VALUE) failed", 
NumberUtils.stringToFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE);
           assertTrue("stringToFloat(empty) failed", NumberUtils.stringToFloat("") == 
0.0f);
           assertTrue("stringToFloat(null) failed", NumberUtils.stringToFloat(null) == 
0.0f);
       }
  @@ -140,6 +164,27 @@
       public void testStringToFloatStringF() {
           assertTrue("stringToFloat(String,int) 1 failed", 
NumberUtils.stringToFloat("1.2345", 5.1f) == 1.2345f);
           assertTrue("stringToFloat(String,int) 2 failed", 
NumberUtils.stringToFloat("a", 5.0f) == 5.0f);
  +    }
  +
  +    /**
  +     * Test for double stringToDouble(String)
  +     */
  +    public void testStringToDoubleString() {
  +        assertTrue("stringToDouble(String) 1 failed", 
NumberUtils.stringToDouble("-1.2345") == -1.2345d);
  +        assertTrue("stringToDouble(String) 2 failed", 
NumberUtils.stringToDouble("1.2345") == 1.2345d);
  +        assertTrue("stringToDouble(String) 3 failed", 
NumberUtils.stringToDouble("abc") == 0.0d);
  +        assertTrue("stringToDouble(Double.MAX_VALUE) failed", 
NumberUtils.stringToDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE);
  +        assertTrue("stringToDouble(Double.MIN_VALUE) failed", 
NumberUtils.stringToDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE);
  +        assertTrue("stringToDouble(empty) failed", NumberUtils.stringToDouble("") 
== 0.0d);
  +        assertTrue("stringToDouble(null) failed", NumberUtils.stringToDouble(null) 
== 0.0d);
  +    }
  +
  +    /**
  +     * Test for double stringToFloat(String, float)
  +     */
  +    public void testStringToDoubleStringD() {
  +        assertTrue("stringToDouble(String,int) 1 failed", 
NumberUtils.stringToDouble("1.2345", 5.1d) == 1.2345d);
  +        assertTrue("stringToDouble(String,int) 2 failed", 
NumberUtils.stringToDouble("a", 5.0d) == 5.0d);
       }
   
       public void testCreateNumber() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to