refactor!: 修改数值校验器的 API
原 `IntPropertyValidator`、`LongPropertyValidator` 和 `DoublePropertyValidator` 的 `between` 方法,使用“左闭右开”区间,但方法签名上并不明确,容易产生歧义。 现改为 `gt`(GreaterThan)、`ge`(GreaterThanOrEqualTo)、`lt`(LessThan)、`le`(LessThanOrEqualTo)。
This commit is contained in:
parent
297649c63a
commit
19b7926f0d
@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class DoublePropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
|
public class DoublePropertyValidator<DTO>
|
||||||
|
extends BaseComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
|
||||||
|
|
||||||
DoublePropertyValidator(Function<DTO, Double> getter) {
|
DoublePropertyValidator(Function<DTO, Double> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DoublePropertyValidator<DTO> between(double min, double max) {
|
// ================================
|
||||||
return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max)));
|
// #region - greater than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> gt(double min) {
|
||||||
|
return gt(min, String.format("The value should be greater than %s", min));
|
||||||
}
|
}
|
||||||
|
|
||||||
public DoublePropertyValidator<DTO> between(double min, double max, String errMsg) {
|
public DoublePropertyValidator<DTO> gt(double min, String errMsg) {
|
||||||
return between(min, max, convertExceptionCreator(errMsg));
|
return gt(min, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> DoublePropertyValidator<DTO> between(double min, double max,
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> gt(
|
||||||
Supplier<E> exceptionCreator) {
|
double min, Supplier<E> exceptionCreator) {
|
||||||
return between(min, max, convertExceptionCreator(exceptionCreator));
|
return gt(min, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> DoublePropertyValidator<DTO> between(double min, double max,
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> gt(
|
||||||
Function<Double, E> exceptionCreator) {
|
double min, Function<Double, E> exceptionCreator) {
|
||||||
withRule(value -> (value >= min && value < max), exceptionCreator);
|
withRule(value -> (value != null && value > min), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - greater than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - greater than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> ge(double min) {
|
||||||
|
return ge(min, String.format("The value should be greater than or equal to %s", min));
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> ge(double min, String errMsg) {
|
||||||
|
return ge(min, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> ge(
|
||||||
|
double min, Supplier<E> exceptionCreator) {
|
||||||
|
return ge(min, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> ge(
|
||||||
|
double min, Function<Double, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value >= min), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - greater than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - less than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> lt(double max) {
|
||||||
|
return lt(max, String.format("The value should be less than %s", max));
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> lt(double max, String errMsg) {
|
||||||
|
return lt(max, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> lt(
|
||||||
|
double max, Supplier<E> exceptionCreator) {
|
||||||
|
return lt(max, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> lt(
|
||||||
|
double max, Function<Double, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value < max), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - less than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - less than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> le(double max) {
|
||||||
|
return le(max, String.format("The value should be less than or equal to %s", max));
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoublePropertyValidator<DTO> le(double max, String errMsg) {
|
||||||
|
return le(max, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> le(
|
||||||
|
double max, Supplier<E> exceptionCreator) {
|
||||||
|
return le(max, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> DoublePropertyValidator<DTO> le(
|
||||||
|
double max, Function<Double, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value <= max), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - less than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DoublePropertyValidator<DTO> thisObject() {
|
protected DoublePropertyValidator<DTO> thisObject() {
|
||||||
return this;
|
return this;
|
||||||
|
@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class IntPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
|
public class IntPropertyValidator<DTO>
|
||||||
|
extends BaseComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
|
||||||
|
|
||||||
IntPropertyValidator(Function<DTO, Integer> getter) {
|
IntPropertyValidator(Function<DTO, Integer> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntPropertyValidator<DTO> between(int min, int max) {
|
// ================================
|
||||||
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
|
// #region - greater than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> gt(int min) {
|
||||||
|
return gt(min, String.format("The value should be greater than %d", min));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntPropertyValidator<DTO> between(int min, int max, String errMsg) {
|
public IntPropertyValidator<DTO> gt(int min, String errMsg) {
|
||||||
return between(min, max, convertExceptionCreator(errMsg));
|
return gt(min, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> IntPropertyValidator<DTO> between(int min, int max,
|
public <E extends RuntimeException> IntPropertyValidator<DTO> gt(
|
||||||
Supplier<E> exceptionCreator) {
|
int min, Supplier<E> exceptionCreator) {
|
||||||
return between(min, max, convertExceptionCreator(exceptionCreator));
|
return gt(min, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> IntPropertyValidator<DTO> between(int min, int max,
|
public <E extends RuntimeException> IntPropertyValidator<DTO> gt(
|
||||||
Function<Integer, E> exceptionCreator) {
|
int min, Function<Integer, E> exceptionCreator) {
|
||||||
withRule(value -> (value >= min && value < max), exceptionCreator);
|
withRule(value -> (value != null && value > min), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - greater than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - greater than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> ge(int min) {
|
||||||
|
return ge(min, String.format("The value should be greater than or equal to %d", min));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> ge(int min, String errMsg) {
|
||||||
|
return ge(min, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> IntPropertyValidator<DTO> ge(
|
||||||
|
int min, Supplier<E> exceptionCreator) {
|
||||||
|
return ge(min, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> IntPropertyValidator<DTO> ge(
|
||||||
|
int min, Function<Integer, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value >= min), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - greater than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - less than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> lt(int max) {
|
||||||
|
return lt(max, String.format("The value should be less than %d", max));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> lt(int max, String errMsg) {
|
||||||
|
return lt(max, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> IntPropertyValidator<DTO> lt(
|
||||||
|
int max, Supplier<E> exceptionCreator) {
|
||||||
|
return lt(max, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> IntPropertyValidator<DTO> lt(
|
||||||
|
int max, Function<Integer, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value < max), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - less than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - less than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> le(int max) {
|
||||||
|
return le(max, String.format("The value should be less than or equal to %d", max));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntPropertyValidator<DTO> le(int max, String errMsg) {
|
||||||
|
return le(max, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> IntPropertyValidator<DTO> le(
|
||||||
|
int max, Supplier<E> exceptionCreator) {
|
||||||
|
return le(max, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> IntPropertyValidator<DTO> le(
|
||||||
|
int max, Function<Integer, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value <= max), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - less than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected IntPropertyValidator<DTO> thisObject() {
|
protected IntPropertyValidator<DTO> thisObject() {
|
||||||
return this;
|
return this;
|
||||||
|
@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class LongPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
|
public class LongPropertyValidator<DTO>
|
||||||
|
extends BaseComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
|
||||||
|
|
||||||
LongPropertyValidator(Function<DTO, Long> getter) {
|
LongPropertyValidator(Function<DTO, Long> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LongPropertyValidator<DTO> between(long min, long max) {
|
// ================================
|
||||||
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
|
// #region - greater than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> gt(long min) {
|
||||||
|
return gt(min, String.format("The value should be greater than %d", min));
|
||||||
}
|
}
|
||||||
|
|
||||||
public LongPropertyValidator<DTO> between(long min, long max, String errMsg) {
|
public LongPropertyValidator<DTO> gt(long min, String errMsg) {
|
||||||
return between(min, max, convertExceptionCreator(errMsg));
|
return gt(min, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> LongPropertyValidator<DTO> between(long min, long max,
|
public <E extends RuntimeException> LongPropertyValidator<DTO> gt(
|
||||||
Supplier<E> exceptionCreator) {
|
long min, Supplier<E> exceptionCreator) {
|
||||||
return between(min, max, convertExceptionCreator(exceptionCreator));
|
return gt(min, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> LongPropertyValidator<DTO> between(long min, long max,
|
public <E extends RuntimeException> LongPropertyValidator<DTO> gt(
|
||||||
Function<Long, E> exceptionCreator) {
|
long min, Function<Long, E> exceptionCreator) {
|
||||||
withRule(value -> (value >= min && value < max), exceptionCreator);
|
withRule(value -> (value != null && value > min), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - greater than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - greater than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> ge(long min) {
|
||||||
|
return ge(min, String.format("The value should be greater than or equal to %d", min));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> ge(long min, String errMsg) {
|
||||||
|
return ge(min, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> LongPropertyValidator<DTO> ge(
|
||||||
|
long min, Supplier<E> exceptionCreator) {
|
||||||
|
return ge(min, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> LongPropertyValidator<DTO> ge(
|
||||||
|
long min, Function<Long, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value >= min), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - greater than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - less than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> lt(long max) {
|
||||||
|
return lt(max, String.format("The value should be less than %d", max));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> lt(long max, String errMsg) {
|
||||||
|
return lt(max, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> LongPropertyValidator<DTO> lt(
|
||||||
|
long max, Supplier<E> exceptionCreator) {
|
||||||
|
return lt(max, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> LongPropertyValidator<DTO> lt(
|
||||||
|
long max, Function<Long, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value < max), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - less than
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - less than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> le(long max) {
|
||||||
|
return le(max, String.format("The value should be less than or equal to %d", max));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LongPropertyValidator<DTO> le(long max, String errMsg) {
|
||||||
|
return le(max, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> LongPropertyValidator<DTO> le(
|
||||||
|
long max, Supplier<E> exceptionCreator) {
|
||||||
|
return le(max, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> LongPropertyValidator<DTO> le(
|
||||||
|
long max, Function<Long, E> exceptionCreator) {
|
||||||
|
withRule(value -> (value != null && value <= max), exceptionCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - less than or equal to
|
||||||
|
// ================================
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected LongPropertyValidator<DTO> thisObject() {
|
protected LongPropertyValidator<DTO> thisObject() {
|
||||||
return this;
|
return this;
|
||||||
|
@ -14,7 +14,6 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
|||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
import xyz.zhouxy.plusone.validator.MapValidator;
|
import xyz.zhouxy.plusone.validator.MapValidator;
|
||||||
|
|
||||||
public //
|
|
||||||
class MapValidatorTests {
|
class MapValidatorTests {
|
||||||
|
|
||||||
private static final MapValidator<String, Object> validator = ParamsValidator.INSTANCE;
|
private static final MapValidator<String, Object> validator = ParamsValidator.INSTANCE;
|
||||||
@ -72,7 +71,8 @@ class ParamsValidator extends MapValidator<String, Object> {
|
|||||||
|
|
||||||
ruleForInt(AGE)
|
ruleForInt(AGE)
|
||||||
.withRule(Objects::nonNull)
|
.withRule(Objects::nonNull)
|
||||||
.between(18, 61);
|
.ge(18)
|
||||||
|
.le(60);
|
||||||
|
|
||||||
ruleForBool(BOOLEAN)
|
ruleForBool(BOOLEAN)
|
||||||
.notNull("Boolean property could not be null.")
|
.notNull("Boolean property could not be null.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user