对象判断空 不同类型判空

对象判断空  不同类型判空

package com.v.common.str;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * 类 : <code>DwStringContrast.java</code>
 * 描述: <code>对象判断空</code>
 *
 * @author   作者: {TinVie}
 * @version  版本: {V1.0 ,2015年3月25日 下午2:50:51}
 * @since    JDK版本: {jdk 1.6}
 */
public class ObjectUtils {
	
	/**
     * 判断字符串不为空
     * @param str
     * @return
     */
    public static boolean notEmpty(String str){
        return str != null && !"".equals(str);
    }
    
    /**
     * 判断字符串不为空
     * jdk StringUtils工具类实现如下所示
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }
    
    /**
     * 判断字符串为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        return str == null || str.length() == 0;
    }
    
    /**
     * 集合判断是否为空
     * @param collection 使用泛型
     * @return
     */
    public static <T> boolean notEmpty(Collection<T> collection){
        if(collection != null){
            Iterator<T> iterator = collection.iterator();
            if(iterator != null){
                while(iterator.hasNext()){
                    Object next = iterator.next();
                    if(next != null){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    /**
     * map集合不为空的判断
     * @param map 使用泛型,可以传递不同的类型参数
     * @return
     */
    public static <T> boolean notEmpty(Map<T, T> map){
        return map != null && !map.isEmpty(); 
    }
    
    /**
     * byte类型数组判断不为空
     * @param t
     * @return
     */
    public static boolean notEmpty(byte[] t){
        return t != null && t.length > 0;
    }
    
    /**
     * short类型数组不为空判断
     * @param t
     * @return
     */
    public static boolean notEmpty(short[] t){
        return t != null && t.length > 0;
    }
    
    /**
     * 数组判断不为空,没有泛型数组,所以还是分开写吧
     * @param t 可以是int,short,byte,String,Object,long
     * @return 
     */
    public static boolean notEmpty(int[] t){
        return t != null && t.length > 0;
    }
    
    /**
     * long类型数组不为空
     * @param t
     * @return
     */
    public static boolean notEmpty(long[] t){
        return t != null && t.length > 0;
    }
    
    /**
     * String类型的数组不为空
     * @param t
     * @return
     */
    public static boolean notEmpty(String[] t){
        return t != null && t.length > 0;
    }
    
    /**
     * Object类型数组不为空
     * @param t
     * @return
     */
    public static boolean notEmpty(Object[] t){
        return t != null && t.length > 0;
    }
    
    /**
     * 
     * @param o
     * @return
     */
    public static boolean notEmpty(Object o){
        return o != null && !"".equals(o) && !"null".equals(o); 
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值