Java 数据类型转换工具类
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.text.ParseException;
/**
* A simple object converter
* <br>
* 一个简单的数据类型转换工具类
*/
public class Converter {
/**
* Gets a String from an Object in a null-safe manner.
* <p>
* The String is obtained via <code>toString</code>.
*
* @param obj the object to use
* @return the value of the Object as a String, <code>null</code> if null object input
*/
public static String getAsString(final Object obj) {
if (obj != null) {
return obj.toString();
}
return null;
}
/**
* Gets a String from an Object in a null-safe manner.
* <p>
* The String is obtained via <code>toString</code>.
*
* @param obj the object to use
* @param defaultValue what to return if the value is null or if the conversion fails
* @return the value of the Object as a String, <code>defaultValue</code> if null object input
*/
public static String getAsString(final Object obj, final String defaultValue) {
String answer = getAsString(obj);
if (answer == null) {
answer = defaultValue;
}
return answer;
}
/**
* Gets a Number from an Object in a null-safe manner.
* <p>
* If the value is a <code>Number</code> it is returned directly.
* If the value is a <code>String</code> it is converted using
* {@link NumberFormat#parse(String)} on the system default formatter
* returning <code>null</code> if the conversion fails.
* Otherwise, <code>null</code> is returned.
*
* @param obj the object to use
* @return the value of the Object as a Number, <code>null</code> if null object input
*/
public static Number getAsNumber(final Object obj) {
if (obj != null) {
if (obj instanceof Number) {
return (Number) obj;
} else if (obj instanceof Boolean) {
return ((Boolean) obj) ? 1 : 0;
} else if (obj instanceof String) {
try {
return NumberFormat.getInstance().parse((String) obj);
} catch (final ParseException e) {
throw new NumberFormatException("For input string: \"" + obj + "\"");
}
} else {
throw new UnsupportedOperationException();
}
}
return null;
}
/**
* Converting the Object into a number,
* using the default value if the the conversion fails.
*
* @param obj the object to use
* @param defaultValue what to return if the value is null or if the conversion fails
* @return the value of the object as a number, or defaultValue if the
* original value is null, the object is null or the number conversion
* fails
*/
@SuppressWarnings("unchecked")
public static <R extends Number> R getAsNumber(final Object obj, R defaultValue) {
Number answer = getAsNumber(obj);
if (answer == null) {
answer = defaultValue;
}
return (R) answer;
}
/**
* Gets a Boolean from an Object in a null-safe manner.
* <p>
* If the value is a <code>Boolean</code> it is returned directly.
* If the value is a <code>String</code> and it equals 'true' ignoring case
* then <code>true</code> is returned, otherwise <code>false</code>.
* If the value is a <code>Number</code> an integer zero value returns
* <code>false</code> and non-zero returns <code>true</code>.
* Otherwise, <code>null</code> is returned.
*
* @param obj the object to use
* @return the value of the Object as a Boolean, <code>null</code> if null object input
*/
public static Boolean getAsBoolean(final Object obj) {
if (obj != null) {
if (obj instanceof Boolean) {
return (Boolean) obj;
} else if (obj instanceof String) {
return Boolean.valueOf((String) obj);
} else if (obj instanceof Number) {
final Number n = (Number) obj;
return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE;
} else {
throw new UnsupportedOperationException();
}
}
return null;
}
/**
* Gets a Boolean from an Object in a null-safe manner.
* <p>
* If the value is a <code>Boolean</code> it is returned directly.
* If the value is a <code>String</code> and it equals 'true' ignoring case
* then <code>true</code> is returned, otherwise <code>false</code>.
* If the value is a <code>Number</code> an integer zero value returns
* <code>false</code> and non-zero returns <code>true</code>.
* Otherwise, <code>null</code> is returned.
*
* @param obj the object to use
* @param defaultValue what to return if the value is null or if the conversion fails
* @return the value of the Object as a Boolean, <code>defaultValue</code> if null object input
*/
public static Boolean getAsBoolean(final Object obj, final Boolean defaultValue) {
Boolean answer = getAsBoolean(obj);
if (answer == null) {
answer = defaultValue;
}
return answer;
}
/**
* Gets a boolean from an Object in a null-safe manner.
* <p>
* If the value is a <code>Boolean</code> its value is returned.
* If the value is a <code>String</code> and it equals 'true' ignoring case
* then <code>true</code> is returned, otherwise <code>false</code>.
* If the value is a <code>Number</code> an integer zero value returns
* <code>false</code> and non-zero returns <code>true</code>.
* Otherwise, <code>false</code> is returned.
*
* @param obj the object to use
* @return the value of the Object as a Boolean, <code>false</code> if null object input
*/
public static boolean getAsBooleanValue(final Object obj) {
final Boolean booleanObject = getAsBoolean(obj);
if (booleanObject == null) {
return false;
}
return booleanObject.booleanValue();
}
/**
* Gets a boolean from an Object in a null-safe manner.
* <p>
* If the value is a <code>Boolean</code> its value is returned.
* If the value is a <code>String</code> and it equals 'true' ignoring case
* then <code>true</code> is returned, otherwise <code>false</code>.
* If the value is a <code>Number</code> an integer zero value returns
* <code>false</code> and non-zero returns <code>true</code>.
* Otherwise, <code>false</code> is returned.
*
* @param obj the object to use
* @param defaultValue what to return if the value is null or if the conversion fails
* @return the value in the Map as a Boolean, <code>defaultValue</code> if null object input
*/
public static boolean getAsBooleanValue(final Object obj, final boolean defaultValue) {
final Boolean booleanObject = getAsBoolean(obj);
if (booleanObject == null) {
return defaultValue;
}
return booleanObject.booleanValue();
}
/**
* Gets a Byte from an Object in a null-safe manner,
* using the default value if the the conversion fails.
* <p>
* The Byte is obtained