【Java】Converter(数据类型转换工具类)

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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值