import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class EmptyUtil {
/**
* 提供字符串,集合,数组,map等常见对象判空处理
*
* @param obj
* @return
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof String) {
String str = (String) obj;
return "".equals(str.trim());
}
if (obj instanceof Number) {
Number num = (Number) obj;
return num.byteValue() == 0;
}
if (obj instanceof Collection) {
Collection col = (Collection) obj;
return col.isEmpty();
}
if (obj instanceof Map) {
Map map = (Map) obj;
return map.isEmpty();
}
if (obj.getClass().getSimpleName().endsWith("[]")) {
List<Object> list = Arrays.asList(obj);
Object[] objs = (Object[]) list.get(0);
return objs.length == 0;
}
return false;
}
/**
* 提供字符串,集合,数组,map等常见对象判空处理
*
* @param obj
* @return
*/
public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
}
java判空工具类
最新推荐文章于 2024-07-22 14:15:06 发布