import java.util.Optional; public final class NullUtil { private static final String NULL_STRING = "null"; private static final int NULL_STRING_LENGTH = NULL_STRING.length(); public NullUtil(){} /** * 判断参数是否不为null * @param object 判断参数 * @return 如果object为空值(null)、空串("")或"null" 返回false,否则返回true */ public static boolean isNotNull(Object object){ if(object instanceof String){ String temp = (String)object; final int len = temp.length(); if (len > 0) { return len != NULL_STRING_LENGTH || !NULL_STRING.equalsIgnoreCase(temp); }else{ return false; } }else{ Optional<Object> optUserLogin = Optional.ofNullable(object); return optUserLogin.isPresent(); } } /** * 判断参数是否为null * @param object 判断参数 * @return 如果object为空值(null)、空串("")或"null" 返回true,否则返回false */ public static boolean isNull(Object object){ if(object instanceof String){ String temp = (String)object; final int len = temp.length(); if (len > 0) { return len == NULL_STRING_LENGTH && NULL_STRING.equalsIgnoreCase(temp); }else{ return true; } }else{ Optional<Object> optUserLogin = Optional.ofNullable(object); return !optUserLogin.isPresent(); } } }