判空工具类

て烟熏妆下的殇ゞ 提交于 2020-01-22 09:00:45
package utils;

import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * 〈判空工具类〉
 * 非空加!关键字即可
 *
 * @author Barrett
 * @version 1.0.0
 * @time 2020/1/6
 */
public class ValidateUtil {

    /**
     * @Author Barrett
     * @Date 10:46 2020/1/6
     * @Description 对字符串进行判空
     * StringUtils.isBlank()可判断某个字段为null,"","  ",三种类型
     */
    public static boolean isBlankToStr(String s) {
        return StringUtils.isBlank(s);
    }

    /**
     * @Author Barrett
     * @Date 10:46 2020/1/6
     * @Description 对日期进行判空
     */
    public static boolean isBlankToDate(Date date) {
        if (null == date) return true;
        return false;
    }

    /**
     * @Author Barrett
     * @Date 10:46 2020/1/6
     * @Description 对集合进行判空
     */
    public static <T> boolean isBlankToCollection(Collection<T> collection) {
        if (null == collection || collection.isEmpty()) return true;
        return false;
    }

    /**
     * @Author Barrett
     * @Date 10:46 2020/1/6
     * @Description 对map集合进行判空
     */
    public static <T> boolean isBlankToMap(Map<T, T> map) {
        if (null == map || map.isEmpty()) return true;
        return false;
    }

    /**
     * @Author Barrett
     * @Date 10:46 2020/1/6
     * @Description 对对象进行判空
     */
    public static boolean isBlankToObject(Object object) {
        if (object == null) return true;
        if (object instanceof String) {
            return isBlankToStr(String.valueOf(object));
        } else if (object instanceof Date) {
            return isBlankToDate((Date) object);
        } else if (object instanceof Collection) {
            return isBlankToCollection((Collection) object);
        } else if (object instanceof Map) {
            return isBlankToMap((Map) object);
        }
        return false;
    }

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!