You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by fr...@apache.org on 2003/09/23 17:46:42 UTC

cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/math NumberUtils.java

fredrik     2003/09/23 08:46:42

  Modified:    lang/src/test/org/apache/commons/lang/math
                        NumberUtilsTest.java
               lang/src/java/org/apache/commons/lang/math NumberUtils.java
  Log:
  Renamed stringTo<Type>-metods to to<Type>.
  The stringToInt-methods have been deprecated.
  
  Revision  Changes    Path
  1.10      +59 -41    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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- NumberUtilsTest.java	5 Sep 2003 15:55:09 -0000	1.9
  +++ NumberUtilsTest.java	23 Sep 2003 15:46:41 -0000	1.10
  @@ -106,7 +106,7 @@
       //---------------------------------------------------------------------
   
       /**
  -     * Test for int stringToInt(String)
  +     * Test for {@link NumberUtils#stringToInt(String)}.
        */
       public void testStringToIntString() {
           assertTrue("stringToInt(String) 1 failed", NumberUtils.stringToInt("12345") == 12345);
  @@ -116,7 +116,17 @@
       }
   
       /**
  -     * Test for int stringToInt(String, int)
  +     * Test for {@link NumberUtils#toInt(String)}.
  +     */
  +    public void testToIntString() {
  +        assertTrue("toInt(String) 1 failed", NumberUtils.toInt("12345") == 12345);
  +        assertTrue("toInt(String) 2 failed", NumberUtils.toInt("abc") == 0);
  +        assertTrue("toInt(empty) failed", NumberUtils.toInt("") == 0);
  +        assertTrue("toInt(null) failed", NumberUtils.toInt(null) == 0);
  +    }
  +
  +    /**
  +     * Test for {@link NumberUtils#stringToInt(String, int)}.
        */
       public void testStringToIntStringI() {
           assertTrue("stringToInt(String,int) 1 failed", NumberUtils.stringToInt("12345", 5) == 12345);
  @@ -124,67 +134,75 @@
       }
   
       /**
  -     * Test for long stringToLong(String)
  +     * Test for {@link NumberUtils#toInt(String, int)}.
  +     */
  +    public void testToIntStringI() {
  +        assertTrue("toInt(String,int) 1 failed", NumberUtils.toInt("12345", 5) == 12345);
  +        assertTrue("toInt(String,int) 2 failed", NumberUtils.toInt("1234.5", 5) == 5);
  +    }
  +
  +    /**
  +     * Test for {@link NumberUtils#toLong(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);
  +    public void testToLongString() {
  +        assertTrue("toLong(String) 1 failed", NumberUtils.toLong("12345") == 12345l);
  +        assertTrue("toLong(String) 2 failed", NumberUtils.toLong("abc") == 0l);
  +        assertTrue("toLong(String) 3 failed", NumberUtils.toLong("1L") == 0l);
  +        assertTrue("toLong(String) 4 failed", NumberUtils.toLong("1l") == 0l);
  +        assertTrue("toLong(Long.MAX_VALUE) failed", NumberUtils.toLong(Long.MAX_VALUE+"") == Long.MAX_VALUE);
  +        assertTrue("toLong(Long.MIN_VALUE) failed", NumberUtils.toLong(Long.MIN_VALUE+"") == Long.MIN_VALUE);
  +        assertTrue("toLong(empty) failed", NumberUtils.toLong("") == 0l);
  +        assertTrue("toLong(null) failed", NumberUtils.toLong(null) == 0l);
       }
   
       /**
  -     * Test for long stringToLong(String, long)
  +     * Test for {@link NumberUtils#toLong(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);
  +    public void testToLongStringL() {
  +        assertTrue("toLong(String,long) 1 failed", NumberUtils.toLong("12345", 5l) == 12345l);
  +        assertTrue("toLong(String,long) 2 failed", NumberUtils.toLong("1234.5", 5l) == 5l);
       }
   
       /**
  -     * Test for float stringToFloat(String)
  +     * Test for {@link NumberUtils#toFloat(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);
  +    public void testToFloatString() {
  +        assertTrue("toFloat(String) 1 failed", NumberUtils.toFloat("-1.2345") == -1.2345f);
  +        assertTrue("toFloat(String) 2 failed", NumberUtils.toFloat("1.2345") == 1.2345f);
  +        assertTrue("toFloat(String) 3 failed", NumberUtils.toFloat("abc") == 0.0f);
  +        assertTrue("toFloat(Float.MAX_VALUE) failed", NumberUtils.toFloat(Float.MAX_VALUE+"") ==  Float.MAX_VALUE);
  +        assertTrue("toFloat(Float.MIN_VALUE) failed", NumberUtils.toFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE);
  +        assertTrue("toFloat(empty) failed", NumberUtils.toFloat("") == 0.0f);
  +        assertTrue("toFloat(null) failed", NumberUtils.toFloat(null) == 0.0f);
       }
   
       /**
  -     * Test for float stringToFloat(String, float)
  +     * Test for {@link NumberUtils#toFloat(String, float)}.
        */
  -    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);
  +    public void testToFloatStringF() {
  +        assertTrue("toFloat(String,int) 1 failed", NumberUtils.toFloat("1.2345", 5.1f) == 1.2345f);
  +        assertTrue("toFloat(String,int) 2 failed", NumberUtils.toFloat("a", 5.0f) == 5.0f);
       }
   
       /**
  -     * Test for double stringToDouble(String)
  +     * Test for {@link NumberUtils#toDouble(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);
  +        assertTrue("toDouble(String) 1 failed", NumberUtils.toDouble("-1.2345") == -1.2345d);
  +        assertTrue("toDouble(String) 2 failed", NumberUtils.toDouble("1.2345") == 1.2345d);
  +        assertTrue("toDouble(String) 3 failed", NumberUtils.toDouble("abc") == 0.0d);
  +        assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils.toDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE);
  +        assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils.toDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE);
  +        assertTrue("toDouble(empty) failed", NumberUtils.toDouble("") == 0.0d);
  +        assertTrue("toDouble(null) failed", NumberUtils.toDouble(null) == 0.0d);
       }
   
       /**
  -     * Test for double stringToFloat(String, float)
  +     * Test for {@link NumberUtils#toDouble(String, double)}.
        */
       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);
  +        assertTrue("toDouble(String,int) 1 failed", NumberUtils.toDouble("1.2345", 5.1d) == 1.2345d);
  +        assertTrue("toDouble(String,int) 2 failed", NumberUtils.toDouble("a", 5.0d) == 5.0d);
       }
   
       public void testCreateNumber() {
  
  
  
  1.15      +73 -29    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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- NumberUtils.java	13 Sep 2003 03:11:30 -0000	1.14
  +++ NumberUtils.java	23 Sep 2003 15:46:42 -0000	1.15
  @@ -138,9 +138,31 @@
        * @param str  the string to convert, may be null
        * @return the int represented by the string, or <code>zero</code> if
        *  conversion fails
  +     * @deprecated Use {@link #toInt(String)}
  +     *  This method will be removed in Commons Lang 3.0
        */
       public static int stringToInt(String str) {
  -        return stringToInt(str, 0);
  +        return toInt(str);
  +    }
  +
  +    /**
  +     * <p>Convert a <code>String</code> to an <code>int</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.toInt(null) = 0
  +     *   NumberUtils.toInt("")   = 0
  +     *   NumberUtils.toInt("1")  = 1
  +     * </pre>
  +     *
  +     * @param str  the string to convert, may be null
  +     * @return the int represented by the string, or <code>zero</code> if
  +     *  conversion fails
  +     */
  +    public static int toInt(String str) {
  +        return toInt(str, 0);
       }
   
       /**
  @@ -158,8 +180,30 @@
        * @param str  the string to convert, may be null
        * @param defaultValue  the default value
        * @return the int represented by the string, or the default if conversion fails
  +     * @deprecated Use {@link #toInt(String, int)}
  +     *  This method will be removed in Commons Lang 3.0
        */
       public static int stringToInt(String str, int defaultValue) {
  +        return toInt(str, defaultValue);
  +    }
  +
  +    /**
  +     * <p>Convert a <code>String</code> to an <code>int</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.toInt(null, 1) = 1
  +     *   NumberUtils.toInt("", 1)   = 1
  +     *   NumberUtils.toInt("1", 0)  = 1
  +     * </pre>
  +     *
  +     * @param str  the string to convert, may be null
  +     * @param defaultValue  the default value
  +     * @return the int represented by the string, or the default if conversion fails
  +     */
  +    public static int toInt(String str, int defaultValue) {
           try {
               return Integer.parseInt(str);
           } catch (NumberFormatException nfe) {
  @@ -174,9 +218,9 @@
        * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
        *
        * <pre>
  -     *   NumberUtils.stringToLong(null) = 0L
  -     *   NumberUtils.stringToLong("")   = 0L
  -     *   NumberUtils.stringToLong("1")  = 1L
  +     *   NumberUtils.toLong(null) = 0L
  +     *   NumberUtils.toLong("")   = 0L
  +     *   NumberUtils.toLong("1")  = 1L
        * </pre>
        *
        * @param str  the string to convert, may be null
  @@ -184,8 +228,8 @@
        *  conversion fails
        * @since 2.1
        */
  -    public static long stringToLong(String str) {
  -        return stringToLong(str, 0L);
  +    public static long toLong(String str) {
  +        return toLong(str, 0L);
       }
   
       /**
  @@ -195,9 +239,9 @@
        * <p>If the string is <code>null</code>, the default value is returned.</p>
        *
        * <pre>
  -     *   NumberUtils.stringToLong(null, 1L) = 1L
  -     *   NumberUtils.stringToLong("", 1L)   = 1L
  -     *   NumberUtils.stringToLong("1", 0L)  = 1L
  +     *   NumberUtils.toLong(null, 1L) = 1L
  +     *   NumberUtils.toLong("", 1L)   = 1L
  +     *   NumberUtils.toLong("1", 0L)  = 1L
        * </pre>
        *
        * @param str  the string to convert, may be null
  @@ -205,7 +249,7 @@
        * @return the long represented by the string, or the default if conversion fails
        * @since 2.1
        */
  -    public static long stringToLong(String str, long defaultValue) {
  +    public static long toLong(String str, long defaultValue) {
           try {
               return Long.parseLong(str);
           } catch (NumberFormatException nfe) {
  @@ -221,9 +265,9 @@
        * <code>0.0f</code> is returned.</p>
        *
        * <pre>
  -     *   NumberUtils.stringToFloat(null)   = 0.0f
  -     *   NumberUtils.stringToFloat("")     = 0.0f
  -     *   NumberUtils.stringToFloat("1.5")  = 1.5f
  +     *   NumberUtils.toFloat(null)   = 0.0f
  +     *   NumberUtils.toFloat("")     = 0.0f
  +     *   NumberUtils.toFloat("1.5")  = 1.5f
        * </pre>
        *
        * @param str the string to convert, may be <code>null</code>
  @@ -231,8 +275,8 @@
        *  if conversion fails
        * @since 2.1
        */
  -    public static float stringToFloat(String str) {
  -        return stringToFloat(str, 0.0f);
  +    public static float toFloat(String str) {
  +        return toFloat(str, 0.0f);
       }
   
       /**
  @@ -243,9 +287,9 @@
        * value is returned.</p>
        *
        * <pre>
  -     *   NumberUtils.stringToFloat(null, 1.1f)   = 1.0f
  -     *   NumberUtils.stringToFloat("", 1.1f)     = 1.1f
  -     *   NumberUtils.stringToFloat("1.5", 0.0f)  = 1.5f
  +     *   NumberUtils.toFloat(null, 1.1f)   = 1.0f
  +     *   NumberUtils.toFloat("", 1.1f)     = 1.1f
  +     *   NumberUtils.toFloat("1.5", 0.0f)  = 1.5f
        * </pre>
        *
        * @param str the string to convert, may be <code>null</code>
  @@ -254,7 +298,7 @@
        *  if conversion fails
        * @since 2.1
        */
  -    public static float stringToFloat(String str, float defaultValue) {
  +    public static float toFloat(String str, float defaultValue) {
         if (str == null) {
             return defaultValue;
         }     
  @@ -273,9 +317,9 @@
        * <code>0.0d</code> is returned.</p>
        *
        * <pre>
  -     *   NumberUtils.stringToDouble(null)   = 0.0d
  -     *   NumberUtils.stringToDouble("")     = 0.0d
  -     *   NumberUtils.stringToDouble("1.5")  = 1.5d
  +     *   NumberUtils.toDouble(null)   = 0.0d
  +     *   NumberUtils.toDouble("")     = 0.0d
  +     *   NumberUtils.toDouble("1.5")  = 1.5d
        * </pre>
        *
        * @param str the string to convert, may be <code>null</code>
  @@ -283,8 +327,8 @@
        *  if conversion fails
        * @since 2.1
        */
  -    public static double stringToDouble(String str) {
  -        return stringToDouble(str, 0.0d);
  +    public static double toDouble(String str) {
  +        return toDouble(str, 0.0d);
       }
   
       /**
  @@ -295,9 +339,9 @@
        * value is returned.</p>
        *
        * <pre>
  -     *   NumberUtils.stringToDouble(null, 1.1d)   = 1.1d
  -     *   NumberUtils.stringToDouble("", 1.1d)     = 1.1d
  -     *   NumberUtils.stringToDouble("1.5", 0.0d)  = 1.5d
  +     *   NumberUtils.toDouble(null, 1.1d)   = 1.1d
  +     *   NumberUtils.toDouble("", 1.1d)     = 1.1d
  +     *   NumberUtils.toDouble("1.5", 0.0d)  = 1.5d
        * </pre>
        *
        * @param str the string to convert, may be <code>null</code>
  @@ -306,7 +350,7 @@
        *  if conversion fails
        * @since 2.1
        */
  -    public static double stringToDouble(String str, double defaultValue) {
  +    public static double toDouble(String str, double defaultValue) {
         if (str == null) {
             return defaultValue;
         }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org