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 9e859d3..d8fd23c 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 @@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class DoublePropertyValidator extends BaseComparablePropertyValidator> { +public class DoublePropertyValidator + extends BaseComparablePropertyValidator> { DoublePropertyValidator(Function getter) { super(getter); } - public DoublePropertyValidator between(double min, double max) { - return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); + // ================================ + // #region - greater than + // ================================ + + public DoublePropertyValidator gt(double min) { + return gt(min, String.format("The value should be greater than %s", min)); } - public DoublePropertyValidator between(double min, double max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); + public DoublePropertyValidator gt(double min, String errMsg) { + return gt(min, convertExceptionCreator(errMsg)); } - public DoublePropertyValidator between(double min, double max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); + public DoublePropertyValidator gt( + double min, Supplier exceptionCreator) { + return gt(min, convertExceptionCreator(exceptionCreator)); } - public DoublePropertyValidator between(double min, double max, - Function exceptionCreator) { - withRule(value -> (value >= min && value < max), exceptionCreator); + public DoublePropertyValidator gt( + double min, Function exceptionCreator) { + withRule(value -> (value != null && value > min), exceptionCreator); return this; } + // ================================ + // #endregion - greater than + // ================================ + + // ================================ + // #region - greater than or equal to + // ================================ + + public DoublePropertyValidator ge(double min) { + return ge(min, String.format("The value should be greater than or equal to %s", min)); + } + + public DoublePropertyValidator ge(double min, String errMsg) { + return ge(min, convertExceptionCreator(errMsg)); + } + + public DoublePropertyValidator ge( + double min, Supplier exceptionCreator) { + return ge(min, convertExceptionCreator(exceptionCreator)); + } + + public DoublePropertyValidator ge( + double min, Function exceptionCreator) { + withRule(value -> (value != null && value >= min), exceptionCreator); + return this; + } + + // ================================ + // #endregion - greater than or equal to + // ================================ + + // ================================ + // #region - less than + // ================================ + + public DoublePropertyValidator lt(double max) { + return lt(max, String.format("The value should be less than %s", max)); + } + + public DoublePropertyValidator lt(double max, String errMsg) { + return lt(max, convertExceptionCreator(errMsg)); + } + + public DoublePropertyValidator lt( + double max, Supplier exceptionCreator) { + return lt(max, convertExceptionCreator(exceptionCreator)); + } + + public DoublePropertyValidator lt( + double max, Function exceptionCreator) { + withRule(value -> (value != null && value < max), exceptionCreator); + return this; + } + + // ================================ + // #endregion - less than + // ================================ + + // ================================ + // #region - less than or equal to + // ================================ + + public DoublePropertyValidator le(double max) { + return le(max, String.format("The value should be less than or equal to %s", max)); + } + + public DoublePropertyValidator le(double max, String errMsg) { + return le(max, convertExceptionCreator(errMsg)); + } + + public DoublePropertyValidator le( + double max, Supplier exceptionCreator) { + return le(max, convertExceptionCreator(exceptionCreator)); + } + + public DoublePropertyValidator le( + double max, Function exceptionCreator) { + withRule(value -> (value != null && value <= max), exceptionCreator); + return this; + } + + // ================================ + // #endregion - less than or equal to + // ================================ + @Override protected DoublePropertyValidator thisObject() { return this; 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 241f078..4b0d078 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 @@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class IntPropertyValidator extends BaseComparablePropertyValidator> { +public class IntPropertyValidator + extends BaseComparablePropertyValidator> { IntPropertyValidator(Function getter) { super(getter); } - public IntPropertyValidator between(int min, int max) { - return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max)); + // ================================ + // #region - greater than + // ================================ + + public IntPropertyValidator gt(int min) { + return gt(min, String.format("The value should be greater than %d", min)); } - public IntPropertyValidator between(int min, int max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); + public IntPropertyValidator gt(int min, String errMsg) { + return gt(min, convertExceptionCreator(errMsg)); } - public IntPropertyValidator between(int min, int max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); + public IntPropertyValidator gt( + int min, Supplier exceptionCreator) { + return gt(min, convertExceptionCreator(exceptionCreator)); } - public IntPropertyValidator between(int min, int max, - Function exceptionCreator) { - withRule(value -> (value >= min && value < max), exceptionCreator); + public IntPropertyValidator gt( + int min, Function exceptionCreator) { + withRule(value -> (value != null && value > min), exceptionCreator); return this; } + // ================================ + // #endregion - greater than + // ================================ + + // ================================ + // #region - greater than or equal to + // ================================ + + public IntPropertyValidator ge(int min) { + return ge(min, String.format("The value should be greater than or equal to %d", min)); + } + + public IntPropertyValidator ge(int min, String errMsg) { + return ge(min, convertExceptionCreator(errMsg)); + } + + public IntPropertyValidator ge( + int min, Supplier exceptionCreator) { + return ge(min, convertExceptionCreator(exceptionCreator)); + } + + public IntPropertyValidator ge( + int min, Function exceptionCreator) { + withRule(value -> (value != null && value >= min), exceptionCreator); + return this; + } + + // ================================ + // #endregion - greater than or equal to + // ================================ + + // ================================ + // #region - less than + // ================================ + + public IntPropertyValidator lt(int max) { + return lt(max, String.format("The value should be less than %d", max)); + } + + public IntPropertyValidator lt(int max, String errMsg) { + return lt(max, convertExceptionCreator(errMsg)); + } + + public IntPropertyValidator lt( + int max, Supplier exceptionCreator) { + return lt(max, convertExceptionCreator(exceptionCreator)); + } + + public IntPropertyValidator lt( + int max, Function exceptionCreator) { + withRule(value -> (value != null && value < max), exceptionCreator); + return this; + } + + // ================================ + // #endregion - less than + // ================================ + + // ================================ + // #region - less than or equal to + // ================================ + + public IntPropertyValidator le(int max) { + return le(max, String.format("The value should be less than or equal to %d", max)); + } + + public IntPropertyValidator le(int max, String errMsg) { + return le(max, convertExceptionCreator(errMsg)); + } + + public IntPropertyValidator le( + int max, Supplier exceptionCreator) { + return le(max, convertExceptionCreator(exceptionCreator)); + } + + public IntPropertyValidator le( + int max, Function exceptionCreator) { + withRule(value -> (value != null && value <= max), exceptionCreator); + return this; + } + + // ================================ + // #endregion - less than or equal to + // ================================ + @Override protected IntPropertyValidator thisObject() { return this; 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 9ee5070..9c77bbd 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 @@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class LongPropertyValidator extends BaseComparablePropertyValidator> { +public class LongPropertyValidator + extends BaseComparablePropertyValidator> { LongPropertyValidator(Function getter) { super(getter); } - public LongPropertyValidator between(long min, long max) { - return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max)); + // ================================ + // #region - greater than + // ================================ + + public LongPropertyValidator gt(long min) { + return gt(min, String.format("The value should be greater than %d", min)); } - public LongPropertyValidator between(long min, long max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); + public LongPropertyValidator gt(long min, String errMsg) { + return gt(min, convertExceptionCreator(errMsg)); } - public LongPropertyValidator between(long min, long max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); + public LongPropertyValidator gt( + long min, Supplier exceptionCreator) { + return gt(min, convertExceptionCreator(exceptionCreator)); } - public LongPropertyValidator between(long min, long max, - Function exceptionCreator) { - withRule(value -> (value >= min && value < max), exceptionCreator); + public LongPropertyValidator gt( + long min, Function exceptionCreator) { + withRule(value -> (value != null && value > min), exceptionCreator); return this; } + // ================================ + // #endregion - greater than + // ================================ + + // ================================ + // #region - greater than or equal to + // ================================ + + public LongPropertyValidator ge(long min) { + return ge(min, String.format("The value should be greater than or equal to %d", min)); + } + + public LongPropertyValidator ge(long min, String errMsg) { + return ge(min, convertExceptionCreator(errMsg)); + } + + public LongPropertyValidator ge( + long min, Supplier exceptionCreator) { + return ge(min, convertExceptionCreator(exceptionCreator)); + } + + public LongPropertyValidator ge( + long min, Function exceptionCreator) { + withRule(value -> (value != null && value >= min), exceptionCreator); + return this; + } + + // ================================ + // #endregion - greater than or equal to + // ================================ + + // ================================ + // #region - less than + // ================================ + + public LongPropertyValidator lt(long max) { + return lt(max, String.format("The value should be less than %d", max)); + } + + public LongPropertyValidator lt(long max, String errMsg) { + return lt(max, convertExceptionCreator(errMsg)); + } + + public LongPropertyValidator lt( + long max, Supplier exceptionCreator) { + return lt(max, convertExceptionCreator(exceptionCreator)); + } + + public LongPropertyValidator lt( + long max, Function exceptionCreator) { + withRule(value -> (value != null && value < max), exceptionCreator); + return this; + } + + // ================================ + // #endregion - less than + // ================================ + + // ================================ + // #region - less than or equal to + // ================================ + + public LongPropertyValidator le(long max) { + return le(max, String.format("The value should be less than or equal to %d", max)); + } + + public LongPropertyValidator le(long max, String errMsg) { + return le(max, convertExceptionCreator(errMsg)); + } + + public LongPropertyValidator le( + long max, Supplier exceptionCreator) { + return le(max, convertExceptionCreator(exceptionCreator)); + } + + public LongPropertyValidator le( + long max, Function exceptionCreator) { + withRule(value -> (value != null && value <= max), exceptionCreator); + return this; + } + + // ================================ + // #endregion - less than or equal to + // ================================ + @Override protected LongPropertyValidator thisObject() { return this; diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java index 60500c4..7009c9f 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java @@ -14,7 +14,6 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.validator.MapValidator; -public // class MapValidatorTests { private static final MapValidator validator = ParamsValidator.INSTANCE; @@ -72,7 +71,8 @@ class ParamsValidator extends MapValidator { ruleForInt(AGE) .withRule(Objects::nonNull) - .between(18, 61); + .ge(18) + .le(60); ruleForBool(BOOLEAN) .notNull("Boolean property could not be null.")