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
This commit is contained in:
Henri Yandell 2009-06-25 07:09:30 +00:00
parent 275f3fa088
commit f3ac86a292
2 changed files with 131 additions and 0 deletions

View File

@ -288,6 +288,101 @@ public class NumberUtils {
}
}
//-----------------------------------------------------------------------
/**
* <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

View File

@ -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"));