统一判空封装
别想 用就完了
import org.apache.commons.lang3.StringUtils;
import java.util.*;
@SuppressWarnings("ALL")
public class IsNotNull {
public static <T> boolean get(T t) {
Optional<T> stringOptional = Optional.ofNullable(t);
return stringOptional
.map(a -> !isNullOrEmpty(a)).orElse(false);
}
private static boolean isNullOrEmpty(Object obj) {
if (obj == null)
return true;
if (obj instanceof CharSequence)
return ((CharSequence) obj).length() == 0 || StringUtils.isBlank(((CharSequence) obj));
if (obj instanceof Collection)
return ((Collection) obj).isEmpty()&&((Collection) obj).size()<=0;
if (obj instanceof Map)
return ((Map) obj).isEmpty();
if (obj instanceof Object[]) {
Object[] object = (Object[]) obj;
if (object.length == 0) {
return true;
}
boolean empty = true;
for (int i = 0; i < object.length; i++) {
if (!isNullOrEmpty(object[i])) {
empty = false;
break;
}
}
return empty;
}
return false;
}
public static void main(String[] args) {
List l = new ArrayList();
String [] a = args;
System.out.println(IsNotNull.get(l));
}
}