Renamed stringTo<Type>-metods to to<Type>.

The stringToInt-methods have been deprecated.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137671 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fredrik Westermarck 2003-09-23 15:46:42 +00:00
parent 9622df2db9
commit 00731e8e2a
2 changed files with 132 additions and 70 deletions

View File

@ -71,7 +71,7 @@ import org.apache.commons.lang.StringUtils;
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
* @since 2.0
* @version $Id: NumberUtils.java,v 1.14 2003/09/13 03:11:30 psteitz Exp $
* @version $Id: NumberUtils.java,v 1.15 2003/09/23 15:46:42 fredrik Exp $
*/
public class NumberUtils {
@ -138,9 +138,31 @@ public class NumberUtils {
* @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 @@ public class NumberUtils {
* @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 @@ public class NumberUtils {
* <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 @@ public class NumberUtils {
* 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 @@ public class NumberUtils {
* <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 @@ public class NumberUtils {
* @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 @@ public class NumberUtils {
* <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 @@ public class NumberUtils {
* 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 @@ public class NumberUtils {
* 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 @@ public class NumberUtils {
* 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 @@ public class NumberUtils {
* <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 @@ public class NumberUtils {
* 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 @@ public class NumberUtils {
* 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 @@ public class NumberUtils {
* 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;
}

View File

@ -75,7 +75,7 @@ import org.apache.commons.lang.SystemUtils;
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: NumberUtilsTest.java,v 1.9 2003/09/05 15:55:09 psteitz Exp $
* @version $Id: NumberUtilsTest.java,v 1.10 2003/09/23 15:46:41 fredrik Exp $
*/
public class NumberUtilsTest extends TestCase {
@ -106,7 +106,7 @@ public class NumberUtilsTest extends TestCase {
//---------------------------------------------------------------------
/**
* 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 @@ public class NumberUtilsTest extends TestCase {
}
/**
* 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 @@ public class NumberUtilsTest extends TestCase {
}
/**
* Test for long stringToLong(String)
* Test for {@link NumberUtils#toInt(String, int)}.
*/
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 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 long stringToLong(String, long)
* Test for {@link NumberUtils#toLong(String)}.
*/
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 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 float stringToFloat(String)
* Test for {@link NumberUtils#toLong(String, long)}.
*/
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 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, float)
* Test for {@link NumberUtils#toFloat(String)}.
*/
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 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 double stringToDouble(String)
* Test for {@link NumberUtils#toFloat(String, float)}.
*/
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 {@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() {