创建对应类型的 Predicates 的正确方式。
parent
ad01c58633
commit
9b268b668e
|
@ -2,9 +2,13 @@ package xyz.zhouxy.plusone.commons.function;
|
|||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class Predicates<T> {
|
||||
public class Predicates {
|
||||
|
||||
public final Predicate<T> of(Predicate<? super T> predicate) {
|
||||
public static <T> Predicate<T> of(Predicate<? super T> predicate) {
|
||||
return predicate::test;
|
||||
}
|
||||
|
||||
private Predicates() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package xyz.zhouxy.plusone.commons.function;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -11,12 +12,8 @@ class FunctionTests {
|
|||
@Test
|
||||
void test() {
|
||||
String str = "";
|
||||
Predicate<String> predicate = new Predicates<String>().of(this::nonNull)
|
||||
.or(StringUtils::isNotBlank);
|
||||
Assert.isTrue(predicate.test(str), "未通过");
|
||||
}
|
||||
|
||||
boolean nonNull(Object obj) {
|
||||
return obj != null;
|
||||
Predicate<String> predicate = Predicates.<String>of(Objects::nonNull)
|
||||
.and(StringUtils::isNotEmpty);
|
||||
Assert.isFalse(predicate.test(str), "校验应是不通过");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue