在我尝试重构的那一刻,我已经有了这个方法。
public static boolean isEmpty(Object value) {
boolean empty = false;
if (value instanceof String && ((String) value).isEmpty()) {
empty = true;
} else if (value instanceof List && ((List<?>) value).isEmpty()) {
empty = true;
} else if (value instanceof String[] && ArrayUtils.isEmpty((String[]) value)) {
empty = true;
} else if (value == null) {
empty = true;
}
return empty;
}
Run Code Online (Sandbox Code Playgroud)
有没有明显更好的方法来做到这一点,我错过了?
我知道我可以if使用链式||语句将所有条件放在一个语句上,但我真的不知道这比我现在得到的更好。