forked from plusone/plusone-validator
docs: 完善文档注释
This commit is contained in:
parent
9f0c7cd2fb
commit
a4d91dde35
@ -21,6 +21,18 @@ import java.util.function.Supplier;
|
||||
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
/**
|
||||
* 针对 {@code Comparable} 类型的属性校验器基类
|
||||
*
|
||||
* <p>
|
||||
* 内置了判断属性是否在给定区间内的校验规则。
|
||||
*
|
||||
* @param <T> 待校验对象类型
|
||||
* @param <TProperty> 属性类型
|
||||
* @param <TPropertyValidator> 当前属性校验器类型,用于链式调用
|
||||
* @see Range
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public abstract class BaseComparablePropertyValidator<T, TProperty extends Comparable<TProperty>, TPropertyValidator extends BaseComparablePropertyValidator<T, TProperty, TPropertyValidator>>
|
||||
extends BasePropertyValidator<T, TProperty, TPropertyValidator> {
|
||||
|
||||
@ -28,6 +40,12 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
|
||||
super(getter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
*
|
||||
* @param range 区间
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator inRange(Range<TProperty> range) {
|
||||
withRule(value -> value != null && range.contains(value),
|
||||
value -> new IllegalArgumentException(
|
||||
@ -35,30 +53,41 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
*
|
||||
* @param range 区间
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator inRange(Range<TProperty> range, String errMsg) {
|
||||
withRule(value -> value != null && range.contains(value), convertExceptionCreator(errMsg));
|
||||
withRule(value -> value != null && range.contains(value), convertToExceptionFunction(errMsg));
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
*
|
||||
* @param range 区间
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator inRange(
|
||||
Range<TProperty> range,
|
||||
Supplier<E> exceptionCreator) {
|
||||
withRule(value -> value != null && range.contains(value), exceptionCreator);
|
||||
Range<TProperty> range, Supplier<E> e) {
|
||||
withRule(value -> value != null && range.contains(value), e);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
*
|
||||
* @param range 区间
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator inRange(
|
||||
Range<TProperty> range,
|
||||
Function<TProperty, E> exceptionCreator) {
|
||||
withRule(value -> value != null && range.contains(value), exceptionCreator);
|
||||
Range<TProperty> range, Function<TProperty, E> e) {
|
||||
withRule(value -> value != null && range.contains(value), e);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
|
||||
return value -> new IllegalArgumentException(errMsg);
|
||||
}
|
||||
|
||||
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
|
||||
return value -> exceptionSupplier.get();
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,15 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 属性校验器。包含针对属性的校验规则。
|
||||
*
|
||||
* <p>
|
||||
* <i>内置基础的校验规则。</i>
|
||||
* </p>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator extends BasePropertyValidator<T, TProperty, TPropertyValidator>> {
|
||||
|
||||
private final Function<T, ? extends TProperty> getter;
|
||||
@ -35,20 +44,47 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
this.getter = getter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param rule 校验规则
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public final TPropertyValidator withRule(Predicate<? super TProperty> rule) {
|
||||
return withRule(rule, v -> new IllegalArgumentException());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param rule 校验规则
|
||||
* @param errMsg 校验失败的错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public final TPropertyValidator withRule(
|
||||
Predicate<? super TProperty> rule, String errMsg) {
|
||||
return withRule(rule, convertExceptionCreator(errMsg));
|
||||
return withRule(rule, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param rule 校验规则
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public final <E extends RuntimeException> TPropertyValidator withRule(
|
||||
Predicate<? super TProperty> rule, Supplier<E> e) {
|
||||
return withRule(rule, convertExceptionCreator(e));
|
||||
return withRule(rule, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param rule 校验规则
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public final <E extends RuntimeException> TPropertyValidator withRule(
|
||||
Predicate<? super TProperty> rule, Function<TProperty, E> e) {
|
||||
this.consumers.add(v -> {
|
||||
@ -59,9 +95,13 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
public final void validate(T obj) {
|
||||
/**
|
||||
* 校验属性
|
||||
* @param propertyValue 属性值
|
||||
*/
|
||||
public final void validate(T propertyValue) {
|
||||
for (Consumer<? super TProperty> consumer : consumers) {
|
||||
consumer.accept(getter.apply(obj));
|
||||
consumer.accept(getter.apply(propertyValue));
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,20 +115,45 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
// #region - notNull
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator notNull() {
|
||||
return notNull("The input must not be null.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空
|
||||
*
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator notNull(String errMsg) {
|
||||
return notNull(convertExceptionCreator(errMsg));
|
||||
return notNull(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> TPropertyValidator notNull(Supplier<E> exceptionCreator) {
|
||||
return notNull(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator notNull(Supplier<E> e) {
|
||||
return notNull(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> TPropertyValidator notNull(Function<TProperty, E> exceptionCreator) {
|
||||
withRule(Objects::nonNull, exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator notNull(Function<TProperty, E> e) {
|
||||
withRule(Objects::nonNull, e);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
@ -100,20 +165,45 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
// #region - isNull
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator isNull() {
|
||||
return isNull("The input must be null.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator isNull(String errMsg) {
|
||||
return isNull(convertExceptionCreator(errMsg));
|
||||
return isNull(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> TPropertyValidator isNull(Supplier<E> exceptionCreator) {
|
||||
return isNull(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator isNull(Supplier<E> e) {
|
||||
return isNull(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> TPropertyValidator isNull(Function<TProperty, E> exceptionCreator) {
|
||||
withRule(Objects::isNull, exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator isNull(Function<TProperty, E> e) {
|
||||
withRule(Objects::isNull, e);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
@ -125,23 +215,52 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
// #region - equalTo
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param that 给定值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator equalTo(Object that) {
|
||||
return equalTo(that,
|
||||
value -> new IllegalArgumentException(String.format("The input must be equal to '%s'.", that)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param that 给定值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator equalTo(Object that, String errMsg) {
|
||||
return equalTo(that, convertExceptionCreator(errMsg));
|
||||
return equalTo(that, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param that 给定值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator equalTo(
|
||||
Object that, Supplier<E> exceptionCreator) {
|
||||
return equalTo(that, convertExceptionCreator(exceptionCreator));
|
||||
Object that, Supplier<E> e) {
|
||||
return equalTo(that, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param that 给定值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator equalTo(
|
||||
Object that, Function<TProperty, E> exceptionCreator) {
|
||||
withRule(value -> Objects.equals(value, that), exceptionCreator);
|
||||
Object that, Function<TProperty, E> e) {
|
||||
withRule(value -> Objects.equals(value, that), e);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
@ -153,46 +272,103 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
// #region - must
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否满足给定的条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator must(Predicate<? super TProperty> condition) {
|
||||
return must(condition, "The specified condition was not met for the input.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否满足给定的条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator must(Predicate<? super TProperty> condition, String errMsg) {
|
||||
return must(condition, convertExceptionCreator(errMsg));
|
||||
return must(condition, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否满足给定的条件
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param condition 校验规则
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator must(
|
||||
Predicate<? super TProperty> condition,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return must(condition, convertExceptionCreator(exceptionCreator));
|
||||
Supplier<E> e) {
|
||||
return must(condition, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否满足给定的条件
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param condition 校验规则
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator must(
|
||||
Predicate<? super TProperty> condition,
|
||||
Function<TProperty, E> exceptionCreator) {
|
||||
withRule(condition, exceptionCreator);
|
||||
Function<TProperty, E> e) {
|
||||
withRule(condition, e);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多条校验属性的规则,校验属性是否满足给定的所有条件
|
||||
*
|
||||
* @param conditions 校验规则
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) {
|
||||
return must(conditions, "The specified conditions were not met for the input.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多条校验属性的规则,校验属性是否满足给定的所有条件
|
||||
*
|
||||
* @param conditions 校验规则
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
|
||||
return must(conditions, convertExceptionCreator(errMsg));
|
||||
return must(conditions, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多条校验属性的规则,校验属性是否满足给定的所有条件
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param conditions 校验规则
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator must(
|
||||
Collection<Predicate<? super TProperty>> conditions,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return must(conditions, convertExceptionCreator(exceptionCreator));
|
||||
Collection<Predicate<? super TProperty>> conditions, Supplier<E> e) {
|
||||
return must(conditions, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加多条校验属性的规则,校验属性是否满足给定的所有条件
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param conditions 校验规则
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> TPropertyValidator must(
|
||||
Collection<Predicate<? super TProperty>> conditions,
|
||||
Function<TProperty, E> exceptionCreator) {
|
||||
Function<TProperty, E> e) {
|
||||
for (Predicate<? super TProperty> condition : conditions) {
|
||||
withRule(condition, exceptionCreator);
|
||||
withRule(condition, e);
|
||||
}
|
||||
return thisObject();
|
||||
}
|
||||
@ -201,11 +377,12 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
// #endregion - must
|
||||
// ================================
|
||||
|
||||
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
|
||||
static <V> Function<V, IllegalArgumentException> convertToExceptionFunction(String errMsg) {
|
||||
return value -> new IllegalArgumentException(errMsg);
|
||||
}
|
||||
|
||||
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
|
||||
static <V, E extends RuntimeException> Function<V, E> convertToExceptionFunction(
|
||||
Supplier<E> exceptionSupplier) {
|
||||
return value -> exceptionSupplier.get();
|
||||
}
|
||||
}
|
||||
|
@ -27,120 +27,226 @@ import java.util.function.Supplier;
|
||||
import xyz.zhouxy.plusone.validator.function.*;
|
||||
|
||||
/**
|
||||
* BaseValidator
|
||||
*
|
||||
* <p>
|
||||
* 校验器的基类
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: content.</b>
|
||||
* </p>
|
||||
* 通过继承 {@code BaseValidator},可以自定义一个针对特定类型的校验器,包含对该类型的校验逻辑。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
|
||||
/**
|
||||
* 规则集合
|
||||
*/
|
||||
private final List<Consumer<? super T>> rules = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
*
|
||||
* @param rule 校验规则
|
||||
* @param errorMessage 错误信息
|
||||
*/
|
||||
protected final void withRule(final Predicate<? super T> rule, final String errorMessage) {
|
||||
withRule(rule, () -> new IllegalArgumentException(errorMessage));
|
||||
}
|
||||
|
||||
protected final <E extends RuntimeException> void withRule(Predicate<? super T> rule, Supplier<E> exceptionBuilder) {
|
||||
withRule(rule, value -> exceptionBuilder.get());
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param rule 校验规则
|
||||
* @param e 自定义异常
|
||||
*/
|
||||
protected final <E extends RuntimeException> void withRule(
|
||||
final Predicate<? super T> rule, final Supplier<E> e) {
|
||||
withRule(rule, value -> e.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param e 自定义异常
|
||||
*/
|
||||
protected final <E extends RuntimeException> void withRule(
|
||||
Predicate<? super T> condition, Function<T, E> exceptionBuilder) {
|
||||
final Predicate<? super T> condition, final Function<T, E> e) {
|
||||
withRule(value -> {
|
||||
if (!condition.test(value)) {
|
||||
throw exceptionBuilder.apply(value);
|
||||
throw e.apply(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
*
|
||||
* @param rule 校验规则。内部包含断言条件,如果条件不满足,则抛出异常。
|
||||
*/
|
||||
protected final void withRule(Consumer<? super T> rule) {
|
||||
this.rules.add(rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final <R> ObjectPropertyValidator<T, R> ruleFor(Function<T, R> getter) {
|
||||
ObjectPropertyValidator<T, R> validator = new ObjectPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
protected final <R extends Comparable<R>> ComparablePropertyValidator<T, R> ruleForComparable(Function<T, R> getter) {
|
||||
/**
|
||||
* 添加一个针对 {@code Comparable} 属性的校验器
|
||||
*
|
||||
* @param <R> 属性类型
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final <R extends Comparable<R>> ComparablePropertyValidator<T, R> ruleForComparable(
|
||||
Function<T, R> getter) {
|
||||
ComparablePropertyValidator<T, R> validator = new ComparablePropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Integer} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final IntPropertyValidator<T> ruleForInt(Function<T, Integer> getter) {
|
||||
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Integer} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final IntPropertyValidator<T> ruleFor(ToIntegerFunction<T> getter) {
|
||||
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Long} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final LongPropertyValidator<T> ruleForLong(Function<T, Long> getter) {
|
||||
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Long} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final LongPropertyValidator<T> ruleFor(ToLongObjectFunction<T> getter) {
|
||||
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Double} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final DoublePropertyValidator<T> ruleForDouble(Function<T, Double> getter) {
|
||||
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Double} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final DoublePropertyValidator<T> ruleFor(ToDoubleObjectFunction<T> getter) {
|
||||
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Boolean} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final BoolPropertyValidator<T> ruleForBool(Function<T, Boolean> getter) {
|
||||
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Boolean} 属性的校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final BoolPropertyValidator<T> ruleFor(ToBoolObjectFunction<T> getter) {
|
||||
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code String} 属性的校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final StringPropertyValidator<T> ruleForString(Function<T, String> getter) {
|
||||
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code String} 属性的校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final StringPropertyValidator<T> ruleFor(ToStringFunction<T> getter) {
|
||||
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Collection} 属性的校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 集合属性校验器
|
||||
*/
|
||||
protected final <E> CollectionPropertyValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
|
||||
CollectionPropertyValidator<T, E> validator = new CollectionPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void validate(T obj) {
|
||||
this.rules.forEach(rule -> rule.accept(obj));
|
||||
|
@ -19,6 +19,15 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 针对 {@code Boolean} 类型的属性校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置了判断 Boolean 值是否为 true 或 false 的校验规则。
|
||||
* </p>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
|
||||
|
||||
BoolPropertyValidator(Function<T, Boolean> getter) {
|
||||
@ -27,43 +36,89 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
|
||||
|
||||
// ====== isTrueValue ======
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public BoolPropertyValidator<T> isTrueValue() {
|
||||
return isTrueValue("The input must be true.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
*
|
||||
* @param errMsg 校验失败的错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public BoolPropertyValidator<T> isTrueValue(String errMsg) {
|
||||
return isTrueValue(convertExceptionCreator(errMsg));
|
||||
return isTrueValue(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
|
||||
Supplier<E> exceptionCreator) {
|
||||
return isTrueValue(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(Supplier<E> e) {
|
||||
return isTrueValue(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
|
||||
Function<Boolean, E> exceptionCreator) {
|
||||
withRule(Boolean.TRUE::equals, exceptionCreator);
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(Function<Boolean, E> e) {
|
||||
withRule(Boolean.TRUE::equals, e);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ====== isFalseValue ======
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public BoolPropertyValidator<T> isFalseValue() {
|
||||
return isFalseValue("The input must be false.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
*
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public BoolPropertyValidator<T> isFalseValue(String errMsg) {
|
||||
return isFalseValue(convertExceptionCreator(errMsg));
|
||||
return isFalseValue(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
|
||||
Supplier<E> exceptionCreator) {
|
||||
return isFalseValue(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(Supplier<E> e) {
|
||||
return isFalseValue(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
|
||||
Function<Boolean, E> exceptionCreator) {
|
||||
withRule(Boolean.FALSE::equals, exceptionCreator);
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(Function<Boolean, E> e) {
|
||||
withRule(Boolean.FALSE::equals, e);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,14 @@ import java.util.function.Supplier;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
|
||||
/**
|
||||
* 针对集合类型的属性校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置判断集合是否为空的校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class CollectionPropertyValidator<T, TElement>
|
||||
extends BasePropertyValidator<T, Collection<TElement>, CollectionPropertyValidator<T, TElement>> {
|
||||
|
||||
@ -31,43 +39,91 @@ public class CollectionPropertyValidator<T, TElement>
|
||||
|
||||
// ====== notEmpty =====
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public CollectionPropertyValidator<T, TElement> notEmpty() {
|
||||
return notEmpty("The input must not be empty.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public CollectionPropertyValidator<T, TElement> notEmpty(String errMsg) {
|
||||
return notEmpty(convertExceptionCreator(errMsg));
|
||||
return notEmpty(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
|
||||
Supplier<E> exceptionCreator) {
|
||||
return notEmpty(convertExceptionCreator(exceptionCreator));
|
||||
Supplier<E> e) {
|
||||
return notEmpty(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @param <E> 自定义异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
|
||||
Function<Collection<TElement>, E> exceptionCreator) {
|
||||
withRule(CollectionTools::isNotEmpty, exceptionCreator);
|
||||
Function<Collection<TElement>, E> e) {
|
||||
withRule(CollectionTools::isNotEmpty, e);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ====== isEmpty =====
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public CollectionPropertyValidator<T, TElement> isEmpty() {
|
||||
return isEmpty("The input must be empty.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public CollectionPropertyValidator<T, TElement> isEmpty(String errMsg) {
|
||||
return isEmpty(convertExceptionCreator(errMsg));
|
||||
return isEmpty(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
|
||||
Supplier<E> exceptionCreator) {
|
||||
return isEmpty(convertExceptionCreator(exceptionCreator));
|
||||
Supplier<E> e) {
|
||||
return isEmpty(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
|
||||
Function<Collection<TElement>, E> exceptionCreator) {
|
||||
withRule(CollectionTools::isEmpty, exceptionCreator);
|
||||
Function<Collection<TElement>, E> e) {
|
||||
withRule(CollectionTools::isEmpty, e);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,18 @@ package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 针对 {@code Comparable} 类型的默认属性校验器
|
||||
*
|
||||
* <p>
|
||||
* 继承自 {@link BaseComparablePropertyValidator},内置了判断属性是否在给定区间内的校验规则。
|
||||
*
|
||||
* @param <T> 待校验对象类型
|
||||
* @param <TProperty> 属性类型
|
||||
* @param <TPropertyValidator> 当前属性校验器类型,用于链式调用
|
||||
* @see Range
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class ComparablePropertyValidator<T, TProperty extends Comparable<TProperty>>
|
||||
extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> {
|
||||
|
||||
|
@ -19,6 +19,14 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 浮点数属性校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置对 {@code Double} 类型常用的校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class DoublePropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Double, DoublePropertyValidator<T>> {
|
||||
|
||||
@ -30,23 +38,50 @@ public class DoublePropertyValidator<T>
|
||||
// #region - greater than
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> gt(double min) {
|
||||
return gt(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than '%s'.", min)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> gt(double min, String errMsg) {
|
||||
return gt(min, convertExceptionCreator(errMsg));
|
||||
return gt(min, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> gt(
|
||||
double min, Supplier<E> exceptionCreator) {
|
||||
return gt(min, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> gt(double min, Supplier<E> e) {
|
||||
return gt(min, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> gt(
|
||||
double min, Function<Double, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value > min), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> gt(double min, Function<Double, E> e) {
|
||||
withRule(value -> (value != null && value > min), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -58,23 +93,50 @@ public class DoublePropertyValidator<T>
|
||||
// #region - greater than or equal to
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> ge(double min) {
|
||||
return ge(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than or equal to '%s'.", min)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> ge(double min, String errMsg) {
|
||||
return ge(min, convertExceptionCreator(errMsg));
|
||||
return ge(min, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> ge(
|
||||
double min, Supplier<E> exceptionCreator) {
|
||||
return ge(min, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> ge(double min, Supplier<E> e) {
|
||||
return ge(min, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> ge(
|
||||
double min, Function<Double, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value >= min), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> ge(double min, Function<Double, E> e) {
|
||||
withRule(value -> (value != null && value >= min), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -86,23 +148,50 @@ public class DoublePropertyValidator<T>
|
||||
// #region - less than
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> lt(double max) {
|
||||
return lt(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than '%s'.", max)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> lt(double max, String errMsg) {
|
||||
return lt(max, convertExceptionCreator(errMsg));
|
||||
return lt(max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> lt(
|
||||
double max, Supplier<E> exceptionCreator) {
|
||||
return lt(max, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> lt(double max, Supplier<E> e) {
|
||||
return lt(max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> lt(
|
||||
double max, Function<Double, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value < max), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> lt(double max, Function<Double, E> e) {
|
||||
withRule(value -> (value != null && value < max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -114,23 +203,50 @@ public class DoublePropertyValidator<T>
|
||||
// #region - less than or equal to
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> le(double max) {
|
||||
return le(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than or equal to '%s'.", max)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public DoublePropertyValidator<T> le(double max, String errMsg) {
|
||||
return le(max, convertExceptionCreator(errMsg));
|
||||
return le(max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> le(
|
||||
double max, Supplier<E> exceptionCreator) {
|
||||
return le(max, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> le(double max, Supplier<E> e) {
|
||||
return le(max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> le(
|
||||
double max, Function<Double, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value <= max), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> DoublePropertyValidator<T> le(double max, Function<Double, E> e) {
|
||||
withRule(value -> (value != null && value <= max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -22,5 +22,10 @@ package xyz.zhouxy.plusone.validator;
|
||||
*/
|
||||
public interface IValidator<T> {
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @param obj
|
||||
*/
|
||||
void validate(T obj);
|
||||
}
|
||||
|
@ -19,6 +19,14 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 整数属性校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置对 {@code Integer} 类型常用的校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class IntPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Integer, IntPropertyValidator<T>> {
|
||||
|
||||
@ -30,23 +38,50 @@ public class IntPropertyValidator<T>
|
||||
// #region - greater than
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> gt(int min) {
|
||||
return gt(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than '%d'.", min)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> gt(int min, String errMsg) {
|
||||
return gt(min, convertExceptionCreator(errMsg));
|
||||
return gt(min, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> gt(
|
||||
int min, Supplier<E> exceptionCreator) {
|
||||
return gt(min, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> gt(int min, Supplier<E> e) {
|
||||
return gt(min, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> gt(
|
||||
int min, Function<Integer, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value > min), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> gt(int min, Function<Integer, E> e) {
|
||||
withRule(value -> (value != null && value > min), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -58,23 +93,50 @@ public class IntPropertyValidator<T>
|
||||
// #region - greater than or equal to
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> ge(int min) {
|
||||
return ge(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than or equal to '%d'.", min)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> ge(int min, String errMsg) {
|
||||
return ge(min, convertExceptionCreator(errMsg));
|
||||
return ge(min, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> ge(
|
||||
int min, Supplier<E> exceptionCreator) {
|
||||
return ge(min, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> ge(int min, Supplier<E> e) {
|
||||
return ge(min, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> ge(
|
||||
int min, Function<Integer, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value >= min), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> ge(int min, Function<Integer, E> e) {
|
||||
withRule(value -> (value != null && value >= min), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -86,23 +148,50 @@ public class IntPropertyValidator<T>
|
||||
// #region - less than
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> lt(int max) {
|
||||
return lt(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than '%d'.", max)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> lt(int max, String errMsg) {
|
||||
return lt(max, convertExceptionCreator(errMsg));
|
||||
return lt(max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> lt(
|
||||
int max, Supplier<E> exceptionCreator) {
|
||||
return lt(max, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> lt(int max, Supplier<E> e) {
|
||||
return lt(max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> lt(
|
||||
int max, Function<Integer, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value < max), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> lt(int max, Function<Integer, E> e) {
|
||||
withRule(value -> (value != null && value < max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -114,23 +203,50 @@ public class IntPropertyValidator<T>
|
||||
// #region - less than or equal to
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> le(int max) {
|
||||
return le(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than or equal to '%d'.", max)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public IntPropertyValidator<T> le(int max, String errMsg) {
|
||||
return le(max, convertExceptionCreator(errMsg));
|
||||
return le(max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> le(
|
||||
int max, Supplier<E> exceptionCreator) {
|
||||
return le(max, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> le(int max, Supplier<E> e) {
|
||||
return le(max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> le(
|
||||
int max, Function<Integer, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value <= max), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> IntPropertyValidator<T> le(int max, Function<Integer, E> e) {
|
||||
withRule(value -> (value != null && value <= max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,14 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 长整数属性校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置对 {@code Long} 类型常用的校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class LongPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Long, LongPropertyValidator<T>> {
|
||||
|
||||
@ -30,23 +38,50 @@ public class LongPropertyValidator<T>
|
||||
// #region - greater than
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> gt(long min) {
|
||||
return gt(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than '%d'.", min)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> gt(long min, String errMsg) {
|
||||
return gt(min, convertExceptionCreator(errMsg));
|
||||
return gt(min, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> gt(
|
||||
long min, Supplier<E> exceptionCreator) {
|
||||
return gt(min, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> gt(long min, Supplier<E> e) {
|
||||
return gt(min, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> gt(
|
||||
long min, Function<Long, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value > min), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> gt(long min, Function<Long, E> e) {
|
||||
withRule(value -> (value != null && value > min), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -58,23 +93,50 @@ public class LongPropertyValidator<T>
|
||||
// #region - greater than or equal to
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> ge(long min) {
|
||||
return ge(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than or equal to '%d'.", min)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> ge(long min, String errMsg) {
|
||||
return ge(min, convertExceptionCreator(errMsg));
|
||||
return ge(min, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> ge(
|
||||
long min, Supplier<E> exceptionCreator) {
|
||||
return ge(min, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> ge(long min, Supplier<E> e) {
|
||||
return ge(min, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> ge(
|
||||
long min, Function<Long, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value >= min), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param min 最小值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> ge(long min, Function<Long, E> e) {
|
||||
withRule(value -> (value != null && value >= min), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -86,23 +148,50 @@ public class LongPropertyValidator<T>
|
||||
// #region - less than
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> lt(long max) {
|
||||
return lt(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than '%d'.", max)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> lt(long max, String errMsg) {
|
||||
return lt(max, convertExceptionCreator(errMsg));
|
||||
return lt(max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> lt(
|
||||
long max, Supplier<E> exceptionCreator) {
|
||||
return lt(max, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> lt(long max, Supplier<E> e) {
|
||||
return lt(max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> lt(
|
||||
long max, Function<Long, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value < max), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> lt(long max, Function<Long, E> e) {
|
||||
withRule(value -> (value != null && value < max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -114,23 +203,50 @@ public class LongPropertyValidator<T>
|
||||
// #region - less than or equal to
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> le(long max) {
|
||||
return le(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than or equal to '%d'.", max)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public LongPropertyValidator<T> le(long max, String errMsg) {
|
||||
return le(max, convertExceptionCreator(errMsg));
|
||||
return le(max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> le(
|
||||
long max, Supplier<E> exceptionCreator) {
|
||||
return le(max, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> le(long max, Supplier<E> e) {
|
||||
return le(max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> le(
|
||||
long max, Function<Long, E> exceptionCreator) {
|
||||
withRule(value -> (value != null && value <= max), exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param max 最大值
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> LongPropertyValidator<T> le(long max, Function<Long, E> e) {
|
||||
withRule(value -> (value != null && value <= max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -23,28 +23,67 @@ import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 对 Map 进行校验的校验器
|
||||
*
|
||||
* <p>
|
||||
* 校验后拷贝出一个新的 Map 对象,仅保留指定的 key。
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
|
||||
private final Set<K> keys;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param keys 要保留的 key 的集合
|
||||
*/
|
||||
protected MapValidator(K[] keys) {
|
||||
this(Arrays.asList(keys));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param keys 要保留的 key 的集合
|
||||
*/
|
||||
protected MapValidator(Collection<K> keys) {
|
||||
this.keys = keys.stream().collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
// ========== validate & validateAndCopy ==========
|
||||
// ================================
|
||||
// #region - validateAndCopy
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 校验并拷贝,仅保留指定 key 的属性。
|
||||
*
|
||||
* @param obj 待校验的 map
|
||||
* @return 拷贝后的 map
|
||||
*/
|
||||
public final Map<K, V> validateAndCopy(Map<K, V> obj) {
|
||||
return validateAndCopyInternal(obj, this.keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并拷贝,仅保留指定 key 的属性。
|
||||
*
|
||||
* @param obj 待校验的 map
|
||||
* @param keys 指定 key
|
||||
* @return 拷贝后的 map
|
||||
*/
|
||||
public final Map<K, V> validateAndCopy(Map<K, V> obj, Collection<K> keys) {
|
||||
return validateAndCopyInternal(obj, keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并拷贝,仅保留指定 key 的属性。
|
||||
*
|
||||
* @param obj 待校验的 map
|
||||
* @param keys 待校验的 key
|
||||
* @return 拷贝后的 map
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final Map<K, V> validateAndCopy(Map<K, V> obj, K... keys) {
|
||||
return validateAndCopyInternal(obj, Arrays.asList(keys));
|
||||
@ -57,42 +96,101 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
// ========== ruleFor ==========
|
||||
// ================================
|
||||
// #endregion - validateAndCopy
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ruleFor
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final ObjectPropertyValidator<Map<K, V>, V> ruleFor(K key) {
|
||||
final Function<Map<K, V>, V> func = m -> m.get(key);
|
||||
return ruleFor(func);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 {@code Integer} 类型的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final IntPropertyValidator<Map<K, V>> ruleForInt(K key) {
|
||||
return ruleForInt(m -> (Integer) m.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 {@code Long} 类型的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final LongPropertyValidator<Map<K, V>> ruleForLong(K key) {
|
||||
return ruleForLong(m -> (Long) m.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 {@code Double} 类型的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final DoublePropertyValidator<Map<K, V>> ruleForDouble(K key) {
|
||||
return ruleForDouble(m -> (Double) m.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 {@code Boolean} 类型的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final BoolPropertyValidator<Map<K, V>> ruleForBool(K key) {
|
||||
return ruleForBool(m -> (Boolean) m.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 {@code String} 类型的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final StringPropertyValidator<Map<K, V>> ruleForString(K key) {
|
||||
return ruleForString(m -> (String) m.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的 {@code Comparable} 的 value 进行校验
|
||||
*
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final <E extends Comparable<E>> ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<Map<K, V>, E> getter = m -> (E) m.get(key);
|
||||
return ruleForComparable(getter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定 key 对应的集合类型的 value 进行校验
|
||||
*
|
||||
* @param <E> 集合元素的类型
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final <E> CollectionPropertyValidator<Map<K, V>, E> ruleForCollection(K key) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<Map<K, V>, Collection<E>> getter = m -> (Collection<E>) m.get(key);
|
||||
return ruleForCollection(getter);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ruleFor
|
||||
// ================================
|
||||
}
|
||||
|
@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 通用类型属性校验器。继承自 {@link BasePropertyValidator},包含针对属性的校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public class ObjectPropertyValidator<T, TProperty>
|
||||
extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> {
|
||||
|
||||
|
@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
* StringPropertyValidator
|
||||
*
|
||||
* <p>
|
||||
* 针对文本字段的验证器。
|
||||
* 针对文本字段的校验器。
|
||||
* </p>
|
||||
*
|
||||
* @author ZhouXY
|
||||
@ -46,20 +46,41 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - matches
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配正则表达式
|
||||
*
|
||||
* @param regex 正则表达式
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> matches(Pattern regex, String errMsg) {
|
||||
return matches(regex, convertExceptionCreator(errMsg));
|
||||
return matches(regex, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配正则表达式
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regex 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matches(
|
||||
Pattern regex,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matches(regex, convertExceptionCreator(exceptionCreator));
|
||||
Pattern regex, Supplier<E> e) {
|
||||
return matches(regex, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配正则表达式
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regex 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matches(
|
||||
Pattern regex,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> (input == null || RegexTools.matches(input, regex)), exceptionCreator);
|
||||
Pattern regex, Function<String, E> e) {
|
||||
withRule(input -> (input == null || RegexTools.matches(input, regex)), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -71,37 +92,79 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - matchesOne
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
*
|
||||
* @param regexs 正则表达式
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> matchesOne(Pattern[] regexs, String errMsg) {
|
||||
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||
return matchesOne(regexs, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
|
||||
Pattern[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
|
||||
Pattern[] regexs, Supplier<E> e) {
|
||||
return matchesOne(regexs, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
|
||||
Pattern[] regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> input == null || RegexTools.matchesOne(input, regexs), exceptionCreator);
|
||||
Pattern[] regexs, Function<String, E> e) {
|
||||
withRule(input -> input == null || RegexTools.matchesOne(input, regexs), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
*
|
||||
* @param regexs 正则表达式
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> matchesOne(List<Pattern> regexs, String errMsg) {
|
||||
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||
return matchesOne(regexs, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
|
||||
List<Pattern> regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
|
||||
List<Pattern> regexs, Supplier<E> e) {
|
||||
return matchesOne(regexs, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
|
||||
List<Pattern> regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
List<Pattern> regexs, Function<String, E> e) {
|
||||
withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -113,37 +176,79 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - matchesAll
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param regexs 正则表达式
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> matchesAll(Pattern[] regexs, String errMsg) {
|
||||
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||
return matchesAll(regexs, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
Pattern[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
|
||||
Pattern[] regexs, Supplier<E> e) {
|
||||
return matchesAll(regexs, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
Pattern[] regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> input == null || RegexTools.matchesAll(input, regexs), exceptionCreator);
|
||||
Pattern[] regexs, Function<String, E> e) {
|
||||
withRule(input -> input == null || RegexTools.matchesAll(input, regexs), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param regexs 正则表达式
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> matchesAll(Collection<Pattern> regexs, String errMsg) {
|
||||
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||
return matchesAll(regexs, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
Collection<Pattern> regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
|
||||
Collection<Pattern> regexs, Supplier<E> e) {
|
||||
return matchesAll(regexs, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param regexs 正则表达式
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
Collection<Pattern> regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
Collection<Pattern> regexs, Function<String, E> e) {
|
||||
withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -155,21 +260,45 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - notBlank
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> notBlank() {
|
||||
return notBlank("The input must not be blank.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> notBlank(String errMsg) {
|
||||
return notBlank(convertExceptionCreator(errMsg));
|
||||
return notBlank(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Supplier<E> exceptionCreator) {
|
||||
return notBlank(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Supplier<E> e) {
|
||||
return notBlank(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(StringTools::isNotBlank, exceptionCreator);
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Function<String, E> e) {
|
||||
withRule(StringTools::isNotBlank, e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -181,22 +310,46 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - emailAddress
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> emailAddress() {
|
||||
return emailAddress("The input is not a valid email address.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
*
|
||||
* @param errMsg 校验失败的错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> emailAddress(String errMsg) {
|
||||
return emailAddress(convertExceptionCreator(errMsg));
|
||||
return emailAddress(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(
|
||||
Supplier<E> exceptionCreator) {
|
||||
return emailAddress(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(Supplier<E> e) {
|
||||
return emailAddress(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(
|
||||
Function<String, E> exceptionCreator) {
|
||||
return matches(PatternConsts.EMAIL, exceptionCreator);
|
||||
Function<String, E> e) {
|
||||
return matches(PatternConsts.EMAIL, e);
|
||||
}
|
||||
|
||||
// ================================
|
||||
@ -207,21 +360,46 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - notEmpty
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> notEmpty() {
|
||||
return notEmpty("The input must not be empty.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> notEmpty(String errMsg) {
|
||||
return notEmpty(convertExceptionCreator(errMsg));
|
||||
return notEmpty(convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(Supplier<E> exceptionCreator) {
|
||||
return notEmpty(convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(Supplier<E> e) {
|
||||
return notEmpty(convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(s -> s != null && !s.isEmpty(), exceptionCreator);
|
||||
Function<String, E> e) {
|
||||
withRule(s -> s != null && !s.isEmpty(), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -233,20 +411,41 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// #region - length
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param length 指定长度
|
||||
* @param errMsg 异常信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> length(int length, String errMsg) {
|
||||
return length(length, convertExceptionCreator(errMsg));
|
||||
return length(length, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int length,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return length(length, convertExceptionCreator(exceptionCreator));
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param length 指定长度
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int length, Supplier<E> e) {
|
||||
return length(length, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int length,
|
||||
Function<String, E> exceptionCreator) {
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param <E> 异常类型
|
||||
* @param length 指定长度
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int length, Function<String, E> e) {
|
||||
AssertTools.checkArgument(length >= 0,
|
||||
"The expected length must be greater than or equal to 0.");
|
||||
withRule(s -> s == null || s.length() == length, exceptionCreator);
|
||||
withRule(s -> s == null || s.length() == length, e);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -258,20 +457,44 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
return len >= min && len <= max;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param errMsg 错误信息
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public StringPropertyValidator<T> length(int min, int max, String errMsg) {
|
||||
return length(min, max, convertExceptionCreator(errMsg));
|
||||
return length(min, max, convertToExceptionFunction(errMsg));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return length(min, max, convertExceptionCreator(exceptionCreator));
|
||||
Supplier<E> e) {
|
||||
return length(min, max, convertToExceptionFunction(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param e 自定义异常
|
||||
* @return 属性校验器
|
||||
*/
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
|
||||
Function<String, E> exceptionCreator) {
|
||||
Function<String, E> e) {
|
||||
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
|
||||
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
|
||||
withRule(s -> length(s, min, max), exceptionCreator);
|
||||
withRule(s -> length(s, min, max), e);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Function<T, Boolean>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToBoolObjectFunction<T> extends Function<T, Boolean>, Serializable {
|
||||
}
|
||||
|
@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Function<T, Double>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToDoubleObjectFunction<T> extends Function<T, Double>, Serializable {
|
||||
}
|
||||
|
@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Function<T, Integer>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToIntegerFunction<T> extends Function<T, Integer>, Serializable {
|
||||
}
|
||||
|
@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Function<T, Long>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToLongObjectFunction<T> extends Function<T, Long>, Serializable {
|
||||
}
|
||||
|
@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Function<T, String>
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToStringFunction<T> extends Function<T, String>, Serializable {
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user