From 7ae8a5b6d361f7c2e7c29cc7b16a29791af67966 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 19 Jul 2023 02:38:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/validator/PropertyValidator.java | 32 ++++++------ .../plusone/validator/StringValidator.java | 52 +++++++++---------- .../validator/test/BaseValidatorTest.java | 3 +- .../validator2/test/BaseValidator2Test.java | 25 +++++---- 4 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java index 2adf9b9..a0298d2 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java @@ -87,46 +87,46 @@ abstract class PropertyValidator { return thisObject(); } - // ===== state ===== + // ===== isTrue ===== - public THIS state(Predicate condition) { - return state(condition, "无效的用户输入"); + public THIS isTrue(Predicate condition) { + return isTrue(condition, "无效的用户输入"); } - public THIS state(Predicate condition, String errMsg) { - return state(condition, convertExceptionCreator(errMsg)); + public THIS isTrue(Predicate condition, String errMsg) { + return isTrue(condition, convertExceptionCreator(errMsg)); } - public THIS state( + public THIS isTrue( Predicate condition, Supplier exceptionCreator) { - return state(condition, convertExceptionCreator(exceptionCreator)); + return isTrue(condition, convertExceptionCreator(exceptionCreator)); } - public THIS state( + public THIS isTrue( Predicate condition, Function exceptionCreator) { withRule(condition, exceptionCreator); return thisObject(); } - // ===== state ===== + // ===== isTrue ===== - public THIS state(Collection> conditions) { - return state(conditions, "无效的用户输入"); + public THIS isTrue(Collection> conditions) { + return isTrue(conditions, "无效的用户输入"); } - public THIS state(Collection> conditions, String errMsg) { - return state(conditions, convertExceptionCreator(errMsg)); + public THIS isTrue(Collection> conditions, String errMsg) { + return isTrue(conditions, convertExceptionCreator(errMsg)); } - public THIS state( + public THIS isTrue( Collection> conditions, Supplier exceptionCreator) { - return state(conditions, convertExceptionCreator(exceptionCreator)); + return isTrue(conditions, convertExceptionCreator(exceptionCreator)); } - public THIS state( + public THIS isTrue( Collection> conditions, Function exceptionCreator) { for (Predicate condition : conditions) { diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index f1676b0..77824e3 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -40,75 +40,75 @@ public class StringValidator extends PropertyValidator matchesOr(Pattern[] regexs, String errMsg) { - return matchesOr(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesOne(Pattern[] regexs, String errMsg) { + return matchesOne(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesOr( + public StringValidator matchesOne( Pattern[] regexs, Supplier exceptionCreator) { - return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); + return matchesOne(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesOr( + public StringValidator matchesOne( Pattern[] regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator); + withRule(input -> RegexUtil.matchesOne(input, regexs), exceptionCreator); return this; } - public StringValidator matchesOr(List regexs, String errMsg) { - return matchesOr(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesOne(List regexs, String errMsg) { + return matchesOne(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesOr( + public StringValidator matchesOne( List regexs, Supplier exceptionCreator) { - return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); + return matchesOne(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesOr( + public StringValidator matchesOne( List regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> RegexUtil.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } - // ===== matchesAnd ===== + // ===== matchesAll ===== - public StringValidator matchesAnd(Pattern[] regexs, String errMsg) { - return matchesAnd(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesAll(Pattern[] regexs, String errMsg) { + return matchesAll(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Pattern[] regexs, Supplier exceptionCreator) { - return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + return matchesAll(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Pattern[] regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAnd(input, regexs), exceptionCreator); + withRule(input -> RegexUtil.matchesAll(input, regexs), exceptionCreator); return this; } - public StringValidator matchesAnd(Collection regexs, String errMsg) { - return matchesAnd(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesAll(Collection regexs, String errMsg) { + return matchesAll(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Collection regexs, Supplier exceptionCreator) { - return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + return matchesAll(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Collection regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAnd(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> RegexUtil.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java index 135cf14..6cc351b 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java @@ -3,7 +3,6 @@ package xyz.zhouxy.plusone.validator.test; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.regex.Pattern; import org.junit.jupiter.api.Test; @@ -34,7 +33,7 @@ class RegisterCommandValidator extends BaseValidator { username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username))); ruleForString(RegisterCommand::getAccount) .notNull("请输入邮箱地址或手机号") - .matchesOr(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号"); + .matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号"); ruleForString(RegisterCommand::getCode) .notNull("验证码不能为空") .matches(PatternConsts.CAPTCHA, "验证码不符合规范"); diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java index 9aa5dec..257bd30 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java +++ b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java @@ -3,37 +3,36 @@ package xyz.zhouxy.plusone.validator2.test; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.function.Predicate; -import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; -import xyz.zhouxy.plusone.commons.constant.RegexConsts; +import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import xyz.zhouxy.plusone.commons.function.Predicates; +import xyz.zhouxy.plusone.commons.util.RegexUtil; import xyz.zhouxy.plusone.validator.BaseValidator; class BaseValidator2Test { @Test void testValidate() { - RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", "A1b2C3d4", - "A1b2C3d4", - Arrays.asList(new String[] { "admin", "editor" })); - RegisterCommandValidator2.INSTANCE.validate(registerCommand); + RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", + "A1b2C3d4", "A1b2C3d4", Arrays.asList(new String[] { "admin", "editor" })); + RegisterCommandValidator.INSTANCE.validate(registerCommand); System.out.println(registerCommand); } } -class RegisterCommandValidator2 extends BaseValidator { +class RegisterCommandValidator extends BaseValidator { - static final RegisterCommandValidator2 INSTANCE = new RegisterCommandValidator2(); + static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); - private RegisterCommandValidator2() { + private RegisterCommandValidator() { ruleForString(RegisterCommand::getUsername) - .state(((Predicate) Objects::nonNull) + .isTrue(Predicates.of(Objects::nonNull) .and(StringUtils::isNotEmpty) .and(StringUtils::isNotBlank) - .and(username -> Pattern.matches(RegexConsts.EMAIL, username)), - (username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username)))); + .and(username -> RegexUtil.matches(username, PatternConsts.EMAIL)), + username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); } }