problem description
java.util.function,,
A code that determines whether a field is empty has been rewritten (some business definitions assume that a field is empty by default if it is equal to-1).
,,,:
related codes
/ / Old Code
public static <T> boolean isBlank(T t) {
if (t == null) {
return true;
}
if (t instanceof List) {
if (((List) t).size() == 0) {
return true;
}
} else if (t instanceof Map) {
if (((Map) t).size() == 0) {
return true;
}
if (((Map) t).get("-1") != null && ((Map) t).get("-1").equals("-1")) {
return true;
}
} else if (t instanceof Set) {
if (((Set) t).size() == 0) {
return true;
}
} else if (t instanceof Object[]) {
if (((Object[]) t).length == 0) {
return true;
}
} else if (t instanceof String) {
String str = (String) t;
if (str.length() == 0)
return true;
str = str.trim();
if (str.length() == 0)
return true;
}
return false;
}
public static <T> boolean isinFallback(T t) {
Class<?> c = t.getClass();
Method[] methods = c.getMethods();
try {
for (Method method : methods) {
if (method.getName().equals("getId")) {
Object o = method.invoke(t);
if (o == null) {
return true;
}
if (o.equals(-1L)) {
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return true;
}
return false;
}
/ / New Code
private static final Function<Method[], Method> filter = (s) -> {
for (Method m : s) {
if (m.getName().equals("getId")) {
return m;
} else if (m.getName().equals("getUid")) {
return m;
}
}
return null;
};
private static final Predicate<Object> object = (s) -> s.equals("-1");
private static final Function<Object, Predicate> getPre = (s) -> {
if (s instanceof List) {
return (Predicate<List>) l -> l.size() == 0;
} else if (s instanceof Map) {
return (Predicate<Map>) l -> l.size() == 0 || l.get("-1") != null;
} else if (s instanceof Set) {
return (Predicate<Set>) l -> l.size() == 0;
} else if (s instanceof Object[]) {
return (Predicate<Object[]>) l -> Objects.nonNull(l) && l.length == 0;
} else if (s instanceof String) {
return (Predicate<String>) String::isEmpty;
}
return null;
};
public static <T> boolean isBlank(T t) {
if (t == null) {
return true;
}
if (t instanceof String) {
String str = (String) t;
str = str.trim();
return getPre.apply(str).test(str);
}
Predicate apply = getPre.apply(t);
return apply != null && apply.test(t);
}
public static <T> boolean isinFallback(T t) {
Class<?> c = t.getClass();
Method[] method = c.getMethods();
try {
Method apply = filter.apply(method);
return apply != null && object.or(oj -> oj.equals(-1L)).test(apply.invoke(t));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
Test code:
:
question:
the results of the three methods are inconsistent because I have added some new judgments, so don"t worry about it. The confusion is that the code logic has not changed at all, only the implementation method has been replaced with the new implementation of java8, and the performance is so much worse. So the question is:
1. If the problem is caused by the wrong logic written in my code, where am I wrong?
2. Looking at the method introduction, the class Predicate seems to exist because it can do some judgmental actions, but what is the need to use it when its performance is so slow?
3. If I kill a chicken with an awesome knife, then where is the correct use of several utility classes under the java.util.function package (Predicate, Consumer, Function, Supplier, UnaryOperator, BinaryOperator)?