All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ludii.excel.utils.ObjectUtils Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/*
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.ludii.excel.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 对象操作工具类,继承 org.apache.commons.lang3.ObjectUtils 类
 *
 * @author ThinkGem
 * @version 2020-3-29
 */
class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {

    /**
     * 转换为 Double 类型
     */
    public static Double toDouble(final Object val) {
        if (val == null) {
            return 0D;
        }
        try {
            String str = val.toString();
            // 乘号
            String multiplySign = "*";
            if (StringUtils.contains(str, multiplySign)) {
                Double number = null;
                double d;
                for (String s : StringUtils.split(str, multiplySign)) {
                    d = Double.parseDouble(StringUtils.trim(s));
                    if (number == null) {
                        number = d;
                    } else {
                        number *= d;
                    }
                }
                return number;
            }
            return Double.parseDouble(StringUtils.trim(str));
        } catch (Exception e) {
            return 0D;
        }
    }

    /**
     * 转换为 Float 类型
     */
    public static Float toFloat(final Object val) {
        return toDouble(val).floatValue();
    }

    /**
     * 转换为 Long 类型
     */
    public static Long toLong(final Object val) {
        return toDouble(val).longValue();
    }

    /**
     * 转换为 Integer 类型
     */
    public static Integer toInteger(final Object val) {
        return toDouble(val).intValue();
    }


    /**
     * 转换为字符串
     */
    public static String toString(final Object obj) {
        return toString(obj, StringUtils.EMPTY);
    }

    /**
     * 转换为字符串,如果对象为空,则使用 defaultVal 值
     */
    public static String toString(final Object obj, final String defaultVal) {
        return obj == null ? defaultVal : obj.toString();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy