java判空工具类


复制代码
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * @program: 
 * @description:
 * @author: Dong
 * @create: 2021-04-07 18:08
 **/
public class EmptyUtils {
    private EmptyUtils() {
    }

    /**
     * ========================================
     *
     * @param obj
     * @return boolean
     * @throws
     * @方法说明 : 空判断 空返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 11:14
     * ========================================
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null || "null".equals(obj.toString()) || "".equals(obj.toString())) {
            return true;
        }

        if (obj instanceof String) {
            return ((String) obj).trim().length() == 0;
        }

        if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        }

        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        }

        return false;
    }

    /**
     * ========================================
     *
     * @param obj
     * @return boolean
     * @throws
     * @方法说明 : 判断非空 非空返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 11:14
     * ========================================
     */
    public static boolean notEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * ========================================
     *
     * @param array 数组
     * @return boolean
     * @throws
     * @方法说明 :数组判空 空返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 11:14
     * ========================================
     */
    public static boolean isEmpty(Object[] array) {
        if (array == null || array.length == 0) {
            return true;
        }

        return false;
    }

    /**
     * ========================================
     *
     * @param obj
     * @return boolean
     * @throws
     * @方法说明 : 如果任意一个参数为空 返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 11:14
     * ========================================
     */
    public static boolean isAnyOneEmpty(Object... obj) {
        for (int i = 0; i < obj.length; i++) {
            boolean temp = isEmpty(obj[i]);
            if (temp) {
                return true;
            }
        }

        return false;
    }

    /**
     * ========================================
     *
     * @param obj
     * @return boolean
     * @throws
     * @方法说明 : 如果所有参数为空 返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 12:14
     * ========================================
     */
    public static boolean isAllEmpty(Object... obj) {
        for (int i = 0; i < obj.length; i++) {
            boolean temp = notEmpty(obj[i]);
            if (temp) {
                return false;
            }
        }

        return true;
    }

    /**
     * ========================================
     *
     * @param t bean
     * @return boolean
     * @throws
     * @方法说明 : 类 空判断 其中一个值为空返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 12:20
     * ========================================
     */
    public static <T> boolean beanIsEmpty(T t) {
        if (notEmpty(t)) {
            Field[] fields = t.getClass().getDeclaredFields();
            for (Field obj : fields) {
                if (isEmpty(getBeanValue(t, obj))) {
                    return true;

                }
            }
            return false;
        }

        return true;
    }

    /**
     * ========================================
     *
     * @param t bean
     * @return boolean
     * @throws
     * @方法说明 : 类 空判断 所有值为空返回true
     * @author : miss you BUG
     * @创建时间: 2020/7/9 14:14
     * ========================================
     */
    public static <T> boolean beanIsAllEmpty(T t) {
        if (notEmpty(t)) {
            Field[] fields = t.getClass().getDeclaredFields();
            int num = 0;
            for (Field obj : fields) {
                if (isEmpty(getBeanValue(t, obj))) {
                    num++;
                }
            }
            if (num != fields.length) {
                return false;
            }
        }
        return true;
    }

    //通过反射拿到值
    private static String getBeanValue(Object obj, Field field) {
        try {
            //设置放开私有变量
            field.setAccessible(true);
            //获取属性的名字
            String name = field.getName();
            //将属性名字首字母大写
            name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
            //整合出属性的get方法
            Method m = obj.getClass().getMethod("get" + name);

            return dataCheck(m.invoke(obj));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //处理时间格式的参数
    private static String dataCheck(Object obj) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return (obj instanceof Date || obj instanceof LocalDate) ? simpleFormat.format(obj) : String.valueOf(obj);
    }
}
复制代码

 

 
posted @   照旧  阅读(633)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示