超级好用的通用工具类DataUtil

关注我,升职加薪就是你!
小伙伴们,你们是怎么避免空指针问题的?
下面是我就这方面问题做的一个通用工具类DataUtil。

package com.demo.nacosprovider.common;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * @Author: Paul
 * @Date: 2021/10/07
 * @Function: 通用对象工具类
 */
public class DataUtil {

    /**
     *@author: Paul
     *@time: 2021/10/07 
     *@description: 判断类型是否是基本类型
     *@param: [clazz]
     *@return: boolean
     */
    public static boolean isBaseType(Class<?> clazz) {
        // java基本类型对象
        if (clazz.isPrimitive()) {
            return true;
        }
        // java.lang下对象不处理
        if (clazz.getName().startsWith("java.lang.")) {
            return true;
        }
        return false;
    }

    /**
     *@author: Paul
     *@time: 2021/10/07 
     *@description: 判断对象是否为空
     *@param: [object]
     *@return: boolean
     */
    public static boolean isEmpty(Object object) {
        if (object == null)
            return true;
        // 字符串
        if (object instanceof String) {
            return "".equals(object.toString());
        }
        // 集合
        else if (object instanceof Collection<?>) {
            return ((Collection<?>) object).isEmpty();
        }
        // 数组
        else if (object instanceof Object[]) {
            return ((Object[]) object).length == 0;
        }
        // Map
        else if (object instanceof Map<?, ?>) {
            return ((Map<?, ?>) object).isEmpty();
        }
        return false;
    }

    /**
     *@author: Paul
     *@time: 2021/10/07 
     *@description: 判断对象不为空
     *@param: [value]
     *@return: boolean
     */
    public static boolean isNotEmpty(Object value) {
        return !isEmpty(value);
    }

    public static String toString(Object object) {
        if (object == null)
            return null;
        String value = null;
        if (object instanceof java.util.Date) {
            value = DateUtils.formatString((java.util.Date) object, DateUtils.FULL_DATE_STR);
        } else if (object instanceof String[]) {
            value = toString((String[]) object);
        } else if (object instanceof java.lang.Throwable) {
            value = toString((Throwable) object);
        } else {
            value = object.toString();
        }
        return value;
    }

    public static String toString(String[] array) {
        if (array == null)
            return null;
        if (array.length == 0)
            return "";
        String value = null;
        for (String s : array) {
            if (isEmpty(s))
                value = (value == null) ? s : value + "," + s;
        }
        return value;
    }

    public static String toString(Throwable e) {
        if (e == null)
            return null;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(os));
        String value = os.toString();
        return value;
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 将字符串转为数值类型,非数值转为默认值
     *@param: [value, defaultValue]
     *@return: java.math.BigDecimal
     */
    public static BigDecimal getBigDecimal(String value, Object defaultValue) {
        BigDecimal result = null;
        try {
            result = new BigDecimal(value);
        } catch (Exception e) {
            result = new BigDecimal(defaultValue.toString());
        }
        return result;
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 判断不是所有参数全部为空。true 只要存在某个参数不为空, 否则false
     *@param: [values]
     *@return: boolean
     */
    public static boolean isNotAllEmpty(Object... values) {
        // 不需要校验参数
        if (values == null || values.length == 0) {
            return true;
        }
        return Arrays.asList(values).stream().anyMatch(s -> isNotEmpty(s));
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 判断不是所有参数全部为空
     *@param: [values]
     *@return: boolean
     */
    public static boolean isAllEmpty(Object... values) {
        return !isNotAllEmpty(values);
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 参数解码。当解码异常时,将返回输入参数
     *@param: [value]
     *@return: java.lang.String
     */
    public static String decode(String value) {
        if (isEmpty(value)) {
            return value;
        }
        try {
            return URLDecoder.decode(value, "UTF-8");
        } catch (Exception e) {
            return value;
        }
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 参数URL编码,如果编码失败。将返回输入参数
     *@param: [value]
     *@return: java.lang.String
     */
    public static String encode(String value) {
        if (isEmpty(value)) {
            return value;
        }
        try {
            return URLEncoder.encode(value, "UTF-8");
        } catch (Exception e) {
            return value;
        }
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 合并2个数组,如果为null将返回一个长度为0的数组。
     *@param: [a, b]
     *@return: java.lang.String[]
     */
    public static String[] combine(String[] a, String[] b) {
        // 同时为空,返回长度为0的数组
        if (a == null && b == null) {
            return new String[0];
        }
        // a 和 b 其中一个为空,返回另一个数组
        if (a == null) {
            return b;
        }
        if (b == null) {
            return a;
        }
        int al = a == null ? 0 : a.length;
        int bl = b == null ? 0 : b.length;
        String[] c = new String[al + bl];
        int index = 0;
        for (int i = 0; i < a.length; i++) {
            c[index++] = a[i];
        }
        for (int i = 0; i < b.length; i++) {
            c[index++] = b[i];
        }
        return c;
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 返回Boolean的值。如果value为空时,将返回false
     *@param: [value]
     *@return: boolean
     */
    public static boolean isTrue(Boolean value) {
        return value == null ? false : value;
    }

    /**
     *@author: Paul
     *@time: 2021/10/07
     *@description: 判断数组是否包括指定的值
     *@param: [value, values]
     *@return: boolean
     */
    public static boolean contain(Object value,Object[] values) {
        if(DataUtil.isEmpty(value)) {
            return false;
        }
        if(DataUtil.isEmpty(values)) {
            return false;
        }
        //比较值是否相等
        for(Object v : values) {
            if(eq(v, value)) {
                return true;
            }
        }
        return false;
    }

    public static boolean eq(Object value1,Object value2) {
        if(value1 == null || value2 == null) {
            return false;
        }
        return value1.equals(value2);
    }
}

完事啦。
关注我,升职加薪就是你!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐先生Paul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值