java自定义工具类判断对象为空返回默认值
public static boolean isNull(Object obj) {
if (obj instanceof List) {
return ((List<?>) obj).size() == 0;
}
if (obj instanceof Map) {
return ((Map<?, ?>) obj).size() == 0;
}
if (obj instanceof String) {
return "".equals(obj);
}
if (obj instanceof Long) {
return (Long) obj == 0L;
}
if (obj instanceof Set) {
return ((Set<?>) obj).size() == 0;
}
return obj == null;
}
public static <T> T defaults(T obj, T value) {
return isNull(obj) ? value : obj;
}
public static Integer defaults(Integer obj, Integer value) {
return isNull(obj) ? value : obj;
}
public static String defaults(String obj, String value) {
return isNull(obj) ? value : obj;
}