当前位置 : 主页 > 编程语言 > java >

强制转换类

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt package com.edu.sky.teaching.provider.utils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import pub.tbc.toolkit.Objs;/** * @author zhaoming * @create 2017-12-11 */public class CastUtil { private static final Logger l
gistfile1.txt
package com.edu.sky.teaching.provider.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pub.tbc.toolkit.Objs;

/**
 * @author zhaoming
 * @create 2017-12-11
 */
public class CastUtil {
    private static final Logger log = LoggerFactory.getLogger(pub.tbc.toolkit.CastUtil.class);
    private static final String MSG_PREFIX = "类型强制转换失败";

    private CastUtil() {
        throw new AssertionError("No " + this.getClass() + " instances for you!");
    }

    private static String msg(String source, Class sourceType, Class targetType) {
        return "类型强制转换失败, 原始数据:[" + source + ", ->" + sourceType.getName() + "]不能转换为目标类型:" + targetType.getName();
    }

    private static void exception(Object source, Class targetType, Exception e) {
        log.error(msg(source.toString(), source.getClass(), targetType));
        log.error("Exception type: {}, Exception message: {}", e.getClass().getName(), e.getMessage());
        throw new RuntimeException(e.getMessage(), e);
    }

    public static String castString(Object obj) {
        return castString(obj, "");
    }

    public static String castString(Object obj, String defaultValue) {
        return obj == null ? defaultValue : String.valueOf(obj);
    }

    public static double castDouble(Object obj) {
        return castDouble(obj, 0.0D);
    }

    public static double castDouble(Object obj, double defaultValue) {
        double doubleValue = defaultValue;
        if (obj != null) {
            String strValue = castString(obj);
            if (Objs.nonEmpty(strValue)) {
                try {
                    doubleValue = Double.parseDouble(strValue);
                } catch (NumberFormatException var7) {
                    exception(strValue, Double.class, var7);
                }
            }
        }

        return doubleValue;
    }

    public static long castLong(Object obj) {
        return castLong(obj, 0L);
    }

    public static long castLong(Object obj, long defaultValue) {
        long longValue = defaultValue;
        if (obj != null) {
            String strValue = castString(obj);
            if (Objs.nonEmpty(strValue)) {
                try {
                    longValue = Long.parseLong(strValue);
                } catch (NumberFormatException var7) {
                    exception(strValue, Long.class, var7);
                }
            }
        }

        return longValue;
    }

    public static int castInt(Object obj) {
        return castInt(obj, 0);
    }

    public static int castInt(Object obj, int defaultValue) {
        int intValue = defaultValue;
        if (obj != null) {
            String strValue = castString(obj);
            if (Objs.nonEmpty(strValue)) {
                try {
                    intValue = Integer.parseInt(strValue);
                } catch (NumberFormatException var5) {
                    exception(strValue, Integer.class, var5);
                }
            }
        }

        return intValue;
    }

    public static boolean castBoolean(Object obj) {
        return castBoolean(obj, false);
    }

    public static boolean castBoolean(Object obj, boolean defaultValue) {
        boolean booleanValue = defaultValue;
        if (obj != null) {
            booleanValue = Boolean.parseBoolean(castString(obj));
        }

        return booleanValue;
    }
}
上一篇:测试代码片段
下一篇:maven的相关命令
网友评论