From 9b268b668e4aad3ab7ae05fca08de7c9681bc171 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 30 Apr 2023 08:56:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=AF=B9=E5=BA=94=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=20Predicates=20=E7=9A=84=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/function/Predicates.java | 8 ++++++-- .../plusone/commons/function/FunctionTests.java | 11 ++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/function/Predicates.java b/src/main/java/xyz/zhouxy/plusone/commons/function/Predicates.java index d0ee32c..b949d18 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/function/Predicates.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/function/Predicates.java @@ -2,9 +2,13 @@ package xyz.zhouxy.plusone.commons.function; import java.util.function.Predicate; -public class Predicates { +public class Predicates { - public final Predicate of(Predicate predicate) { + public static Predicate of(Predicate predicate) { return predicate::test; } + + private Predicates() { + throw new IllegalStateException("Utility class"); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java b/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java index ddf94b1..1ec1353 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java @@ -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 predicate = new Predicates().of(this::nonNull) - .or(StringUtils::isNotBlank); - Assert.isTrue(predicate.test(str), "未通过"); - } - - boolean nonNull(Object obj) { - return obj != null; + Predicate predicate = Predicates.of(Objects::nonNull) + .and(StringUtils::isNotEmpty); + Assert.isFalse(predicate.test(str), "校验应是不通过"); } }