java常用判空工具类

本文介绍了一个实用的空判断工具类,包含多种类型的空值检查方法,如字符串、集合、数组等,并提供了实体类中字段的空值判断逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

空判断工具类,object对象判空,非空,数组判空,多个参数判空,实体类判空等等。

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * ==================================================
 * <p>
 * FileName: EmptyChecker
 *
 * @Author miss you BUG
 * @Date 2021/8/12 16:11
 * @Email rkj01135525@cainiao.com
 * @since 1.0.0
 * 〈功能〉:空校验辅助类

 * ==================================================
 */
public class EmptyChecker {

    private EmptyChecker() {
    }

     /**
     *========================================

     * @方法说明 : 空判断 空返回true
     * @author : miss you BUG
     * @param obj
     * @return      boolean
     * @exception
     * @创建时间:     2020/7/9 11:14

     *========================================
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null || "".equals(obj.toString())) {
            return true;
        }

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

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

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

        return false;
    }
    
     /**
     *========================================
     * @方法说明 : 判断非空 非空返回true
     * @author : miss you BUG
     * @param obj
     * @return      boolean
     * @exception
     * @创建时间:     2020/7/9 11:14

     *========================================
     */
    public static boolean notEmpty(Object obj) {
        return !isEmpty(obj);
    }

     /**
     *========================================
     * @方法说明 :数组判空 空返回true
     * @author : miss you BUG
     * @param array 数组
     * @return      boolean
     * @exception
     * @创建时间:     2020/7/9 11:14

     *========================================
     */
    public static boolean isEmpty(Object[] array) {
        if (array == null || array.length == 0) {
            return true;
        }

        return false;
    }

    /**
     *========================================

     * @方法说明 : 如果任意一个参数为空 返回true
     * @author : miss you BUG
     * @param obj
     * @return      boolean
     * @exception
     * @创建时间:     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;
    }

    /**
     *========================================

     * @方法说明 : 如果所有参数为空 返回true
     * @author : miss you BUG
     * @param obj
     * @return      boolean
     * @exception
     * @创建时间:     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;
    }

    /**
     *========================================
     *
     * @方法说明 : 类 空判断 其中一个值为空返回true
     * @author : miss you BUG
     * @param t   bean
     * @return      boolean
     * @exception
     * @创建时间:     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;
    }

    /**
     *========================================
     *
     * @方法说明 : 类 空判断 所有值为空返回true
     * @author : miss you BUG
     * @param t   bean
     * @return      boolean
     * @exception
     * @创建时间:     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) {
        if (obj instanceof Date) {
            return localDateTimeToString(toLocalDateTime((Date) obj));
        }
        if (obj instanceof LocalDate) {
            return localDateToString((LocalDate) obj);
        }
        if (obj instanceof LocalDateTime) {
            return localDateTimeToString((LocalDateTime) obj);
        }
        return isEmpty(obj)?"":String.valueOf(obj);
    }

    private static LocalDateTime toLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    private static String localDateToString(LocalDate date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return date.format(formatter);
    }

    private static String localDateTimeToString(LocalDateTime dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值