工具类- 对传入判空

/**
* 判断对象是否为空
* 对于Collection,Array,Map,String 会进行长度的空判断
*
* @param obj 对象
* @return {@code true}: 为空<br>{@code false}: 不为空
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof String && obj.toString().length() == 0) {
return true;
}
if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
return true;
}
if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
return true;
}
if (obj instanceof Map && ((Map) obj).isEmpty()) {
return true;
}
return false;
}

/**
* 判断字符串是否为空
* 当字符串为 null, 空串或纯空格时,返回true
*
* @param str 字符串
* @return {@code true}: 为空<br>{@code false}: 不为空
*/
public static boolean isEmptyStrTrim(String str ) {
if (str == null) {
return true;
}

if(str.trim().length() == 0){
return true;
}

return false;
}
posted @ 2019-05-27 15:44  有容奶大  阅读(200)  评论(0)    收藏  举报