java 判空工具类

 对java中的一些类型进行判空的工具类:

public class EmptyUtils {

    // 私有EmptyUtils构造类,让外界不能创建对象,只能静态调用方法
    private EmptyUtils() {
        throw new UnsupportedOperationException("you can't instantiate me...");
    }

    /**
     * 功能描述: Object 等于空
     * @date 2018/11/01 15:06
     * @Param [obj]
     * @return boolean
     */
    public static boolean isEmpty(Object obj){
        return obj==null;
    }


    /**
     * 功能描述: Object 不等于空
     * @date 2018/11/01 15:07
     * @Param [obj]
     * @return boolean
     */
    public static boolean isNotEmpty(Object obj){
        return obj!=null;
    }

    /**
     * 功能描述: str 等于空
     * @date 2018/11/01 15:03
     * @Param [str]
     * @return boolean
     */
    public static boolean isEmpty(String str){
        return (str==null || "".equals(str) || (str.trim().length() == 0));
    }


    /**
     * 功能描述: str 不等于空
     * @date 2018/11/01 15:05
     * @Param [str]
     * @return boolean
     */
    public static boolean isNotEmpty(String str){
        return (str!=null && !str.equals(""));
    }

    /**
     * 功能描述: int数字类型为空
     * @date 2018/11/01 15:25
     * @Param [num]
     * @return boolean
     */
    public static boolean isEmpty(Integer num){
        return num == null;
    }

    /**
     * 功能描述: int数字类型不为空
     * @date 2018/11/01 15:25
     * @Param [num]
     * @return boolean
     */
    public static boolean isNotEmpty(Integer num){
        return num != null;
    }
    

    /**
     * 功能描述: 判断list集合为空:list == null 或 list.size ==0  返回 true
     * @date 2018/11/01 15:00
     * @Param [list]
     * @return boolean
     */
    @SuppressWarnings("unchecked")
    public static boolean isEmpty(List list){
        return (list==null || list.size()==0);
    }

    
    /**
     * 功能描述: 判断list集合不为空:list != null 或 list.size >0  返回 true
     * @date 2018/11/01 15:02
     * @Param [list]
     * @return boolean
     */
    @SuppressWarnings("unchecked")
    public static boolean isNotEmpty(List list){
        return (list != null && list.size() > 0);
    }


    /**
     * 功能描述: 对象数组为空
     * @date 2018/11/01 15:09
     * @Param [strings]
     * @return boolean
     */
    public static boolean isEmpty(Object[] obj) {
        return ((obj == null) || (obj.length == 0));
    }


    /**
     * 功能描述: 对象数组不为空
     * @date 2018/11/01 15:23
     * @Param [strings]
     * @return boolean
     */
    public static boolean isNotEmpty(Object[] obj){
        return ((obj != null) && (obj.length > 0));
    }


    /**
     * 功能描述: collection集合为空
     * @date 2018/11/01 15:27
     * @Param [obj]
     * @return boolean
     */
    public static boolean isEmpty(Collection<?> conn) {
        return ((conn == null) || (conn.size() <= 0));
    }

    /**
     * 功能描述: collection集合不为空
     * @date 2018/11/01 15:28
     * @Param [conn]
     * @return boolean
     */
    public static boolean isNotEmpty(Collection<?> conn) {
        return ((conn != null) && (conn.size() > 0));
    }

    /**
     * 功能描述: byte数组为空
     * @date 2018/11/01 15:29
     * @Param [bys]
     * @return boolean
     */
    public static boolean isEmpty(byte[] bys) {
        return ((bys == null) || (bys.length == 0));
    }

    /**
     * 功能描述: byte数组不为空
     * @date 2018/11/01 15:30
     * @Param [bys]
     * @return boolean
     */
    public static boolean isNotEmpty(byte[] bys) {
        return ((bys != null) && (bys.length > 0));
    }

    /**
     * 功能描述: long类型为空
     * @date 2018/11/01 15:30
     * @Param [l]
     * @return boolean
     */
    public static boolean isEmpty(Long l) {
        return ((l == null) || (l.longValue() == 0L));
    }


    /**
     * 功能描述: long类型不为空
     * @date 2018/11/01 15:30
     * @Param [l]
     * @return boolean
     */
    public static boolean isNotEmpty(Long l) {
        return ((l != null) && (l.longValue() > 0L));
    }


    public static void main(String[] args) {
        Object object = null;
        String str=null;
        String[] strArr = {};
        boolean empty = EmptyUtils.isEmpty(object);
        boolean strBol = EmptyUtils.isEmpty(str);
        boolean strArrBol = EmptyUtils.isEmpty(strArr);
        System.out.println(strBol);
        System.out.println("字符数组:" + strArrBol);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值