From cc6e3a41c38ebd27477bb2e59ca15469cbc73b7a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 10 Dec 2022 03:42:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20BaseValidator2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator/validator2/BaseValidator2.java | 9 +- .../validator/validator2/ValueValidator.java | 177 ++++++++++++------ 2 files changed, 128 insertions(+), 58 deletions(-) diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java index 843672a..257af16 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java @@ -6,16 +6,17 @@ import java.util.function.Function; public abstract class BaseValidator2 { - private List> hs = new ArrayList<>(); + private List> valueValidators = new ArrayList<>(); protected final ValueValidator ruleFor(Function getter) { ValueValidator validValueHolder = new ValueValidator<>(getter); - hs.add(validValueHolder); + valueValidators.add(validValueHolder); return validValueHolder; } public void validate(T obj) { - this.hs.forEach(valueValidator -> valueValidator.validateProperty(obj)); + for (ValueValidator valueValidator : this.valueValidators) { + valueValidator.validateProperty(obj); + } } } - diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java index 69b1467..933db5c 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java @@ -4,31 +4,35 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; +import cn.hutool.core.util.StrUtil; import xyz.zhouxy.plusone.constant.RegexConsts; import xyz.zhouxy.plusone.exception.InvalidInputException; import xyz.zhouxy.plusone.util.RegexUtil; public class ValueValidator { Function getter; - List> rules = new ArrayList<>(); + List> rules = new ArrayList<>(); public ValueValidator(Function getter) { this.getter = getter; } - private void withRule(Predicate condition, String errMsg) { - withRule(condition, value -> new InvalidInputException(errMsg)); - } - private void withRule(Predicate condition, Function exceptionCreator) { - this.rules.add(new RuleInfo<>(condition, exceptionCreator)); + withRule(value -> { + if (!condition.test(value)) { + throw exceptionCreator.apply(value); + } + }); + } + + private void withRule(Consumer rule) { + this.rules.add(rule); } // ==================== @@ -37,12 +41,16 @@ public class ValueValidator { // ====== notNull ===== + public ValueValidator notNull() { + return notNull("Value could not be null."); + } + public ValueValidator notNull(String errMsg) { - return notNull(value -> new InvalidInputException(errMsg)); + return notNull(convertExceptionCreator(errMsg)); } public ValueValidator notNull(Supplier exceptionCreator) { - return notNull(value -> exceptionCreator.get()); + return notNull(convertExceptionCreator(exceptionCreator)); } public ValueValidator notNull(Function exceptionCreator) { @@ -53,11 +61,11 @@ public class ValueValidator { // ====== isNull ===== public ValueValidator isNull(String errMsg) { - return isNull(value -> new InvalidInputException(errMsg)); + return isNull(convertExceptionCreator(errMsg)); } public ValueValidator isNull(Supplier exceptionCreator) { - return isNull(value -> exceptionCreator.get()); + return isNull(convertExceptionCreator(exceptionCreator)); } public ValueValidator isNull(Function exceptionCreator) { @@ -72,12 +80,12 @@ public class ValueValidator { } public ValueValidator equalsThat(Object that, String errMsg) { - return equalsThat(that, value -> new InvalidInputException(errMsg)); + return equalsThat(that, convertExceptionCreator(errMsg)); } public ValueValidator equalsThat( Object that, Supplier exceptionCreator) { - return equalsThat(that, value -> exceptionCreator.get()); + return equalsThat(that, convertExceptionCreator(exceptionCreator)); } public ValueValidator equalsThat( @@ -89,17 +97,17 @@ public class ValueValidator { // ===== state ===== public ValueValidator state(Predicate condition) { - return state(condition, value -> new InvalidInputException("无效的用户输入")); + return state(condition, "无效的用户输入"); } public ValueValidator state(Predicate condition, String errMsg) { - return state(condition, value -> new InvalidInputException(errMsg)); + return state(condition, convertExceptionCreator(errMsg)); } public ValueValidator state( Predicate condition, Supplier exceptionCreator) { - return state(condition, value -> exceptionCreator.get()); + return state(condition, convertExceptionCreator(exceptionCreator)); } public ValueValidator state( @@ -114,16 +122,16 @@ public class ValueValidator { // ================= public ValueValidator between(int min, int max) { - return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max)); + return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); } public ValueValidator between(int min, int max, String errMsg) { - return between(min, max, value -> new InvalidInputException(errMsg)); + return between(min, max, convertExceptionCreator(errMsg)); } public ValueValidator between(int min, int max, Supplier exceptionCreator) { - return between(min, max, value -> exceptionCreator.get()); + return between(min, max, convertExceptionCreator(exceptionCreator)); } public ValueValidator between(int min, int max, @@ -132,6 +140,29 @@ public class ValueValidator { return this; } + // ==================== + // ====== double ====== + // ==================== + + public ValueValidator between(double min, double max) { + return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); + } + + public ValueValidator between(double min, double max, String errMsg) { + return between(min, max, convertExceptionCreator(errMsg)); + } + + public ValueValidator between(double min, double max, + Supplier exceptionCreator) { + return between(min, max, convertExceptionCreator(exceptionCreator)); + } + + public ValueValidator between(double min, double max, + Function exceptionCreator) { + withRule(value -> ((double) value >= min && (double) value < max), exceptionCreator); + return this; + } + // ================================ // ====== Collection, String ====== // ================================ @@ -139,11 +170,11 @@ public class ValueValidator { // ====== notEmpty ===== public ValueValidator notEmpty(String errMsg) { - return notEmpty(value -> new InvalidInputException(errMsg)); + return notEmpty(convertExceptionCreator(errMsg)); } public ValueValidator notEmpty(Supplier exceptionCreator) { - return notEmpty(value -> exceptionCreator.get()); + return notEmpty(convertExceptionCreator(exceptionCreator)); } public ValueValidator notEmpty(Function exceptionCreator) { @@ -165,11 +196,11 @@ public class ValueValidator { // ====== isEmpty ===== public ValueValidator isEmpty(String errMsg) { - return isEmpty(value -> new InvalidInputException(errMsg)); + return isEmpty(convertExceptionCreator(errMsg)); } public ValueValidator isEmpty(Supplier exceptionCreator) { - return isEmpty(value -> exceptionCreator.get()); + return isEmpty(convertExceptionCreator(exceptionCreator)); } public ValueValidator isEmpty(Function exceptionCreator) { @@ -195,15 +226,15 @@ public class ValueValidator { // ====== isTrue ====== public ValueValidator isTrue() { - return isTrue(value -> new InvalidInputException("Value must be true.")); + return isTrue("The value must be true."); } public ValueValidator isTrue(String errMsg) { - return isTrue(value -> new InvalidInputException(errMsg)); + return isTrue(convertExceptionCreator(errMsg)); } public ValueValidator isTrue(Supplier exceptionCreator) { - return isTrue(value -> exceptionCreator.get()); + return isTrue(convertExceptionCreator(exceptionCreator)); } public ValueValidator isTrue(Function exceptionCreator) { @@ -214,15 +245,15 @@ public class ValueValidator { // ====== isFalse ====== public ValueValidator isFalse() { - return isFalse(value -> new InvalidInputException("Value must be true.")); + return isFalse("The value must be false."); } public ValueValidator isFalse(String errMsg) { - return isFalse(value -> new InvalidInputException(errMsg)); + return isFalse(convertExceptionCreator(errMsg)); } public ValueValidator isFalse(Supplier exceptionCreator) { - return isFalse(value -> exceptionCreator.get()); + return isFalse(convertExceptionCreator(exceptionCreator)); } public ValueValidator isFalse(Function exceptionCreator) { @@ -237,15 +268,17 @@ public class ValueValidator { // ===== matches ===== public ValueValidator matches(String regex, String errMsg) { - return matches(regex, value -> new InvalidInputException(errMsg)); + return matches(regex, convertExceptionCreator(errMsg)); } - public ValueValidator matches(String regex, + public ValueValidator matches( + String regex, Supplier exceptionCreator) { - return matches(regex, value -> exceptionCreator.get()); + return matches(regex, convertExceptionCreator(exceptionCreator)); } - public ValueValidator matches(String regex, + public ValueValidator matches( + String regex, Function exceptionCreator) { withRule(input -> RegexUtil.matches((String) input, regex), exceptionCreator); return this; @@ -254,35 +287,72 @@ public class ValueValidator { // ===== matchesOr ===== public ValueValidator matchesOr(String[] regexs, String errMsg) { - return matchesOr(regexs, value -> new InvalidInputException(errMsg)); + return matchesOr(regexs, convertExceptionCreator(errMsg)); } - public ValueValidator matchesOr(String[] regexs, + public ValueValidator matchesOr( + String[] regexs, Supplier exceptionCreator) { - return matchesOr(regexs, value -> exceptionCreator.get()); + return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); } - public ValueValidator matchesOr(String[] regexs, + public ValueValidator matchesOr( + String[] regexs, Function exceptionCreator) { withRule(input -> RegexUtil.matchesOr((String) input, regexs), exceptionCreator); return this; } - // ===== matchesOr ===== + // ===== matchesAnd ===== public ValueValidator matchesAnd(String[] regexs, String errMsg) { - withRule(input -> RegexUtil.matchesAnd((String) input, regexs), errMsg); + return matchesAnd(regexs, convertExceptionCreator(errMsg)); + } + + public ValueValidator matchesAnd( + String[] regexs, + Supplier exceptionCreator) { + return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + } + + public ValueValidator matchesAnd( + String[] regexs, + Function exceptionCreator) { + withRule(input -> RegexUtil.matchesAnd((String) input, regexs), exceptionCreator); + return this; + } + + // ===== notBlank ===== + + public ValueValidator notBlank() { + return notBlank("This String argument must have text; it must not be null, empty, or blank"); + } + + public ValueValidator notBlank(String errMsg) { + return notBlank(convertExceptionCreator(errMsg)); + } + + public ValueValidator notBlank(Supplier exceptionCreator) { + return notBlank(convertExceptionCreator(exceptionCreator)); + } + + public ValueValidator notBlank(Function exceptionCreator) { + withRule(input -> StrUtil.isNotBlank((String) input), exceptionCreator); return this; } // ===== email ===== + public ValueValidator email() { + return email("The value is not an email address."); + } + public ValueValidator email(String errMsg) { - return email(value -> new InvalidInputException(errMsg)); + return email(convertExceptionCreator(errMsg)); } public ValueValidator email(Supplier exceptionCreator) { - return email(value -> exceptionCreator.get()); + return email(convertExceptionCreator(exceptionCreator)); } public ValueValidator email(Function exceptionCreator) { @@ -293,18 +363,17 @@ public class ValueValidator { void validateProperty(DTO obj) { PROPERTY value = this.getter.apply(obj); - this.rules.forEach(rule -> rule.validate(value)); - } - - @AllArgsConstructor(access = AccessLevel.PRIVATE) - private static final class RuleInfo { - private final Predicate condition; - private final Function exceptionCreator; - - private void validate(V obj) { - if (!this.condition.test(obj)) { - throw exceptionCreator.apply(obj); - } + for (var rule : this.rules) { + rule.accept(value); } } + + private static Function convertExceptionCreator(String errMsg) { + return convertExceptionCreator(errMsg); + } + + private static Function convertExceptionCreator( + Supplier exceptionSupplier) { + return value -> exceptionSupplier.get(); + } }