diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java index 0e2720e..28c612d 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java @@ -31,10 +31,10 @@ import xyz.zhouxy.plusone.commons.util.AssertTools; * * @author ZhouXY */ -public class ArrayPropertyValidator - extends BasePropertyValidator> { +public class ArrayPropertyValidator + extends BasePropertyValidator> { - ArrayPropertyValidator(Function getter) { + ArrayPropertyValidator(Function getter) { super(getter); } @@ -47,42 +47,43 @@ public class ArrayPropertyValidator * * @return 属性校验器 */ - public ArrayPropertyValidator notEmpty() { - return notEmpty("The input must not be empty."); + public final ArrayPropertyValidator notEmpty() { + return withRule(Conditions.notEmpty(), "The input must not be empty."); } /** * 添加一条校验属性的规则,校验属性是否非空 * - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public ArrayPropertyValidator notEmpty(String errMsg) { - return notEmpty(convertToExceptionFunction(errMsg)); + public final ArrayPropertyValidator notEmpty( + final String errorMessage) { + return withRule(Conditions.notEmpty(), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否非空 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator notEmpty( - Supplier e) { - return notEmpty(convertToExceptionFunction(e)); + public final ArrayPropertyValidator notEmpty( + final Supplier exceptionSupplier) { + return withRule(Conditions.notEmpty(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否非空 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator notEmpty( - Function e) { - return withRule(ArrayTools::isNotEmpty, e); + public final ArrayPropertyValidator notEmpty( + final Function exceptionFunction) { + return withRule(Conditions.notEmpty(), exceptionFunction); } // ================================ @@ -98,40 +99,41 @@ public class ArrayPropertyValidator * * @return 属性校验器 */ - public ArrayPropertyValidator isEmpty() { - return isEmpty("The input must be empty."); + public final ArrayPropertyValidator isEmpty() { + return withRule(Conditions.isEmpty(), "The input must be empty."); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public ArrayPropertyValidator isEmpty(String errMsg) { - return isEmpty(convertToExceptionFunction(errMsg)); + public final ArrayPropertyValidator isEmpty( + final String errorMessage) { + return withRule(Conditions.isEmpty(), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator isEmpty( - Supplier e) { - return isEmpty(convertToExceptionFunction(e)); + public final ArrayPropertyValidator isEmpty( + final Supplier exceptionSupplier) { + return withRule(Conditions.isEmpty(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator isEmpty( - Function e) { - return withRule(ArrayTools::isEmpty, e); + public final ArrayPropertyValidator isEmpty( + final Function exceptionFunction) { + return withRule(Conditions.isEmpty(), exceptionFunction); } // ================================ @@ -148,46 +150,65 @@ public class ArrayPropertyValidator * @param condition 校验规则 * @return 属性校验器 */ - public ArrayPropertyValidator allMatch(Predicate condition) { - return allMatch(condition, convertToExceptionFunction("All elements must match the condition.")); - } - - /** - * 添加一条校验属性的规则,校验是否所有元素都满足条件 - * - * @param condition 校验规则 - * @param errMsg 异常信息 - * @return 属性校验器 - */ - public ArrayPropertyValidator allMatch(Predicate condition, String errMsg) { - return allMatch(condition, convertToExceptionFunction(errMsg)); - } - - /** - * 添加一条校验属性的规则,校验是否所有元素都满足条件 - * - * @param condition 校验规则 - * @param e 自定义异常 - * @return 属性校验器 - */ - public ArrayPropertyValidator allMatch( - Predicate condition, Supplier e) { - return allMatch(condition, convertToExceptionFunction(e)); - } - - /** - * 添加一条校验属性的规则,校验是否所有元素都满足条件 - * - * @param condition 校验规则 - * @param e 自定义异常 - * @return 属性校验器 - */ - public ArrayPropertyValidator allMatch( - Predicate condition, Function e) { + public final ArrayPropertyValidator allMatch(final Predicate condition) { return withRule(c -> { - for (TElement element : c) { + for (E element : c) { if (!condition.test(element)) { - throw e.apply(element); + throw ValidationException.withMessage("All elements must match the condition."); + } + } + }); + } + + /** + * 添加一条校验属性的规则,校验是否所有元素都满足条件 + * + * @param condition 校验规则 + * @param errorMessage 异常信息 + * @return 属性校验器 + */ + public final ArrayPropertyValidator allMatch( + final Predicate condition, final String errorMessage) { + return withRule(c -> { + for (E element : c) { + if (!condition.test(element)) { + throw ValidationException.withMessage(errorMessage); + } + } + }); + } + + /** + * 添加一条校验属性的规则,校验是否所有元素都满足条件 + * + * @param condition 校验规则 + * @param exceptionSupplier 自定义异常 + * @return 属性校验器 + */ + public final ArrayPropertyValidator allMatch( + final Predicate condition, final Supplier exceptionSupplier) { + return withRule(c -> { + for (E element : c) { + if (!condition.test(element)) { + throw exceptionSupplier.get(); + } + } + }); + } + + /** + * 添加一条校验属性的规则,校验是否所有元素都满足条件 + * + * @param condition 校验规则 + * @param exceptionFunction 自定义异常 + * @return 属性校验器 + */ + public final ArrayPropertyValidator allMatch( + final Predicate condition, final Function exceptionFunction) { + return withRule(c -> { + for (E element : c) { + if (!condition.test(element)) { + throw exceptionFunction.apply(element); } } }); @@ -205,47 +226,38 @@ public class ArrayPropertyValidator * 添加一条校验属性的规则,校验属性长度是否等于指定长度 * * @param length 指定长度 - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public ArrayPropertyValidator length(int length, String errMsg) { - return length(length, convertToExceptionFunction(errMsg)); + public final ArrayPropertyValidator length( + final int length, final String errorMessage) { + return withRule(Conditions.length(length), errorMessage); } /** * 添加一条校验属性的规则,校验属性长度是否等于指定长度 * - * @param 异常类型 + * @param 异常类型 * @param length 指定长度 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator length( - int length, Supplier e) { - return length(length, convertToExceptionFunction(e)); + public final ArrayPropertyValidator length( + final int length, final Supplier exceptionSupplier) { + return withRule(Conditions.length(length), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性长度是否等于指定长度 * - * @param 异常类型 + * @param 异常类型 * @param length 指定长度 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator length( - int length, Function e) { - AssertTools.checkArgument(length >= 0, - "The expected length must be greater than or equal to 0."); - return withRule(s -> s == null || s.length == length, e); - } - - static boolean checkLength(TElement[] str, int min, int max) { - if (str == null) { - return true; - } - final int len = str.length; - return len >= min && len <= max; + public final ArrayPropertyValidator length( + final int length, final Function exceptionFunction) { + return withRule(Conditions.length(length), exceptionFunction); } /** @@ -253,11 +265,12 @@ public class ArrayPropertyValidator * * @param min 最小长度 * @param max 最大长度 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public ArrayPropertyValidator length(int min, int max, String errMsg) { - return length(min, max, convertToExceptionFunction(errMsg)); + public final ArrayPropertyValidator length( + final int min, final int max, final String errorMessage) { + return withRule(Conditions.length(min, max), errorMessage); } /** @@ -265,12 +278,12 @@ public class ArrayPropertyValidator * * @param min 最小长度 * @param max 最大长度 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator length( - int min, int max, Supplier e) { - return length(min, max, convertToExceptionFunction(e)); + public final ArrayPropertyValidator length( + final int min, final int max, final Supplier exceptionSupplier) { + return withRule(Conditions.length(min, max), exceptionSupplier); } /** @@ -278,22 +291,49 @@ public class ArrayPropertyValidator * * @param min 最小长度 * @param max 最大长度 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public ArrayPropertyValidator length( - int min, int max, Function e) { - AssertTools.checkArgument(min >= 0, "min must be non-negative."); - AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); - return withRule(s -> checkLength(s, min, max), e); + public final ArrayPropertyValidator length( + final int min, final int max, final Function exceptionFunction) { + return withRule(Conditions.length(min, max), exceptionFunction); } // ================================ // #endregion - length // ================================ + private static class Conditions { + + private static Predicate isEmpty() { + return ArrayTools::isEmpty; + } + + private static Predicate notEmpty() { + return ArrayTools::isNotEmpty; + } + + private static Predicate length(final int length) { + AssertTools.checkArgument(length >= 0, + "The expected length must be non-negative."); + return input -> input == null || input.length == length; + } + + private static Predicate length(final int min, final int max) { + AssertTools.checkArgument(min >= 0, "min must be non-negative."); + AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); + return input -> { + if (input == null) { + return true; + } + final int len = input.length; + return len >= min && len <= max; + }; + } + } + @Override - protected ArrayPropertyValidator thisObject() { + protected ArrayPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java index e35d35e..7e0b5eb 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java @@ -17,6 +17,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; import com.google.common.collect.Range; @@ -33,7 +34,10 @@ import com.google.common.collect.Range; * @see Range * @author ZhouXY */ -public abstract class BaseComparablePropertyValidator, TPropertyValidator extends BaseComparablePropertyValidator> +public abstract class BaseComparablePropertyValidator< + T, + TProperty extends Comparable, + TPropertyValidator extends BaseComparablePropertyValidator> extends BasePropertyValidator { BaseComparablePropertyValidator(Function getter) { @@ -46,47 +50,51 @@ public abstract class BaseComparablePropertyValidator range) { - withRule(value -> value != null && range.contains(value), value -> ValidationException.withMessage( + public final TPropertyValidator inRange(final Range range) { + return withRule(Conditions.inRange(range), value -> ValidationException.withMessage( "The input must in the interval %s. You entered %s.", range, value)); - return thisObject(); } /** * 添加一条校验属性的规则,校验属性是否在给定的区间之内 * * @param range 区间 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public TPropertyValidator inRange(Range range, String errMsg) { - withRule(value -> value != null && range.contains(value), convertToExceptionFunction(errMsg)); - return thisObject(); + public final TPropertyValidator inRange( + final Range range, final String errorMessage) { + return withRule(Conditions.inRange(range), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否在给定的区间之内 * * @param range 区间 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public TPropertyValidator inRange( - Range range, Supplier e) { - withRule(value -> value != null && range.contains(value), convertToExceptionFunction(e)); - return thisObject(); + public final TPropertyValidator inRange( + final Range range, final Supplier exceptionSupplier) { + return withRule(Conditions.inRange(range), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否在给定的区间之内 * * @param range 区间 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public TPropertyValidator inRange( - Range range, Function e) { - withRule(value -> value != null && range.contains(value), e); - return thisObject(); + public final TPropertyValidator inRange( + final Range range, final Function exceptionFunction) { + return withRule(Conditions.inRange(range), exceptionFunction); + } + + private static class Conditions { + private static > Predicate inRange( + final Range range) { + return value -> value == null || range.contains(value); + } } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java index 73a32cf..d739577 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java @@ -16,7 +16,6 @@ package xyz.zhouxy.plusone.validator; -import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Objects; @@ -34,7 +33,10 @@ import java.util.function.Supplier; * * @author ZhouXY */ -public abstract class BasePropertyValidator> { +public abstract class BasePropertyValidator< + T, + TProperty, + TPropertyValidator extends BasePropertyValidator> { private final Function getter; @@ -47,15 +49,68 @@ public abstract class BasePropertyValidator TPropertyValidator withRule( - Predicate rule, Function e) { - return withRule(v -> { - if (!rule.test(v)) { - throw e.apply(v); + protected final TPropertyValidator withRule( + final Predicate condition, + final String errorMessage) { + return withRule(input -> { + if (!condition.test(input)) { + throw ValidationException.withMessage(errorMessage); + } + }); + } + + /** + * 添加一条校验属性的规则 + * + * @param condition 校验条件 + * @param errorMessageTemplate 错误信息模版 + * @param errorMessageArgs 错误信息参数 + * @return 属性校验器 + */ + protected final TPropertyValidator withRule( + final Predicate condition, + final String errorMessageTemplate, Object... errorMessageArgs) { + return withRule(input -> { + if (!condition.test(input)) { + throw ValidationException.withMessage(errorMessageTemplate, errorMessageArgs); + } + }); + } + + /** + * 添加一条校验属性的规则 + * + * @param condition 校验条件 + * @param exceptionSupplier 自定义异常 + * @return 属性校验器 + */ + protected final TPropertyValidator withRule( + final Predicate condition, + final Supplier exceptionSupplier) { + return withRule(input -> { + if (!condition.test(input)) { + throw exceptionSupplier.get(); + } + }); + } + + /** + * 添加一条校验属性的规则 + * + * @param condition 校验条件 + * @param exceptionFunction 自定义异常 + * @return 属性校验器 + */ + protected final TPropertyValidator withRule( + final Predicate condition, + final Function exceptionFunction) { + return withRule(input -> { + if (!condition.test(input)) { + throw exceptionFunction.apply(input); } }); } @@ -64,7 +119,6 @@ public abstract class BasePropertyValidator rule) { @@ -74,11 +128,12 @@ public abstract class BasePropertyValidator consumer : consumers) { - consumer.accept(getter.apply(propertyValue)); + consumer.accept(getter.apply(obj)); } } @@ -97,41 +152,42 @@ public abstract class BasePropertyValidator 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public TPropertyValidator notNull(Supplier e) { - return notNull(convertToExceptionFunction(e)); + public final TPropertyValidator notNull( + final Supplier exceptionSupplier) { + return withRule(Objects::nonNull, exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否不为空 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public TPropertyValidator notNull(Function e) { - withRule(Objects::nonNull, e); - return thisObject(); + public final TPropertyValidator notNull( + final Function exceptionFunction) { + return withRule(Objects::nonNull, exceptionFunction); } // ================================ @@ -147,41 +203,42 @@ public abstract class BasePropertyValidator 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public TPropertyValidator isNull(Supplier e) { - return isNull(convertToExceptionFunction(e)); + public final TPropertyValidator isNull( + final Supplier exceptionSupplier) { + return withRule(Objects::isNull, exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public TPropertyValidator isNull(Function e) { - withRule(Objects::isNull, e); - return thisObject(); + public final TPropertyValidator isNull( + final Function exceptionFunction) { + return withRule(Objects::isNull, exceptionFunction); } // ================================ @@ -189,7 +246,7 @@ public abstract class BasePropertyValidator ValidationException - .withMessage("The input must be equal to '%s'.", that)); + public final TPropertyValidator equal(Object that) { + return withRule(Conditions.equal(that), + "The input must be equal to '%s'.", that); } /** * 添加一条校验属性的规则,校验属性是否等于给定值 * * @param that 给定值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public TPropertyValidator equalTo(Object that, String errMsg) { - return equalTo(that, convertToExceptionFunction(errMsg)); + public final TPropertyValidator equal( + final Object that, final String errorMessage) { + return withRule(Conditions.equal(that), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否等于给定值 * - * @param 自定义异常类型 + * @param 自定义异常类型 * @param that 给定值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public TPropertyValidator equalTo( - Object that, Supplier e) { - return equalTo(that, convertToExceptionFunction(e)); + public final TPropertyValidator equal( + final Object that, final Supplier exceptionSupplier) { + return withRule(Conditions.equal(that), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否等于给定值 * - * @param 自定义异常类型 + * @param 自定义异常类型 * @param that 给定值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public TPropertyValidator equalTo( - Object that, Function e) { - withRule(value -> value == null || value.equals(that), e); - return thisObject(); + public final TPropertyValidator equal( + final Object that, final Function exceptionFunction) { + return withRule(Conditions.equal(that), exceptionFunction); } // ================================ - // #endregion - equalTo + // #endregion - equal // ================================ // ================================ @@ -255,47 +312,46 @@ public abstract class BasePropertyValidator ValidationException - .withMessage("The input must not equal '%s'.", that)); + public final TPropertyValidator notEqual(final Object that) { + return withRule(Conditions.notEqual(that), + "The input must not equal '%s'.", that); } /** * 添加一条校验属性的规则,校验属性是否等于给定值 * * @param that 给定值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public TPropertyValidator notEqual(Object that, String errMsg) { - return notEqual(that, convertToExceptionFunction(errMsg)); + public final TPropertyValidator notEqual(final Object that, final String errorMessage) { + return withRule(Conditions.notEqual(that), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否等于给定值 * - * @param 自定义异常类型 + * @param 自定义异常类型 * @param that 给定值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public TPropertyValidator notEqual( - Object that, Supplier e) { - return notEqual(that, convertToExceptionFunction(e)); + public final TPropertyValidator notEqual( + final Object that, final Supplier exceptionSupplier) { + return withRule(Conditions.notEqual(that), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否等于给定值 * - * @param 自定义异常类型 + * @param 自定义异常类型 * @param that 给定值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public TPropertyValidator notEqual( - Object that, Function e) { - withRule(value -> value == null || !value.equals(that), e); - return thisObject(); + public final TPropertyValidator notEqual( + final Object that, final Function exceptionFunction) { + return withRule(Conditions.notEqual(that), exceptionFunction); } // ================================ @@ -309,119 +365,76 @@ public abstract class BasePropertyValidator condition) { - return must(condition, "The specified condition was not met for the input."); + public final TPropertyValidator must(final Predicate condition) { + return withRule(condition, + "The specified condition was not met for the input."); } /** * 添加一条校验属性的规则,校验属性是否满足给定的条件 * - * @param condition 校验规则 - * @param errMsg 错误信息 + * @param condition 校验条件 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public TPropertyValidator must(Predicate condition, String errMsg) { - return must(condition, convertToExceptionFunction(errMsg)); + public final TPropertyValidator must( + final Predicate condition, + final String errorMessage) { + return withRule(condition, errorMessage); } /** * 添加一条校验属性的规则,校验属性是否满足给定的条件 * - * @param 自定义异常类型 + * @param 自定义异常类型 * @param condition 校验规则 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public TPropertyValidator must( - Predicate condition, - Supplier e) { - return must(condition, convertToExceptionFunction(e)); + public final TPropertyValidator must( + final Predicate condition, + final Supplier exceptionSupplier) { + return withRule(condition, exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否满足给定的条件 * - * @param 自定义异常类型 + * @param 自定义异常类型 * @param condition 校验规则 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public TPropertyValidator must( - Predicate condition, - Function e) { - withRule(condition, e); - return thisObject(); - } - - /** - * 添加多条校验属性的规则,校验属性是否满足给定的所有条件 - * - * @param conditions 校验规则 - * @return 属性校验器 - */ - public TPropertyValidator must(Collection> conditions) { - return must(conditions, "The specified conditions were not met for the input."); - } - - /** - * 添加多条校验属性的规则,校验属性是否满足给定的所有条件 - * - * @param conditions 校验规则 - * @param errMsg 错误信息 - * @return 属性校验器 - */ - public TPropertyValidator must(Collection> conditions, String errMsg) { - return must(conditions, convertToExceptionFunction(errMsg)); - } - - /** - * 添加多条校验属性的规则,校验属性是否满足给定的所有条件 - * - * @param 自定义异常类型 - * @param conditions 校验规则 - * @param e 自定义异常 - * @return 属性校验器 - */ - public TPropertyValidator must( - Collection> conditions, Supplier e) { - return must(conditions, convertToExceptionFunction(e)); - } - - /** - * 添加多条校验属性的规则,校验属性是否满足给定的所有条件 - * - * @param 自定义异常类型 - * @param conditions 校验规则 - * @param e 自定义异常 - * @return 属性校验器 - */ - public TPropertyValidator must( - Collection> conditions, - Function e) { - for (Predicate condition : conditions) { - withRule(condition, e); - } - return thisObject(); + public final TPropertyValidator must( + final Predicate condition, + final Function exceptionFunction) { + return withRule(condition, exceptionFunction); } // ================================ // #endregion - must // ================================ - static Function convertToExceptionFunction(String errMsg) { - return value -> ValidationException.withMessage(errMsg); + // ================================ + // #region - conditions + // ================================ + + private static class Conditions { + + private static Predicate equal(Object obj) { + return input -> input == null || input.equals(obj); + } + + private static Predicate notEqual(Object obj) { + return input -> input == null || !input.equals(obj); + } + } - static Function convertToExceptionFunction( - String errorMessageTemplate, Object... errorMessageArgs) { - return value -> ValidationException.withMessage(errorMessageTemplate, errorMessageArgs); - } - - static Function convertToExceptionFunction( - Supplier exceptionSupplier) { - return value -> exceptionSupplier.get(); - } + // ================================ + // #endregion - conditions + // ================================ } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index ed18fef..2a02a30 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -45,37 +45,37 @@ public abstract class BaseValidator implements IValidator { /** * 添加一个校验规则 * - * @param rule 校验规则 - * @param errorMessage 错误信息 - */ - protected final void withRule(final Predicate rule, final String errorMessage) { - withRule(rule, () -> ValidationException.withMessage(errorMessage)); - } - - /** - * 添加一个校验规则 - * - * @param 自定义异常类型 - * @param rule 校验规则 - * @param e 自定义异常 - */ - protected final void withRule( - final Predicate rule, final Supplier e) { - withRule(rule, value -> e.get()); - } - - /** - * 添加一个校验规则 - * - * @param 自定义异常类型 * @param condition 校验条件 - * @param e 自定义异常 + * @param errorMessage 异常信息 */ - protected final void withRule( - final Predicate condition, final Function e) { + protected final void withRule(final Predicate condition, final String errorMessage) { + withRule(condition, () -> ValidationException.withMessage(errorMessage)); + } + + /** + * 添加一个校验规则 + * + * @param 自定义异常类型 + * @param condition 校验条件 + * @param exceptionSupplier 自定义异常 + */ + protected final void withRule( + final Predicate condition, final Supplier exceptionSupplier) { + withRule(condition, value -> exceptionSupplier.get()); + } + + /** + * 添加一个校验规则 + * + * @param 自定义异常类型 + * @param condition 校验条件 + * @param exceptionFunction 自定义异常 + */ + protected final void withRule( + final Predicate condition, final Function exceptionFunction) { withRule(value -> { if (!condition.test(value)) { - throw e.apply(value); + throw exceptionFunction.apply(value); } }); } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java index bf74c36..b66b3f4 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java @@ -17,6 +17,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; /** @@ -28,7 +29,8 @@ import java.util.function.Supplier; * * @author ZhouXY */ -public class BoolPropertyValidator extends BasePropertyValidator> { +public class BoolPropertyValidator + extends BasePropertyValidator> { BoolPropertyValidator(Function getter) { super(getter); @@ -41,40 +43,42 @@ public class BoolPropertyValidator extends BasePropertyValidator isTrueValue() { - return isTrueValue("The input must be true."); + public final BoolPropertyValidator isTrueValue() { + return withRule(Conditions.isTrueValue(), "The input must be true."); } /** * 添加一条判断属性值是否为 {@code true} 的校验规则 * - * @param errMsg 校验失败的错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public BoolPropertyValidator isTrueValue(String errMsg) { - return isTrueValue(convertToExceptionFunction(errMsg)); + public final BoolPropertyValidator isTrueValue(final String errorMessage) { + return withRule(Conditions.isTrueValue(), errorMessage); } /** * 添加一条判断属性值是否为 {@code true} 的校验规则 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public BoolPropertyValidator isTrueValue(Supplier e) { - return isTrueValue(convertToExceptionFunction(e)); + public final BoolPropertyValidator isTrueValue( + final Supplier exceptionSupplier) { + return withRule(Conditions.isTrueValue(), exceptionSupplier); } /** * 添加一条判断属性值是否为 {@code true} 的校验规则 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public BoolPropertyValidator isTrueValue(Function e) { - return withRule(Boolean.TRUE::equals, e); + public final BoolPropertyValidator isTrueValue( + Function exceptionFunction) { + return withRule(Conditions.isTrueValue(), exceptionFunction); } // ====== isFalseValue ====== @@ -84,40 +88,53 @@ public class BoolPropertyValidator extends BasePropertyValidator isFalseValue() { - return isFalseValue("The input must be false."); + public final BoolPropertyValidator isFalseValue() { + return withRule(Conditions.isFalseValue(), "The input must be false."); } /** * 添加一条判断属性值是否为 {@code false} 的校验规则 * - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public BoolPropertyValidator isFalseValue(String errMsg) { - return isFalseValue(convertToExceptionFunction(errMsg)); + public final BoolPropertyValidator isFalseValue(final String errorMessage) { + return withRule(Conditions.isFalseValue(), errorMessage); } /** * 添加一条判断属性值是否为 {@code false} 的校验规则 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public BoolPropertyValidator isFalseValue(Supplier e) { - return isFalseValue(convertToExceptionFunction(e)); + public final BoolPropertyValidator isFalseValue( + final Supplier exceptionSupplier) { + return withRule(Conditions.isFalseValue(), exceptionSupplier); } /** * 添加一条判断属性值是否为 {@code false} 的校验规则 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public BoolPropertyValidator isFalseValue(Function e) { - return withRule(Boolean.FALSE::equals, e); + public final BoolPropertyValidator isFalseValue( + final Function exceptionFunction) { + return withRule(Conditions.isFalseValue(), exceptionFunction); + } + + private static class Conditions { + + private static Predicate isTrueValue() { + return Boolean.TRUE::equals; + } + + private static Predicate isFalseValue() { + return Boolean.FALSE::equals; + } } @Override diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java index 6f5ee05..7b90f7e 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java @@ -32,10 +32,10 @@ import xyz.zhouxy.plusone.commons.util.AssertTools; * * @author ZhouXY */ -public class CollectionPropertyValidator - extends BasePropertyValidator, CollectionPropertyValidator> { +public class CollectionPropertyValidator + extends BasePropertyValidator, CollectionPropertyValidator> { - CollectionPropertyValidator(Function> getter) { + CollectionPropertyValidator(Function> getter) { super(getter); } @@ -48,42 +48,42 @@ public class CollectionPropertyValidator * * @return 属性校验器 */ - public CollectionPropertyValidator notEmpty() { - return notEmpty("The input must not be empty."); + public final CollectionPropertyValidator notEmpty() { + return withRule(Conditions.notEmpty(), "The input must not be empty."); } /** * 添加一条校验属性的规则,校验属性是否非空 * - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public CollectionPropertyValidator notEmpty(String errMsg) { - return notEmpty(convertToExceptionFunction(errMsg)); + public final CollectionPropertyValidator notEmpty(final String errorMessage) { + return withRule(Conditions.notEmpty(), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否非空 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator notEmpty( - Supplier e) { - return notEmpty(convertToExceptionFunction(e)); + public final CollectionPropertyValidator notEmpty( + final Supplier exceptionSupplier) { + return withRule(Conditions.notEmpty(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否非空 * - * @param 自定义异常类型 - * @param e 自定义异常 + * @param 自定义异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator notEmpty( - Function, E> e) { - return withRule(CollectionTools::isNotEmpty, e); + public final CollectionPropertyValidator notEmpty( + final Function, X> exceptionFunction) { + return withRule(Conditions.notEmpty(), exceptionFunction); } // ================================ @@ -99,40 +99,41 @@ public class CollectionPropertyValidator * * @return 属性校验器 */ - public CollectionPropertyValidator isEmpty() { - return isEmpty("The input must be empty."); + public final CollectionPropertyValidator isEmpty() { + return withRule(Conditions.isEmpty(), "The input must be empty."); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public CollectionPropertyValidator isEmpty(String errMsg) { - return isEmpty(convertToExceptionFunction(errMsg)); + public final CollectionPropertyValidator isEmpty( + final String errorMessage) { + return withRule(Conditions.isEmpty(), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator isEmpty( - Supplier e) { - return isEmpty(convertToExceptionFunction(e)); + public final CollectionPropertyValidator isEmpty( + final Supplier exceptionSupplier) { + return withRule(Conditions.isEmpty(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否为空 * - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator isEmpty( - Function, E> e) { - return withRule(CollectionTools::isEmpty, e); + public final CollectionPropertyValidator isEmpty( + final Function, X> exceptionFunction) { + return withRule(Conditions.isEmpty(), exceptionFunction); } // ================================ @@ -146,48 +147,62 @@ public class CollectionPropertyValidator /** * 添加一条校验属性的规则,校验是否所有元素都满足条件 * - * @param condition 校验规则 + * @param condition 校验条件 * @return 属性校验器 */ - public CollectionPropertyValidator allMatch(Predicate condition) { - return allMatch(condition, convertToExceptionFunction("All elements must match the condition.")); - } - - /** - * 添加一条校验属性的规则,校验是否所有元素都满足条件 - * - * @param condition 校验规则 - * @param errMsg 异常信息 - * @return 属性校验器 - */ - public CollectionPropertyValidator allMatch(Predicate condition, String errMsg) { - return allMatch(condition, convertToExceptionFunction(errMsg)); - } - - /** - * 添加一条校验属性的规则,校验是否所有元素都满足条件 - * - * @param condition 校验规则 - * @param e 自定义异常 - * @return 属性校验器 - */ - public CollectionPropertyValidator allMatch( - Predicate condition, Supplier e) { - return allMatch(condition, convertToExceptionFunction(e)); - } - - /** - * 添加一条校验属性的规则,校验是否所有元素都满足条件 - * - * @param condition 校验规则 - * @param e 自定义异常 - * @return 属性校验器 - */ - public CollectionPropertyValidator allMatch( - Predicate condition, Function e) { + public final CollectionPropertyValidator allMatch( + final Predicate condition) { return withRule(c -> c.forEach(element -> { if (!condition.test(element)) { - throw e.apply(element); + throw ValidationException.withMessage("All elements must match the condition."); + } + })); + } + + /** + * 添加一条校验属性的规则,校验是否所有元素都满足条件 + * + * @param condition 校验规则 + * @param errorMessage 异常信息 + * @return 属性校验器 + */ + public final CollectionPropertyValidator allMatch( + final Predicate condition, final String errorMessage) { + return withRule(c -> c.forEach(element -> { + if (!condition.test(element)) { + throw ValidationException.withMessage(errorMessage); + } + })); + } + + /** + * 添加一条校验属性的规则,校验是否所有元素都满足条件 + * + * @param condition 校验条件 + * @param exceptionSupplier 自定义异常 + * @return 属性校验器 + */ + public final CollectionPropertyValidator allMatch( + final Predicate condition, final Supplier exceptionSupplier) { + return withRule(c -> c.forEach(element -> { + if (!condition.test(element)) { + throw exceptionSupplier.get(); + } + })); + } + + /** + * 添加一条校验属性的规则,校验是否所有元素都满足条件 + * + * @param condition 校验条件 + * @param exceptionFunction 自定义异常 + * @return 属性校验器 + */ + public final CollectionPropertyValidator allMatch( + final Predicate condition, final Function exceptionFunction) { + return withRule(c -> c.forEach(element -> { + if (!condition.test(element)) { + throw exceptionFunction.apply(element); } })); } @@ -204,47 +219,38 @@ public class CollectionPropertyValidator * 添加一条校验属性的规则,校验属性大小是否等于指定大小 * * @param size 指定大小 - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public CollectionPropertyValidator size(int size, String errMsg) { - return size(size, convertToExceptionFunction(errMsg)); + public final CollectionPropertyValidator size( + final int size, final String errorMessage) { + return withRule(Conditions.size(size), errorMessage); } /** * 添加一条校验属性的规则,校验属性大小是否等于指定大小 * - * @param 异常类型 + * @param 异常类型 * @param size 指定大小 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator size( - int size, Supplier e) { - return size(size, convertToExceptionFunction(e)); + public final CollectionPropertyValidator size( + final int size, final Supplier exceptionSupplier) { + return withRule(Conditions.size(size), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性大小是否等于指定大小 * - * @param 异常类型 + * @param 异常类型 * @param size 指定大小 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator size( - int size, Function, E> e) { - AssertTools.checkArgument(size >= 0, - "The expected size must be greater than or equal to 0."); - return withRule(s -> s == null || s.size() == size, e); - } - - static boolean checkSize(Collection str, int min, int max) { - if (str == null) { - return true; - } - final int size = str.size(); - return size >= min && size <= max; + public final CollectionPropertyValidator size( + final int size, final Function, X> exceptionFunction) { + return withRule(Conditions.size(size), exceptionFunction); } /** @@ -252,11 +258,12 @@ public class CollectionPropertyValidator * * @param min 最小大小 * @param max 最大大小 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public CollectionPropertyValidator size(int min, int max, String errMsg) { - return size(min, max, convertToExceptionFunction(errMsg)); + public final CollectionPropertyValidator size( + final int min, final int max, final String errorMessage) { + return withRule(Conditions.size(min, max), errorMessage); } /** @@ -264,12 +271,12 @@ public class CollectionPropertyValidator * * @param min 最小大小 * @param max 最大大小 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator size( - int min, int max, Supplier e) { - return size(min, max, convertToExceptionFunction(e)); + public final CollectionPropertyValidator size( + final int min, final int max, final Supplier exceptionSupplier) { + return withRule(Conditions.size(min, max), exceptionSupplier); } /** @@ -277,22 +284,49 @@ public class CollectionPropertyValidator * * @param min 最小大小 * @param max 最大大小 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public CollectionPropertyValidator size( - int min, int max, Function, E> e) { - AssertTools.checkArgument(min >= 0, "min must be non-negative."); - AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); - return withRule(s -> checkSize(s, min, max), e); + public final CollectionPropertyValidator size( + final int min, final int max, final Function, X> exceptionFunction) { + return withRule(Conditions.size(min, max), exceptionFunction); } // ================================ // #endregion - size // ================================ + private static class Conditions { + + private static Predicate> isEmpty() { + return CollectionTools::isEmpty; + } + + private static Predicate> notEmpty() { + return CollectionTools::isNotEmpty; + } + + private static Predicate> size(int size) { + AssertTools.checkArgument(size >= 0, + "The expected size must be non-negative."); + return collection -> collection == null || collection.size() == size; + } + + private static Predicate> size(int min, int max) { + AssertTools.checkArgument(min >= 0, "min must be non-negative."); + AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); + return collection -> { + if (collection == null) { + return true; + } + int size = collection.size(); + return size >= min && size <= max; + }; + } + } + @Override - protected CollectionPropertyValidator thisObject() { + protected CollectionPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java index 63621db..753840c 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java @@ -26,8 +26,7 @@ import java.util.function.Function; * * @param 待校验对象类型 * @param 属性类型 - * @param 当前属性校验器类型,用于链式调用 - * @see Range + * @see com.google.common.collect.Range * @author ZhouXY */ public class ComparablePropertyValidator> diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java index 758c6fa..0ea90b6 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java @@ -17,6 +17,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; /** @@ -44,43 +45,47 @@ public class DoublePropertyValidator * @param min 最小值 * @return 属性校验器 */ - public DoublePropertyValidator gt(double min) { - return gt(min, convertToExceptionFunction("The input must be greater than '%s'.", min)); + public final DoublePropertyValidator gt(final double min) { + return withRule(Conditions.greaterThan(min), + "The input must be greater than '%s'.", min); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * * @param min 最小值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public DoublePropertyValidator gt(double min, String errMsg) { - return gt(min, convertToExceptionFunction(errMsg)); + public final DoublePropertyValidator gt( + final double min, final String errorMessage) { + return withRule(Conditions.greaterThan(min), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 错误信息 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator gt(double min, Supplier e) { - return gt(min, convertToExceptionFunction(e)); + public final DoublePropertyValidator gt( + final double min, final Supplier exceptionSupplier) { + return withRule(Conditions.greaterThan(min), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator gt(double min, Function e) { - return withRule(value -> (value != null && value > min), e); + public final DoublePropertyValidator gt( + final double min, final Function exceptionFunction) { + return withRule(Conditions.greaterThan(min), exceptionFunction); } // ================================ @@ -97,43 +102,47 @@ public class DoublePropertyValidator * @param min 最小值 * @return 属性校验器 */ - public DoublePropertyValidator ge(double min) { - return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%s'.", min)); + public final DoublePropertyValidator ge(final double min) { + return withRule(Conditions.greaterThanOrEqualTo(min), + "The input must be greater than or equal to '%s'.", min); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * * @param min 最小值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public DoublePropertyValidator ge(double min, String errMsg) { - return ge(min, convertToExceptionFunction(errMsg)); + public final DoublePropertyValidator ge( + final double min, final String errorMessage) { + return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator ge(double min, Supplier e) { - return ge(min, convertToExceptionFunction(e)); + public final DoublePropertyValidator ge( + final double min, final Supplier exceptionSupplier) { + return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator ge(double min, Function e) { - return withRule(value -> (value != null && value >= min), e); + public final DoublePropertyValidator ge( + final double min, final Function exceptionFunction) { + return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction); } // ================================ @@ -150,43 +159,47 @@ public class DoublePropertyValidator * @param max 最大值 * @return 属性校验器 */ - public DoublePropertyValidator lt(double max) { - return lt(max, convertToExceptionFunction("The input must be less than '%s'.", max)); + public final DoublePropertyValidator lt(final double max) { + return withRule(Conditions.lessThan(max), + "The input must be less than '%s'.", max); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * * @param max 最大值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public DoublePropertyValidator lt(double max, String errMsg) { - return lt(max, convertToExceptionFunction(errMsg)); + public final DoublePropertyValidator lt( + final double max, final String errorMessage) { + return withRule(Conditions.lessThan(max), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator lt(double max, Supplier e) { - return lt(max, convertToExceptionFunction(e)); + public final DoublePropertyValidator lt( + final double max, final Supplier exceptionSupplier) { + return withRule(Conditions.lessThan(max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator lt(double max, Function e) { - return withRule(value -> (value != null && value < max), e); + public final DoublePropertyValidator lt( + final double max, final Function exceptionFunction) { + return withRule(Conditions.lessThan(max), exceptionFunction); } // ================================ @@ -203,43 +216,47 @@ public class DoublePropertyValidator * @param max 最大值 * @return 属性校验器 */ - public DoublePropertyValidator le(double max) { - return le(max, convertToExceptionFunction("The input must be less than or equal to '%s'.", max)); + public final DoublePropertyValidator le(final double max) { + return withRule(Conditions.lessThanOrEqualTo(max), + "The input must be less than or equal to '%s'.", max); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * * @param max 最大值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public DoublePropertyValidator le(double max, String errMsg) { - return le(max, convertToExceptionFunction(errMsg)); + public final DoublePropertyValidator le( + final double max, final String errorMessage) { + return withRule(Conditions.lessThanOrEqualTo(max), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator le(double max, Supplier e) { - return le(max, convertToExceptionFunction(e)); + public final DoublePropertyValidator le( + final double max, final Supplier exceptionSupplier) { + return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public DoublePropertyValidator le(double max, Function e) { - return withRule(value -> (value != null && value <= max), e); + public final DoublePropertyValidator le( + final double max, final Function exceptionFunction) { + return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction); } // ================================ @@ -250,4 +267,23 @@ public class DoublePropertyValidator protected DoublePropertyValidator thisObject() { return this; } + + private static class Conditions { + + private static Predicate greaterThan(double min) { + return input -> input == null || input > min; + } + + private static Predicate greaterThanOrEqualTo(double min) { + return input -> input == null || input >= min; + } + + private static Predicate lessThan(double max) { + return input -> input == null || input < max; + } + + private static Predicate lessThanOrEqualTo(double max) { + return input -> input == null || input <= max; + } + } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java index e955760..30bc564 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java @@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; +import java.util.function.Predicate; /** * 整数属性校验器 @@ -44,43 +45,47 @@ public class IntPropertyValidator * @param min 最小值 * @return 属性校验器 */ - public IntPropertyValidator gt(int min) { - return gt(min, convertToExceptionFunction("The input must be greater than '%d'.", min)); + public IntPropertyValidator gt(final int min) { + return withRule(Conditions.greaterThan(min), + "The input must be greater than '%d'.", min); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * * @param min 最小值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public IntPropertyValidator gt(int min, String errMsg) { - return gt(min, convertToExceptionFunction(errMsg)); + public IntPropertyValidator gt( + final int min, final String errorMessage) { + return withRule(Conditions.greaterThan(min), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 错误信息 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator gt(int min, Supplier e) { - return gt(min, convertToExceptionFunction(e)); + public IntPropertyValidator gt( + final int min, final Supplier exceptionSupplier) { + return withRule(Conditions.greaterThan(min), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator gt(int min, Function e) { - return withRule(value -> (value != null && value > min), e); + public IntPropertyValidator gt( + final int min, final Function exceptionFunction) { + return withRule(Conditions.greaterThan(min), exceptionFunction); } // ================================ @@ -97,43 +102,46 @@ public class IntPropertyValidator * @param min 最小值 * @return 属性校验器 */ - public IntPropertyValidator ge(int min) { - return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%d'.", min)); + public IntPropertyValidator ge(final int min) { + return withRule(Conditions.greaterThanOrEqualTo(min), + "The input must be greater than or equal to '%d'.", min); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * * @param min 最小值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public IntPropertyValidator ge(int min, String errMsg) { - return ge(min, convertToExceptionFunction(errMsg)); + public IntPropertyValidator ge(final int min, final String errorMessage) { + return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator ge(int min, Supplier e) { - return ge(min, convertToExceptionFunction(e)); + public IntPropertyValidator ge( + final int min, final Supplier exceptionSupplier) { + return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator ge(int min, Function e) { - return withRule(value -> (value != null && value >= min), e); + public IntPropertyValidator ge( + final int min, final Function exceptionFunction) { + return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction); } // ================================ @@ -150,43 +158,47 @@ public class IntPropertyValidator * @param max 最大值 * @return 属性校验器 */ - public IntPropertyValidator lt(int max) { - return lt(max, convertToExceptionFunction("The input must be less than '%d'.", max)); + public IntPropertyValidator lt(final int max) { + return withRule(Conditions.lessThan(max), + "The input must be less than '%d'.", max); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * * @param max 最大值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public IntPropertyValidator lt(int max, String errMsg) { - return lt(max, convertToExceptionFunction(errMsg)); + public IntPropertyValidator lt( + final int max, final String errorMessage) { + return withRule(Conditions.lessThan(max), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator lt(int max, Supplier e) { - return lt(max, convertToExceptionFunction(e)); + public IntPropertyValidator lt( + final int max, final Supplier exceptionSupplier) { + return withRule(Conditions.lessThan(max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator lt(int max, Function e) { - return withRule(value -> (value != null && value < max), e); + public IntPropertyValidator lt( + final int max, final Function exceptionFunction) { + return withRule(Conditions.lessThan(max), exceptionFunction); } // ================================ @@ -203,43 +215,47 @@ public class IntPropertyValidator * @param max 最大值 * @return 属性校验器 */ - public IntPropertyValidator le(int max) { - return le(max, convertToExceptionFunction("The input must be less than or equal to '%d'.", max)); + public IntPropertyValidator le(final int max) { + return withRule(Conditions.lessThanOrEqualTo(max), + "The input must be less than or equal to '%d'.", max); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * * @param max 最大值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public IntPropertyValidator le(int max, String errMsg) { - return le(max, convertToExceptionFunction(errMsg)); + public IntPropertyValidator le( + final int max, final String errorMessage) { + return withRule(Conditions.lessThanOrEqualTo(max), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator le(int max, Supplier e) { - return le(max, convertToExceptionFunction(e)); + public IntPropertyValidator le( + final int max, final Supplier exceptionSupplier) { + return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public IntPropertyValidator le(int max, Function e) { - return withRule(value -> (value != null && value <= max), e); + public IntPropertyValidator le( + final int max, final Function exceptionFunction) { + return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction); } // ================================ @@ -250,4 +266,23 @@ public class IntPropertyValidator protected IntPropertyValidator thisObject() { return this; } + + private static class Conditions { + + private static Predicate greaterThan(int min) { + return input -> input == null || input > min; + } + + private static Predicate greaterThanOrEqualTo(int min) { + return input -> input == null || input >= min; + } + + private static Predicate lessThan(int max) { + return input -> input == null || input < max; + } + + private static Predicate lessThanOrEqualTo(int max) { + return input -> input == null || input <= max; + } + } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java index 6d7cbdc..c4f8d41 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java @@ -17,6 +17,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; /** @@ -44,43 +45,47 @@ public class LongPropertyValidator * @param min 最小值 * @return 属性校验器 */ - public LongPropertyValidator gt(long min) { - return gt(min, convertToExceptionFunction("The input must be greater than '%d'.", min)); + public final LongPropertyValidator gt(final long min) { + return withRule(Conditions.greaterThan(min), + "The input must be greater than '%d'.", min); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * * @param min 最小值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public LongPropertyValidator gt(long min, String errMsg) { - return gt(min, convertToExceptionFunction(errMsg)); + public final LongPropertyValidator gt( + final long min, final String errorMessage) { + return withRule(Conditions.greaterThan(min), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 错误信息 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator gt(long min, Supplier e) { - return gt(min, convertToExceptionFunction(e)); + public final LongPropertyValidator gt( + final long min, final Supplier exceptionSupplier) { + return withRule(Conditions.greaterThan(min), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否大于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator gt(long min, Function e) { - return withRule(value -> (value != null && value > min), e); + public final LongPropertyValidator gt( + final long min, final Function exceptionFunction) { + return withRule(Conditions.greaterThan(min), exceptionFunction); } // ================================ @@ -97,43 +102,47 @@ public class LongPropertyValidator * @param min 最小值 * @return 属性校验器 */ - public LongPropertyValidator ge(long min) { - return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%d'.", min)); + public final LongPropertyValidator ge(final long min) { + return withRule(Conditions.greaterThanOrEqualTo(min), + "The input must be greater than or equal to '%d'.", min); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * * @param min 最小值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public LongPropertyValidator ge(long min, String errMsg) { - return ge(min, convertToExceptionFunction(errMsg)); + public final LongPropertyValidator ge( + final long min, final String errorMessage) { + return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator ge(long min, Supplier e) { - return ge(min, convertToExceptionFunction(e)); + public final LongPropertyValidator ge( + final long min, final Supplier exceptionSupplier) { + return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否大于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param min 最小值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator ge(long min, Function e) { - return withRule(value -> (value != null && value >= min), e); + public final LongPropertyValidator ge( + final long min, final Function exceptionFunction) { + return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction); } // ================================ @@ -150,43 +159,47 @@ public class LongPropertyValidator * @param max 最大值 * @return 属性校验器 */ - public LongPropertyValidator lt(long max) { - return lt(max, convertToExceptionFunction("The input must be less than '%d'.", max)); + public final LongPropertyValidator lt(final long max) { + return withRule(Conditions.lessThan(max), + "The input must be less than '%d'.", max); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * * @param max 最大值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public LongPropertyValidator lt(long max, String errMsg) { - return lt(max, convertToExceptionFunction(errMsg)); + public final LongPropertyValidator lt( + final long max, final String errorMessage) { + return withRule(Conditions.lessThan(max), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator lt(long max, Supplier e) { - return lt(max, convertToExceptionFunction(e)); + public final LongPropertyValidator lt( + final long max, final Supplier exceptionSupplier) { + return withRule(Conditions.lessThan(max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否小于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator lt(long max, Function e) { - return withRule(value -> (value != null && value < max), e); + public final LongPropertyValidator lt( + final long max, final Function exceptionFunction) { + return withRule(Conditions.lessThan(max), exceptionFunction); } // ================================ @@ -203,43 +216,47 @@ public class LongPropertyValidator * @param max 最大值 * @return 属性校验器 */ - public LongPropertyValidator le(long max) { - return le(max, convertToExceptionFunction("The input must be less than or equal to '%d'.", max)); + public final LongPropertyValidator le(final long max) { + return withRule(Conditions.lessThanOrEqualTo(max), + "The input must be less than or equal to '%d'.", max); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * * @param max 最大值 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public LongPropertyValidator le(long max, String errMsg) { - return le(max, convertToExceptionFunction(errMsg)); + public final LongPropertyValidator le( + final long max, final String errorMessage) { + return withRule(Conditions.lessThanOrEqualTo(max), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator le(long max, Supplier e) { - return le(max, convertToExceptionFunction(e)); + public final LongPropertyValidator le( + final long max, final Supplier exceptionSupplier) { + return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否小于等于给定值 * - * @param 异常类型 + * @param 异常类型 * @param max 最大值 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public LongPropertyValidator le(long max, Function e) { - return withRule(value -> (value != null && value <= max), e); + public final LongPropertyValidator le( + final long max, final Function exceptionFunction) { + return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction); } // ================================ @@ -250,4 +267,23 @@ public class LongPropertyValidator protected LongPropertyValidator thisObject() { return this; } + + private static class Conditions { + + private static Predicate greaterThan(long min) { + return input -> input == null || input > min; + } + + private static Predicate greaterThanOrEqualTo(long min) { + return input -> input == null || input >= min; + } + + private static Predicate lessThan(long max) { + return input -> input == null || input < max; + } + + private static Predicate lessThanOrEqualTo(long max) { + return input -> input == null || input <= max; + } + } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/PairPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/PairPropertyValidator.java index d8f247e..0bb8652 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/PairPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/PairPropertyValidator.java @@ -36,7 +36,7 @@ public class PairPropertyValidator /** * 添加一条校验属性的规则,校验二元组是否满足给定的条件 * - * @param condition 校验规则 + * @param condition 校验条件 * @return 属性校验器 */ public final PairPropertyValidator must(BiPredicate condition) { @@ -46,37 +46,37 @@ public class PairPropertyValidator /** * 添加一条校验属性的规则,校验二元组是否满足给定的条件 * - * @param condition 校验规则 - * @param errMsg 错误信息 + * @param condition 校验条件 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public final PairPropertyValidator must(BiPredicate condition, String errMsg) { - return must(pair -> condition.test(pair.getKey(), pair.getValue()), errMsg); + public final PairPropertyValidator must(BiPredicate condition, String errorMessage) { + return must(pair -> condition.test(pair.getKey(), pair.getValue()), errorMessage); } /** * 添加一条校验属性的规则,校验二元组是否满足给定的条件 * - * @param condition 校验规则 - * @param e 自定义异常 + * @param condition 校验条件 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public final PairPropertyValidator must( - BiPredicate condition, Supplier e) { - return must(pair -> condition.test(pair.getKey(), pair.getValue()), e); + public final PairPropertyValidator must( + BiPredicate condition, Supplier exceptionSupplier) { + return must(pair -> condition.test(pair.getKey(), pair.getValue()), exceptionSupplier); } /** * 添加一条校验属性的规则,校验二元组是否满足给定的条件 * - * @param condition 校验规则 - * @param e 自定义异常 + * @param condition 校验条件 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public final PairPropertyValidator must( - BiPredicate condition, BiFunction e) { + public final PairPropertyValidator must( + BiPredicate condition, BiFunction exceptionFunction) { return must(pair -> condition.test(pair.getKey(), pair.getValue()), - pair -> e.apply(pair.getKey(), pair.getValue())); + pair -> exceptionFunction.apply(pair.getKey(), pair.getValue())); } @Override diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java index 5a96707..733b1ed 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java @@ -17,26 +17,24 @@ package xyz.zhouxy.plusone.validator; import java.util.Collection; -import java.util.List; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.regex.Pattern; import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import xyz.zhouxy.plusone.commons.util.ArrayTools; import xyz.zhouxy.plusone.commons.util.AssertTools; import xyz.zhouxy.plusone.commons.util.RegexTools; import xyz.zhouxy.plusone.commons.util.StringTools; /** - * StringPropertyValidator - * - *

- * 针对文本字段的校验器。 - *

+ * 针对文本类型的属性校验器。 * * @author ZhouXY */ -public class StringPropertyValidator extends BaseComparablePropertyValidator> { +public class StringPropertyValidator + extends BaseComparablePropertyValidator> { StringPropertyValidator(Function getter) { super(getter); @@ -49,38 +47,39 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< /** * 添加一条校验属性的规则,校验属性是否匹配正则表达式 * - * @param regex 正则表达式 - * @param errMsg 异常信息 + * @param pattern 正则表达式 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator matches(Pattern regex, String errMsg) { - return matches(regex, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator matches( + final Pattern pattern, final String errorMessage) { + return withRule(Conditions.matches(pattern), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否匹配正则表达式 * - * @param 异常类型 - * @param regex 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param pattern 正则表达式 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matches( - Pattern regex, Supplier e) { - return matches(regex, convertToExceptionFunction(e)); + public StringPropertyValidator matches( + final Pattern pattern, final Supplier exceptionSupplier) { + return withRule(Conditions.matches(pattern), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否匹配正则表达式 * - * @param 异常类型 - * @param regex 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param pattern 正则表达式 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matches( - Pattern regex, Function e) { - return withRule(input -> (input == null || RegexTools.matches(input, regex)), e); + public final StringPropertyValidator matches( + final Pattern pattern, final Function exceptionFunction) { + return withRule(Conditions.matches(pattern), exceptionFunction); } // ================================ @@ -94,75 +93,77 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< /** * 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个 * - * @param regexs 正则表达式 - * @param errMsg 异常信息 + * @param patterns 正则表达式 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator matchesOne(Pattern[] regexs, String errMsg) { - return matchesOne(regexs, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator matchesOne( + final Pattern[] patterns, final String errorMessage) { + return withRule(Conditions.matchesOne(patterns), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesOne( - Pattern[] regexs, Supplier e) { - return matchesOne(regexs, convertToExceptionFunction(e)); + public final StringPropertyValidator matchesOne( + final Pattern[] patterns, final Supplier exceptionSupplier) { + return withRule(Conditions.matchesOne(patterns), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 - * @return + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionFunction 自定义异常 + * @return 属性校验器 */ - public StringPropertyValidator matchesOne( - Pattern[] regexs, Function e) { - return withRule(input -> input == null || RegexTools.matchesOne(input, regexs), e); + public final StringPropertyValidator matchesOne( + final Pattern[] patterns, final Function exceptionFunction) { + return withRule(Conditions.matchesOne(patterns), exceptionFunction); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个 * - * @param regexs 正则表达式 - * @param errMsg 异常信息 + * @param patterns 正则表达式 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator matchesOne(List regexs, String errMsg) { - return matchesOne(regexs, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator matchesOne( + final Collection patterns, final String errorMessage) { + return withRule(Conditions.matchesOne(patterns), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesOne( - List regexs, Supplier e) { - return matchesOne(regexs, convertToExceptionFunction(e)); + public final StringPropertyValidator matchesOne( + final Collection patterns, final Supplier exceptionSupplier) { + return withRule(Conditions.matchesOne(patterns), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesOne( - List regexs, Function e) { - return withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), e); + public final StringPropertyValidator matchesOne( + final Collection patterns, final Function exceptionFunction) { + return withRule(Conditions.matchesOne(patterns), exceptionFunction); } // ================================ @@ -176,75 +177,77 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< /** * 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式 * - * @param regexs 正则表达式 - * @param errMsg 异常信息 + * @param patterns 正则表达式 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator matchesAll(Pattern[] regexs, String errMsg) { - return matchesAll(regexs, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator matchesAll( + final Pattern[] patterns, final String errorMessage) { + return withRule(Conditions.matchesAll(patterns), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesAll( - Pattern[] regexs, Supplier e) { - return matchesAll(regexs, convertToExceptionFunction(e)); + public final StringPropertyValidator matchesAll( + final Pattern[] patterns, final Supplier exceptionSupplier) { + return withRule(Conditions.matchesAll(patterns), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesAll( - Pattern[] regexs, Function e) { - return withRule(input -> input == null || RegexTools.matchesAll(input, regexs), e); + public final StringPropertyValidator matchesAll( + final Pattern[] patterns, final Function exceptionFunction) { + return withRule(Conditions.matchesAll(patterns), exceptionFunction); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式 * - * @param regexs 正则表达式 - * @param errMsg 异常信息 + * @param patterns 正则表达式 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator matchesAll(Collection regexs, String errMsg) { - return matchesAll(regexs, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator matchesAll( + final Collection patterns, final String errorMessage) { + return withRule(Conditions.matchesAll(patterns), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesAll( - Collection regexs, Supplier e) { - return matchesAll(regexs, convertToExceptionFunction(e)); + public final StringPropertyValidator matchesAll( + final Collection patterns, final Supplier exceptionSupplier) { + return withRule(Conditions.matchesAll(patterns), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式 * - * @param 异常类型 - * @param regexs 正则表达式 - * @param e 自定义异常 + * @param 异常类型 + * @param patterns 正则表达式 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator matchesAll( - Collection regexs, Function e) { - return withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), e); + public final StringPropertyValidator matchesAll( + final Collection patterns, final Function exceptionFunction) { + return withRule(Conditions.matchesAll(patterns), exceptionFunction); } // ================================ @@ -260,40 +263,43 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< * * @return 属性校验器 */ - public StringPropertyValidator notBlank() { - return notBlank("The input must not be blank."); + public final StringPropertyValidator notBlank() { + return withRule(Conditions.notBlank(), + "The input must not be blank."); } /** * 添加一条校验属性的规则,校验属性是否不为空白字符串 * - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator notBlank(String errMsg) { - return notBlank(convertToExceptionFunction(errMsg)); + public final StringPropertyValidator notBlank(final String errorMessage) { + return withRule(Conditions.notBlank(), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否不为空白字符串 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator notBlank(Supplier e) { - return notBlank(convertToExceptionFunction(e)); + public final StringPropertyValidator notBlank( + final Supplier exceptionSupplier) { + return withRule(Conditions.notBlank(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否不为空白字符串 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator notBlank(Function e) { - return withRule(StringTools::isNotBlank, e); + public final StringPropertyValidator notBlank( + final Function exceptionFunction) { + return withRule(Conditions.notBlank(), exceptionFunction); } // ================================ @@ -309,41 +315,43 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< * * @return 属性校验器 */ - public StringPropertyValidator emailAddress() { - return emailAddress("The input is not a valid email address."); + public final StringPropertyValidator emailAddress() { + return withRule(Conditions.emailAddress(), + "The input is not a valid email address."); } /** * 添加一条校验属性的规则,校验属性是否是邮箱地址 * - * @param errMsg 校验失败的错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator emailAddress(String errMsg) { - return emailAddress(convertToExceptionFunction(errMsg)); + public final StringPropertyValidator emailAddress(final String errorMessage) { + return withRule(Conditions.emailAddress(), errorMessage); } /** * 添加一条校验属性的规则,校验属性是否是邮箱地址 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator emailAddress(Supplier e) { - return emailAddress(convertToExceptionFunction(e)); + public final StringPropertyValidator emailAddress( + final Supplier exceptionSupplier) { + return withRule(Conditions.emailAddress(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性是否是邮箱地址 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator emailAddress( - Function e) { - return matches(PatternConsts.EMAIL, e); + public final StringPropertyValidator emailAddress( + final Function exceptionFunction) { + return withRule(Conditions.emailAddress(), exceptionFunction); } // ================================ @@ -359,41 +367,43 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< * * @return 属性校验器 */ - public StringPropertyValidator notEmpty() { - return notEmpty("The input must not be empty."); + public final StringPropertyValidator notEmpty() { + return withRule(Conditions.notEmpty(), + "The input must not be empty."); } /** * 添加一条校验属性的规则,校验字符串属性是否不为空 * - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator notEmpty(String errMsg) { - return notEmpty(convertToExceptionFunction(errMsg)); + public final StringPropertyValidator notEmpty(final String errorMessage) { + return withRule(Conditions.notEmpty(), errorMessage); } /** * 添加一条校验属性的规则,校验字符串属性是否不为空 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator notEmpty(Supplier e) { - return notEmpty(convertToExceptionFunction(e)); + public final StringPropertyValidator notEmpty( + final Supplier exceptionSupplier) { + return withRule(Conditions.notEmpty(), exceptionSupplier); } /** * 添加一条校验属性的规则,校验字符串属性是否不为空 * - * @param 异常类型 - * @param e 自定义异常 + * @param 异常类型 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator notEmpty( - Function e) { - return withRule(s -> s != null && !s.isEmpty(), e); + public final StringPropertyValidator notEmpty( + final Function exceptionFunction) { + return withRule(Conditions.notEmpty(), exceptionFunction); } // ================================ @@ -408,45 +418,38 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< * 添加一条校验属性的规则,校验属性长度是否等于指定长度 * * @param length 指定长度 - * @param errMsg 异常信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator length(int length, String errMsg) { - return length(length, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator length( + final int length, final String errorMessage) { + return withRule(Conditions.length(length), errorMessage); } /** * 添加一条校验属性的规则,校验属性长度是否等于指定长度 * - * @param 异常类型 + * @param 异常类型 * @param length 指定长度 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator length(int length, Supplier e) { - return length(length, convertToExceptionFunction(e)); + public final StringPropertyValidator length( + final int length, final Supplier exceptionSupplier) { + return withRule(Conditions.length(length), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性长度是否等于指定长度 * - * @param 异常类型 + * @param 异常类型 * @param length 指定长度 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator length(int length, Function e) { - AssertTools.checkArgument(length >= 0, - "The expected length must be greater than or equal to 0."); - return withRule(s -> s == null || s.length() == length, e); - } - - static boolean checkLength(String str, int min, int max) { - if (str == null) { - return true; - } - final int len = str.length(); - return len >= min && len <= max; + public final StringPropertyValidator length( + final int length, final Function exceptionFunction) { + return withRule(Conditions.length(length), exceptionFunction); } /** @@ -454,39 +457,40 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< * * @param min 最小长度 * @param max 最大长度 - * @param errMsg 错误信息 + * @param errorMessage 异常信息 * @return 属性校验器 */ - public StringPropertyValidator length(int min, int max, String errMsg) { - return length(min, max, convertToExceptionFunction(errMsg)); + public final StringPropertyValidator length( + final int min, final int max, final String errorMessage) { + return withRule(Conditions.length(min, max), errorMessage); } /** * 添加一条校验属性的规则,校验属性的长度范围 * + * @param 异常类型 * @param min 最小长度 * @param max 最大长度 - * @param e 自定义异常 + * @param exceptionSupplier 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator length(int min, int max, - Supplier e) { - return length(min, max, convertToExceptionFunction(e)); + public final StringPropertyValidator length( + final int min, final int max, final Supplier exceptionSupplier) { + return withRule(Conditions.length(min, max), exceptionSupplier); } /** * 添加一条校验属性的规则,校验属性的长度范围 * + * @param 异常类型 * @param min 最小长度 * @param max 最大长度 - * @param e 自定义异常 + * @param exceptionFunction 自定义异常 * @return 属性校验器 */ - public StringPropertyValidator length(int min, int max, - Function e) { - AssertTools.checkArgument(min >= 0, "min must be non-negative."); - AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); - return withRule(s -> checkLength(s, min, max), e); + public final StringPropertyValidator length( + final int min, final int max, final Function exceptionFunction) { + return withRule(Conditions.length(min, max), exceptionFunction); } // ================================ @@ -497,4 +501,63 @@ public class StringPropertyValidator extends BaseComparablePropertyValidator< protected StringPropertyValidator thisObject() { return this; } + + private static class Conditions { + + private static Predicate notEmpty() { + return StringTools::isNotEmpty; + } + + private static Predicate notBlank() { + return StringTools::isNotBlank; + } + + private static Predicate matches(Pattern pattern) { + AssertTools.checkArgumentNotNull(pattern); + return input -> input == null || RegexTools.matches(input, pattern); + } + + private static Predicate matchesOne(Pattern[] patterns) { + AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns)); + return input -> input == null || RegexTools.matchesOne(input, patterns); + } + + private static Predicate matchesOne(Collection patterns) { + AssertTools.checkArgumentNotNull(patterns, "patterns must not be null."); + return input -> input == null || RegexTools.matchesOne(input, patterns.toArray(new Pattern[0])); + } + + private static Predicate matchesAll(Pattern[] patterns) { + AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns)); + return input -> input == null || RegexTools.matchesAll(input, patterns); + } + + private static Predicate matchesAll(Collection patterns) { + AssertTools.checkArgumentNotNull(patterns, "patterns must not be null."); + return input -> input == null || RegexTools.matchesAll(input, patterns.toArray(new Pattern[0])); + } + + private static Predicate emailAddress() { + return input -> input == null || RegexTools.matches(input, PatternConsts.EMAIL); + } + + private static Predicate length(int length) { + AssertTools.checkArgument(length >= 0, + "The expected length must be non-negative."); + return input -> input == null || input.length() == length; + } + + private static Predicate length(int min, int max) { + AssertTools.checkArgument(min >= 0, "min must be non-negative."); + AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); + return input -> { + if (input == null) { + return true; + } + final int len = input.length(); + return len >= min && len <= max; + }; + } + } + } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java index 11133ba..70b065f 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ValidationException.java @@ -60,8 +60,8 @@ public class ValidationException extends RuntimeException { /** * 创建一个验证失败异常 * - * @param errorMessageTemplate 错误信息模版 - * @param errorMessageArgs 错误信息参数 + * @param errorMessageTemplate 异常信息模版 + * @param errorMessageArgs 异常信息参数 * @return 异常 */ public static ValidationException withMessage(String errorMessageTemplate, Object... errorMessageArgs) { diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java index 258cd70..714b3e2 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ComparablePropertyValidatorTests.java @@ -65,8 +65,9 @@ public class ComparablePropertyValidatorTests { }; ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MIN); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -156,89 +157,6 @@ public class ComparablePropertyValidatorTests { // #endregion - not in the interval // ================================ - // ================================ - // #region - null - // ================================ - - @Test - void inRange_default_valueIsNull() { - IValidator validator = new BaseValidator() { - { - ruleForComparable(ExampleCommand::getDateTimeProperty) - .inRange(DATE_TIME_RANGE); - } - }; - - ExampleCommand command = exampleCommandWithComparableProperty(null, null, null); - - ValidationException e = assertThrows( - ValidationException.class, - () -> validator.validate(command)); - - final String expected = String.format("The input must in the interval %s. You entered null.", DATE_TIME_RANGE); - assertEquals(expected, e.getMessage()); - } - - @Test - void inRange_message_valueIsNull() { - IValidator validator = new BaseValidator() { - { - ruleForComparable(ExampleCommand::getDateTimeProperty) - .inRange(DATE_TIME_RANGE, MESSAGE); - } - }; - - ExampleCommand command = exampleCommandWithComparableProperty(null, null, null); - - ValidationException e = assertThrows( - ValidationException.class, - () -> validator.validate(command)); - - assertEquals(MESSAGE, e.getMessage()); - } - - @Test - void inRange_exceptionSupplier_valueIsNull() { - IValidator validator = new BaseValidator() { - { - ruleForComparable(ExampleCommand::getDateTimeProperty) - .inRange(DATE_TIME_RANGE, () -> ExampleException.withMessage(MESSAGE)); - } - }; - - ExampleCommand command = exampleCommandWithComparableProperty(null, null, null); - - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - - assertEquals(MESSAGE, e.getMessage()); - } - - @Test - void inRange_exceptionFunction_valueIsNull() { - IValidator validator = new BaseValidator() { - { - ruleForComparable(ExampleCommand::getDateTimeProperty) - .inRange(DATE_TIME_RANGE, property -> ExampleException.withMessage( - "The dateTimeProperty should in the interval [%s,%s), but it is %s", MIN, MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithComparableProperty(null, null, null); - - ExampleException e = assertThrows( - ExampleException.class, - () -> validator.validate(command)); - - final String expected = String.format("The dateTimeProperty should in the interval [%s,%s), but it is null", MIN, MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - null - // ================================ - static ExampleCommand exampleCommandWithComparableProperty( Integer intProperty, Long longProperty, diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java index 42c8c3a..0d4c426 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/DoublePropertyValidatorTests.java @@ -18,7 +18,6 @@ package xyz.zhouxy.plusone.example.validator; import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -61,8 +60,9 @@ public class DoublePropertyValidatorTests { }; ExampleCommand command = exampleCommandWithDoubleProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -147,80 +147,6 @@ public class DoublePropertyValidatorTests { // #endregion - gt_invalidValue // ================================ - // ================================ - // #region - gt_null - // ================================ - - @Test - void gt_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .gt(MIN); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage()); - } - - @Test - void gt_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .gt(MIN, MESSAGE_GT); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GT, e.getMessage()); - } - - @Test - void gt_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .gt(MIN, () -> ExampleException.withMessage(MESSAGE_GT)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GT, e.getMessage()); - } - - @Test - void gt_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .gt(MIN, property -> ExampleException.withMessage( - "The doubleProperty should be greater than %s, but it is %s", MIN, property)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The doubleProperty should be greater than %s, but it is null", MIN); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - gt_null - // ================================ - // ================================ // #region - ge_validValue // ================================ @@ -243,8 +169,9 @@ public class DoublePropertyValidatorTests { }; ExampleCommand command = exampleCommandWithDoubleProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -329,80 +256,6 @@ public class DoublePropertyValidatorTests { // #endregion - ge_invalidValue // ================================ - // ================================ - // #region - ge_null - // ================================ - - @Test - void ge_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .ge(MIN); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage()); - } - - @Test - void ge_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .ge(MIN, MESSAGE_GE); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GE, e.getMessage()); - } - - @Test - void ge_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .ge(MIN, () -> ExampleException.withMessage(MESSAGE_GE)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GE, e.getMessage()); - } - - @Test - void ge_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .ge(MIN, property -> ExampleException.withMessage( - "The doubleProperty should be greater than or equal to %s, but it is %s", MIN, property)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The doubleProperty should be greater than or equal to %s, but it is null", MIN); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - ge_null - // ================================ - // ================================ // #region - lt_validValue // ================================ @@ -425,8 +278,9 @@ public class DoublePropertyValidatorTests { }; ExampleCommand command = exampleCommandWithDoubleProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -511,80 +365,6 @@ public class DoublePropertyValidatorTests { // #endregion - lt_invalidValue // ================================ - // ================================ - // #region - lt_null - // ================================ - - @Test - void lt_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .lt(MAX); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage()); - } - - @Test - void lt_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .lt(MAX, MESSAGE_LT); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LT, e.getMessage()); - } - - @Test - void lt_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .lt(MAX, () -> ExampleException.withMessage(MESSAGE_LT)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LT, e.getMessage()); - } - - @Test - void lt_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .lt(MAX, property -> ExampleException.withMessage( - "The doubleProperty should be less than %s, but it is %s", MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The doubleProperty should be less than %s, but it is null", MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - lt_null - // ================================ - // ================================ // #region - le_validValue // ================================ @@ -607,8 +387,9 @@ public class DoublePropertyValidatorTests { }; ExampleCommand command = exampleCommandWithDoubleProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -693,80 +474,6 @@ public class DoublePropertyValidatorTests { // #endregion - le_invalidValue // ================================ - // ================================ - // #region - le_null - // ================================ - - @Test - void le_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .le(MAX); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage()); - } - - @Test - void le_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .le(MAX, MESSAGE_LE); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LE, e.getMessage()); - } - - @Test - void le_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .le(MAX, () -> ExampleException.withMessage(MESSAGE_LE)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LE, e.getMessage()); - } - - @Test - void le_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForDouble(ExampleCommand::getDoubleProperty) - .le(MAX, property -> ExampleException.withMessage( - "The doubleProperty should be less than or equal to %s, but it is %s", MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithDoubleProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The doubleProperty should be less than or equal to %s, but it is null", MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - le_null - // ================================ - static ExampleCommand exampleCommandWithDoubleProperty(Double doubleProperty) { ExampleCommand exampleCommand = new ExampleCommand(); exampleCommand.setDoubleProperty(doubleProperty); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java index 7a5a489..b0974dd 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/IntPropertyValidatorTests.java @@ -17,7 +17,6 @@ package xyz.zhouxy.plusone.example.validator; import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -60,8 +59,9 @@ public class IntPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithIntProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -146,80 +146,6 @@ public class IntPropertyValidatorTests { // #endregion - gt_invalidValue // ================================ - // ================================ - // #region - gt_null - // ================================ - - @Test - void gt_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .gt(MIN); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); - } - - @Test - void gt_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .gt(MIN, MESSAGE_GT); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GT, e.getMessage()); - } - - @Test - void gt_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .gt(MIN, () -> ExampleException.withMessage(MESSAGE_GT)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GT, e.getMessage()); - } - - @Test - void gt_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .gt(MIN, property -> ExampleException.withMessage( - "The intProperty should be greater than %d, but it is %d", MIN, property)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The intProperty should be greater than %d, but it is null", MIN); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - gt_null - // ================================ - // ================================ // #region - ge_validValue // ================================ @@ -242,8 +168,9 @@ public class IntPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithIntProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -328,80 +255,6 @@ public class IntPropertyValidatorTests { // #endregion - ge_invalidValue // ================================ - // ================================ - // #region - ge_null - // ================================ - - @Test - void ge_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .ge(MIN); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); - } - - @Test - void ge_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .ge(MIN, MESSAGE_GE); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GE, e.getMessage()); - } - - @Test - void ge_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .ge(MIN, () -> ExampleException.withMessage(MESSAGE_GE)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GE, e.getMessage()); - } - - @Test - void ge_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .ge(MIN, property -> ExampleException.withMessage( - "The intProperty should be greater than or equal to %d, but it is %d", MIN, property)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The intProperty should be greater than or equal to %d, but it is null", MIN); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - ge_null - // ================================ - // ================================ // #region - lt_validValue // ================================ @@ -424,8 +277,9 @@ public class IntPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithIntProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -510,80 +364,6 @@ public class IntPropertyValidatorTests { // #endregion - lt_invalidValue // ================================ - // ================================ - // #region - lt_null - // ================================ - - @Test - void lt_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .lt(MAX); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); - } - - @Test - void lt_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .lt(MAX, MESSAGE_LT); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LT, e.getMessage()); - } - - @Test - void lt_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .lt(MAX, () -> ExampleException.withMessage(MESSAGE_LT)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LT, e.getMessage()); - } - - @Test - void lt_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .lt(MAX, property -> ExampleException.withMessage( - "The intProperty should be less than %d, but it is %d", MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The intProperty should be less than %d, but it is null", MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - lt_null - // ================================ - // ================================ // #region - le_validValue // ================================ @@ -606,8 +386,9 @@ public class IntPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithIntProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -692,80 +473,6 @@ public class IntPropertyValidatorTests { // #endregion - le_invalidValue // ================================ - // ================================ - // #region - le_null - // ================================ - - @Test - void le_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .le(MAX); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); - } - - @Test - void le_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .le(MAX, MESSAGE_LE); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LE, e.getMessage()); - } - - @Test - void le_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .le(MAX, () -> ExampleException.withMessage(MESSAGE_LE)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LE, e.getMessage()); - } - - @Test - void le_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForInt(ExampleCommand::getIntProperty) - .le(MAX, property -> ExampleException.withMessage( - "The intProperty should be less than or equal to %d, but it is %d", MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithIntProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The intProperty should be less than or equal to %d, but it is null", MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - le_null - // ================================ - static ExampleCommand exampleCommandWithIntProperty(Integer intProperty) { ExampleCommand exampleCommand = new ExampleCommand(); exampleCommand.setIntProperty(intProperty); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java index 0a3a3cd..0244050 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/LongPropertyValidatorTests.java @@ -17,7 +17,6 @@ package xyz.zhouxy.plusone.example.validator; import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -61,8 +60,9 @@ public class LongPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithLongProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -147,80 +147,6 @@ public class LongPropertyValidatorTests { // #endregion - gt_invalidValue // ================================ - // ================================ - // #region - gt_null - // ================================ - - @Test - void gt_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .gt(MIN); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage()); - } - - @Test - void gt_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .gt(MIN, MESSAGE_GT); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GT, e.getMessage()); - } - - @Test - void gt_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .gt(MIN, () -> ExampleException.withMessage(MESSAGE_GT)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GT, e.getMessage()); - } - - @Test - void gt_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .gt(MIN, property -> ExampleException.withMessage( - "The longProperty should be greater than %d, but it is %d", MIN, property)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The longProperty should be greater than %d, but it is null", MIN); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - gt_null - // ================================ - // ================================ // #region - ge_validValue // ================================ @@ -243,8 +169,9 @@ public class LongPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithLongProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -329,80 +256,6 @@ public class LongPropertyValidatorTests { // #endregion - ge_invalidValue // ================================ - // ================================ - // #region - ge_null - // ================================ - - @Test - void ge_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .ge(MIN); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage()); - } - - @Test - void ge_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .ge(MIN, MESSAGE_GE); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GE, e.getMessage()); - } - - @Test - void ge_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .ge(MIN, () -> ExampleException.withMessage(MESSAGE_GE)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_GE, e.getMessage()); - } - - @Test - void ge_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .ge(MIN, property -> ExampleException.withMessage( - "The longProperty should be greater than or equal to %d, but it is %d", MIN, property)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The longProperty should be greater than or equal to %d, but it is null", MIN); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - ge_null - // ================================ - // ================================ // #region - lt_validValue // ================================ @@ -425,8 +278,9 @@ public class LongPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithLongProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -511,80 +365,6 @@ public class LongPropertyValidatorTests { // #endregion - lt_invalidValue // ================================ - // ================================ - // #region - lt_null - // ================================ - - @Test - void lt_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .lt(MAX); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage()); - } - - @Test - void lt_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .lt(MAX, MESSAGE_LT); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LT, e.getMessage()); - } - - @Test - void lt_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .lt(MAX, () -> ExampleException.withMessage(MESSAGE_LT)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LT, e.getMessage()); - } - - @Test - void lt_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .lt(MAX, property -> ExampleException.withMessage( - "The longProperty should be less than %d, but it is %d", MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The longProperty should be less than %d, but it is null", MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - lt_null - // ================================ - // ================================ // #region - le_validValue // ================================ @@ -607,8 +387,9 @@ public class LongPropertyValidatorTests { }; ExampleCommand command = exampleCommandWithLongProperty(value); - assertDoesNotThrow(() -> validator.validate(command)); + + assertDoesNotThrow(() -> validator.validate(new ExampleCommand())); } // ================================ @@ -693,80 +474,6 @@ public class LongPropertyValidatorTests { // #endregion - le_invalidValue // ================================ - // ================================ - // #region - le_null - // ================================ - - @Test - void le_default_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .le(MAX); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage()); - } - - @Test - void le_message_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .le(MAX, MESSAGE_LE); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ValidationException e = assertThrows( - ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LE, e.getMessage()); - } - - @Test - void le_exceptionSupplier_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .le(MAX, () -> ExampleException.withMessage(MESSAGE_LE)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE_LE, e.getMessage()); - } - - @Test - void le_exceptionFunction_null() { - IValidator validator = new BaseValidator() { - { - ruleForLong(ExampleCommand::getLongProperty) - .le(MAX, property -> ExampleException.withMessage( - "The longProperty should be less than or equal to %d, but it is %d", MAX, property)); - } - }; - - ExampleCommand command = exampleCommandWithLongProperty(null); - - ExampleException e = assertThrows( - ExampleException.class, () -> validator.validate(command)); - final String expected = String.format("The longProperty should be less than or equal to %d, but it is null", MAX); - assertEquals(expected, e.getMessage()); - } - - // ================================ - // #endregion - le_null - // ================================ - static ExampleCommand exampleCommandWithLongProperty(Long longProperty) { ExampleCommand exampleCommand = new ExampleCommand(); exampleCommand.setLongProperty(longProperty); diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java index b6f0f70..d86bba2 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java @@ -26,10 +26,10 @@ import java.util.Objects; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import xyz.zhouxy.plusone.ExampleException; +import xyz.zhouxy.plusone.commons.function.PredicateTools; import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.example.Foo; @@ -214,19 +214,19 @@ public class ObjectPropertyValidatorTests { // ================================ // ================================ - // #region - equalTo + // #region - equal // ================================ @Test - void equalTo_validInput() { + void equal_validInput() { IValidator validator = new BaseValidator() { { ruleForString(ExampleCommand::getStringProperty) - .equalTo("Foo") - .equalTo("Foo", "The stringProperty should be equal to 'Foo'.") - .equalTo("Foo", () -> + .equal("Foo") + .equal("Foo", "The stringProperty should be equal to 'Foo'.") + .equal("Foo", () -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")) - .equalTo("Foo", str -> + .equal("Foo", str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); } }; @@ -240,13 +240,13 @@ public class ObjectPropertyValidatorTests { } @Test - void equalTo_invalidInput() { + void equal_invalidInput() { ExampleCommand command = new ExampleCommand(); command.setStringProperty("Bar"); IValidator defaultRule = new BaseValidator() { { - ruleForString(ExampleCommand::getStringProperty).equalTo("Foo"); + ruleForString(ExampleCommand::getStringProperty).equal("Foo"); } }; ValidationException eWithDefaultMessage = assertThrows( @@ -255,7 +255,7 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithMessage = new BaseValidator() { { - ruleForString(ExampleCommand::getStringProperty).equalTo("Foo", + ruleForString(ExampleCommand::getStringProperty).equal("Foo", "The stringProperty should be equal to 'Foo'."); } }; @@ -265,7 +265,7 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithExceptionSupplier = new BaseValidator() { { - ruleForString(ExampleCommand::getStringProperty).equalTo("Foo", + ruleForString(ExampleCommand::getStringProperty).equal("Foo", () -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'.")); } }; @@ -275,7 +275,7 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithExceptionFunction = new BaseValidator() { { - ruleForString(ExampleCommand::getStringProperty).equalTo("Foo", + ruleForString(ExampleCommand::getStringProperty).equal("Foo", str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str)); } }; @@ -285,7 +285,7 @@ public class ObjectPropertyValidatorTests { } // ================================ - // #endregion - equalTo + // #endregion - equal // ================================ // ================================ @@ -440,10 +440,10 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithDefaultMessage = new BaseValidator() { { ruleForString(ExampleCommand::getStringProperty) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo"))) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), "The stringProperty must be equal to 'Foo'.") - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals)) + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), "The stringProperty must be equal to 'Foo'.") + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")) + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); } }; assertDoesNotThrow(() -> ruleWithDefaultMessage.validate(command)); @@ -457,17 +457,17 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithDefaultMessage = new BaseValidator() { { ruleForString(ExampleCommand::getStringProperty) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo"))); + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals)); } }; ValidationException eWithDefaultMessage = assertThrows( ValidationException.class, () -> ruleWithDefaultMessage.validate(command)); - assertEquals("The specified conditions were not met for the input.", eWithDefaultMessage.getMessage()); + assertEquals("The specified condition was not met for the input.", eWithDefaultMessage.getMessage()); IValidator ruleWithMessage = new BaseValidator() { { ruleForString(ExampleCommand::getStringProperty) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), "The stringProperty must be equal to 'Foo'."); } }; @@ -478,7 +478,7 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithExceptionSupplier = new BaseValidator() { { ruleForString(ExampleCommand::getStringProperty) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'.")); } }; @@ -489,7 +489,7 @@ public class ObjectPropertyValidatorTests { IValidator ruleWithExceptionFunction = new BaseValidator() { { ruleForString(ExampleCommand::getStringProperty) - .must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), + .must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str)); } };