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