docs: 完善文档注释

This commit is contained in:
zhouxy108 2025-05-29 02:45:58 +08:00
parent 9f0c7cd2fb
commit a4d91dde35
18 changed files with 1373 additions and 234 deletions

View File

@ -21,6 +21,18 @@ import java.util.function.Supplier;
import com.google.common.collect.Range; 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>> public abstract class BaseComparablePropertyValidator<T, TProperty extends Comparable<TProperty>, TPropertyValidator extends BaseComparablePropertyValidator<T, TProperty, TPropertyValidator>>
extends BasePropertyValidator<T, TProperty, TPropertyValidator> { extends BasePropertyValidator<T, TProperty, TPropertyValidator> {
@ -28,6 +40,12 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
super(getter); super(getter);
} }
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @return 属性校验器
*/
public TPropertyValidator inRange(Range<TProperty> range) { public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value), withRule(value -> value != null && range.contains(value),
value -> new IllegalArgumentException( value -> new IllegalArgumentException(
@ -35,30 +53,41 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
return thisObject(); return thisObject();
} }
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @param errMsg 错误信息
* @return 属性校验器
*/
public TPropertyValidator inRange(Range<TProperty> range, String errMsg) { 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(); return thisObject();
} }
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator inRange( public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range, Range<TProperty> range, Supplier<E> e) {
Supplier<E> exceptionCreator) { withRule(value -> value != null && range.contains(value), e);
withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject(); return thisObject();
} }
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator inRange( public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range, Range<TProperty> range, Function<TProperty, E> e) {
Function<TProperty, E> exceptionCreator) { withRule(value -> value != null && range.contains(value), e);
withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject(); 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();
}
} }

View File

@ -25,6 +25,15 @@ import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
/**
* 属性校验器包含针对属性的校验规则
*
* <p>
* <i>内置基础的校验规则</i>
* </p>
*
* @author ZhouXY
*/
public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator extends BasePropertyValidator<T, TProperty, TPropertyValidator>> { public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator extends BasePropertyValidator<T, TProperty, TPropertyValidator>> {
private final Function<T, ? extends TProperty> getter; private final Function<T, ? extends TProperty> getter;
@ -35,20 +44,47 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
this.getter = getter; this.getter = getter;
} }
/**
* 添加一条校验属性的规则
*
* @param rule 校验规则
* @return 属性校验器
*/
public final TPropertyValidator withRule(Predicate<? super TProperty> rule) { public final TPropertyValidator withRule(Predicate<? super TProperty> rule) {
return withRule(rule, v -> new IllegalArgumentException()); return withRule(rule, v -> new IllegalArgumentException());
} }
/**
* 添加一条校验属性的规则
*
* @param rule 校验规则
* @param errMsg 校验失败的错误信息
* @return 属性校验器
*/
public final TPropertyValidator withRule( public final TPropertyValidator withRule(
Predicate<? super TProperty> rule, String errMsg) { 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( public final <E extends RuntimeException> TPropertyValidator withRule(
Predicate<? super TProperty> rule, Supplier<E> e) { 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( public final <E extends RuntimeException> TPropertyValidator withRule(
Predicate<? super TProperty> rule, Function<TProperty, E> e) { Predicate<? super TProperty> rule, Function<TProperty, E> e) {
this.consumers.add(v -> { this.consumers.add(v -> {
@ -59,9 +95,13 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
return thisObject(); return thisObject();
} }
public final void validate(T obj) { /**
* 校验属性
* @param propertyValue 属性值
*/
public final void validate(T propertyValue) {
for (Consumer<? super TProperty> consumer : consumers) { 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 // #region - notNull
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否不为空
*
* @return 属性校验器
*/
public TPropertyValidator notNull() { public TPropertyValidator notNull() {
return notNull("The input must not be null."); return notNull("The input must not be null.");
} }
/**
* 添加一条校验属性的规则校验属性是否不为空
*
* @param errMsg 错误信息
* @return 属性校验器
*/
public TPropertyValidator notNull(String errMsg) { 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(); return thisObject();
} }
@ -100,20 +165,45 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// #region - isNull // #region - isNull
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @return 属性校验器
*/
public TPropertyValidator isNull() { public TPropertyValidator isNull() {
return isNull("The input must be null."); return isNull("The input must be null.");
} }
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param errMsg 错误信息
* @return 属性校验器
*/
public TPropertyValidator isNull(String errMsg) { 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(); return thisObject();
} }
@ -125,23 +215,52 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// #region - equalTo // #region - equalTo
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param that 给定值
* @return 属性校验器
*/
public TPropertyValidator equalTo(Object that) { public TPropertyValidator equalTo(Object that) {
return equalTo(that, return equalTo(that,
value -> new IllegalArgumentException(String.format("The input must be equal to '%s'.", 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) { 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( public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Supplier<E> exceptionCreator) { Object that, Supplier<E> e) {
return equalTo(that, convertExceptionCreator(exceptionCreator)); return equalTo(that, convertToExceptionFunction(e));
} }
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator equalTo( public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Function<TProperty, E> exceptionCreator) { Object that, Function<TProperty, E> e) {
withRule(value -> Objects.equals(value, that), exceptionCreator); withRule(value -> Objects.equals(value, that), e);
return thisObject(); return thisObject();
} }
@ -153,46 +272,103 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// #region - must // #region - must
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否满足给定的条件
*
* @param condition 校验规则
* @return 属性校验器
*/
public TPropertyValidator must(Predicate<? super TProperty> condition) { public TPropertyValidator must(Predicate<? super TProperty> condition) {
return must(condition, "The specified condition was not met for the input."); 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) { 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( public <E extends RuntimeException> TPropertyValidator must(
Predicate<? super TProperty> condition, Predicate<? super TProperty> condition,
Supplier<E> exceptionCreator) { Supplier<E> e) {
return must(condition, convertExceptionCreator(exceptionCreator)); return must(condition, convertToExceptionFunction(e));
} }
/**
* 添加一条校验属性的规则校验属性是否满足给定的条件
*
* @param <E> 自定义异常类型
* @param condition 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator must( public <E extends RuntimeException> TPropertyValidator must(
Predicate<? super TProperty> condition, Predicate<? super TProperty> condition,
Function<TProperty, E> exceptionCreator) { Function<TProperty, E> e) {
withRule(condition, exceptionCreator); withRule(condition, e);
return thisObject(); return thisObject();
} }
/**
* 添加多条校验属性的规则校验属性是否满足给定的所有条件
*
* @param conditions 校验规则
* @return 属性校验器
*/
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) { public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) {
return must(conditions, "The specified conditions were not met for the input."); 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) { 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( public <E extends RuntimeException> TPropertyValidator must(
Collection<Predicate<? super TProperty>> conditions, Collection<Predicate<? super TProperty>> conditions, Supplier<E> e) {
Supplier<E> exceptionCreator) { return must(conditions, convertToExceptionFunction(e));
return must(conditions, convertExceptionCreator(exceptionCreator));
} }
/**
* 添加多条校验属性的规则校验属性是否满足给定的所有条件
*
* @param <E> 自定义异常类型
* @param conditions 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator must( public <E extends RuntimeException> TPropertyValidator must(
Collection<Predicate<? super TProperty>> conditions, Collection<Predicate<? super TProperty>> conditions,
Function<TProperty, E> exceptionCreator) { Function<TProperty, E> e) {
for (Predicate<? super TProperty> condition : conditions) { for (Predicate<? super TProperty> condition : conditions) {
withRule(condition, exceptionCreator); withRule(condition, e);
} }
return thisObject(); return thisObject();
} }
@ -201,11 +377,12 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// #endregion - must // #endregion - must
// ================================ // ================================
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) { static <V> Function<V, IllegalArgumentException> convertToExceptionFunction(String errMsg) {
return value -> new IllegalArgumentException(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(); return value -> exceptionSupplier.get();
} }
} }

View File

@ -27,120 +27,226 @@ import java.util.function.Supplier;
import xyz.zhouxy.plusone.validator.function.*; import xyz.zhouxy.plusone.validator.function.*;
/** /**
* BaseValidator
*
* <p>
* 校验器的基类 * 校验器的基类
* </p>
* *
* <p> * <p>
* <b>NOTE: content.</b> * 通过继承 {@code BaseValidator}可以自定义一个针对特定类型的校验器包含对该类型的校验逻辑
* </p> *
* @author ZhouXY * @author ZhouXY
* @since 0.0.1
*/ */
public abstract class BaseValidator<T> implements IValidator<T> { public abstract class BaseValidator<T> implements IValidator<T> {
/**
* 规则集合
*/
private final List<Consumer<? super T>> rules = new ArrayList<>(); 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) { protected final void withRule(final Predicate<? super T> rule, final String errorMessage) {
withRule(rule, () -> new IllegalArgumentException(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( 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 -> { withRule(value -> {
if (!condition.test(value)) { if (!condition.test(value)) {
throw exceptionBuilder.apply(value); throw e.apply(value);
} }
}); });
} }
/**
* 添加一个校验规则
*
* @param rule 校验规则内部包含断言条件如果条件不满足则抛出异常
*/
protected final void withRule(Consumer<? super T> rule) { protected final void withRule(Consumer<? super T> rule) {
this.rules.add(rule); this.rules.add(rule);
} }
/**
* 添加一个属性校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final <R> ObjectPropertyValidator<T, R> ruleFor(Function<T, R> getter) { protected final <R> ObjectPropertyValidator<T, R> ruleFor(Function<T, R> getter) {
ObjectPropertyValidator<T, R> validator = new ObjectPropertyValidator<>(getter); ObjectPropertyValidator<T, R> validator = new ObjectPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; 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); ComparablePropertyValidator<T, R> validator = new ComparablePropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Integer} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final IntPropertyValidator<T> ruleForInt(Function<T, Integer> getter) { protected final IntPropertyValidator<T> ruleForInt(Function<T, Integer> getter) {
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter); IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Integer} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final IntPropertyValidator<T> ruleFor(ToIntegerFunction<T> getter) { protected final IntPropertyValidator<T> ruleFor(ToIntegerFunction<T> getter) {
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter); IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Long} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final LongPropertyValidator<T> ruleForLong(Function<T, Long> getter) { protected final LongPropertyValidator<T> ruleForLong(Function<T, Long> getter) {
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter); LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Long} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final LongPropertyValidator<T> ruleFor(ToLongObjectFunction<T> getter) { protected final LongPropertyValidator<T> ruleFor(ToLongObjectFunction<T> getter) {
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter); LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Double} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final DoublePropertyValidator<T> ruleForDouble(Function<T, Double> getter) { protected final DoublePropertyValidator<T> ruleForDouble(Function<T, Double> getter) {
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter); DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Double} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final DoublePropertyValidator<T> ruleFor(ToDoubleObjectFunction<T> getter) { protected final DoublePropertyValidator<T> ruleFor(ToDoubleObjectFunction<T> getter) {
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter); DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Boolean} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final BoolPropertyValidator<T> ruleForBool(Function<T, Boolean> getter) { protected final BoolPropertyValidator<T> ruleForBool(Function<T, Boolean> getter) {
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter); BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Boolean} 属性的校验器
*
* @param getter 属性获取函数
* @return 属性校验器
*/
protected final BoolPropertyValidator<T> ruleFor(ToBoolObjectFunction<T> getter) { protected final BoolPropertyValidator<T> ruleFor(ToBoolObjectFunction<T> getter) {
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter); BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code String} 属性的校验器
*
* @param getter 获取属性值的函数
* @return 属性校验器
*/
protected final StringPropertyValidator<T> ruleForString(Function<T, String> getter) { protected final StringPropertyValidator<T> ruleForString(Function<T, String> getter) {
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter); StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code String} 属性的校验器
*
* @param getter 获取属性值的函数
* @return 属性校验器
*/
protected final StringPropertyValidator<T> ruleFor(ToStringFunction<T> getter) { protected final StringPropertyValidator<T> ruleFor(ToStringFunction<T> getter) {
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter); StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/**
* 添加一个针对 {@code Collection} 属性的校验器
*
* @param getter 获取属性值的函数
* @return 集合属性校验器
*/
protected final <E> CollectionPropertyValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) { protected final <E> CollectionPropertyValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
CollectionPropertyValidator<T, E> validator = new CollectionPropertyValidator<>(getter); CollectionPropertyValidator<T, E> validator = new CollectionPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
} }
/** {@inheritDoc} */
@Override @Override
public void validate(T obj) { public void validate(T obj) {
this.rules.forEach(rule -> rule.accept(obj)); this.rules.forEach(rule -> rule.accept(obj));

View File

@ -19,6 +19,15 @@ 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;
/**
* 针对 {@code Boolean} 类型的属性校验器
*
* <p>
* 内置了判断 Boolean 值是否为 true false 的校验规则
* </p>
*
* @author ZhouXY
*/
public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> { public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
BoolPropertyValidator(Function<T, Boolean> getter) { BoolPropertyValidator(Function<T, Boolean> getter) {
@ -27,43 +36,89 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
// ====== isTrueValue ====== // ====== isTrueValue ======
/**
* 添加一条判断属性值是否为 {@code true} 的校验规则
*
* @return 属性校验器
*/
public BoolPropertyValidator<T> isTrueValue() { public BoolPropertyValidator<T> isTrueValue() {
return isTrueValue("The input must be true."); return isTrueValue("The input must be true.");
} }
/**
* 添加一条判断属性值是否为 {@code true} 的校验规则
*
* @param errMsg 校验失败的错误信息
* @return 属性校验器
*/
public BoolPropertyValidator<T> isTrueValue(String errMsg) { public BoolPropertyValidator<T> isTrueValue(String errMsg) {
return isTrueValue(convertExceptionCreator(errMsg)); return isTrueValue(convertToExceptionFunction(errMsg));
} }
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue( /**
Supplier<E> exceptionCreator) { * 添加一条判断属性值是否为 {@code true} 的校验规则
return isTrueValue(convertExceptionCreator(exceptionCreator)); *
* @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) { * 添加一条判断属性值是否为 {@code true} 的校验规则
withRule(Boolean.TRUE::equals, exceptionCreator); *
* @param <E> 自定义异常类型
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(Function<Boolean, E> e) {
withRule(Boolean.TRUE::equals, e);
return this; return this;
} }
// ====== isFalseValue ====== // ====== isFalseValue ======
/**
* 添加一条判断属性值是否为 {@code false} 的校验规则
*
* @return 属性校验器
*/
public BoolPropertyValidator<T> isFalseValue() { public BoolPropertyValidator<T> isFalseValue() {
return isFalseValue("The input must be false."); return isFalseValue("The input must be false.");
} }
/**
* 添加一条判断属性值是否为 {@code false} 的校验规则
*
* @param errMsg 错误信息
* @return 属性校验器
*/
public BoolPropertyValidator<T> isFalseValue(String errMsg) { public BoolPropertyValidator<T> isFalseValue(String errMsg) {
return isFalseValue(convertExceptionCreator(errMsg)); return isFalseValue(convertToExceptionFunction(errMsg));
} }
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue( /**
Supplier<E> exceptionCreator) { * 添加一条判断属性值是否为 {@code false} 的校验规则
return isFalseValue(convertExceptionCreator(exceptionCreator)); *
* @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) { * 添加一条判断属性值是否为 {@code false} 的校验规则
withRule(Boolean.FALSE::equals, exceptionCreator); *
* @param <E> 异常类型
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(Function<Boolean, E> e) {
withRule(Boolean.FALSE::equals, e);
return this; return this;
} }

View File

@ -22,6 +22,14 @@ import java.util.function.Supplier;
import xyz.zhouxy.plusone.commons.collection.CollectionTools; import xyz.zhouxy.plusone.commons.collection.CollectionTools;
/**
* 针对集合类型的属性校验器
*
* <p>
* 内置判断集合是否为空的校验规则
*
* @author ZhouXY
*/
public class CollectionPropertyValidator<T, TElement> public class CollectionPropertyValidator<T, TElement>
extends BasePropertyValidator<T, Collection<TElement>, CollectionPropertyValidator<T, TElement>> { extends BasePropertyValidator<T, Collection<TElement>, CollectionPropertyValidator<T, TElement>> {
@ -31,43 +39,91 @@ public class CollectionPropertyValidator<T, TElement>
// ====== notEmpty ===== // ====== notEmpty =====
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> notEmpty() { public CollectionPropertyValidator<T, TElement> notEmpty() {
return notEmpty("The input must not be empty."); return notEmpty("The input must not be empty.");
} }
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param errMsg 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> notEmpty(String errMsg) { 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( public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
Supplier<E> exceptionCreator) { Supplier<E> e) {
return notEmpty(convertExceptionCreator(exceptionCreator)); return notEmpty(convertToExceptionFunction(e));
} }
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty( public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
Function<Collection<TElement>, E> exceptionCreator) { Function<Collection<TElement>, E> e) {
withRule(CollectionTools::isNotEmpty, exceptionCreator); withRule(CollectionTools::isNotEmpty, e);
return this; return this;
} }
// ====== isEmpty ===== // ====== isEmpty =====
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> isEmpty() { public CollectionPropertyValidator<T, TElement> isEmpty() {
return isEmpty("The input must be empty."); return isEmpty("The input must be empty.");
} }
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param errMsg 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> isEmpty(String errMsg) { 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( public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
Supplier<E> exceptionCreator) { Supplier<E> e) {
return isEmpty(convertExceptionCreator(exceptionCreator)); return isEmpty(convertToExceptionFunction(e));
} }
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty( public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
Function<Collection<TElement>, E> exceptionCreator) { Function<Collection<TElement>, E> e) {
withRule(CollectionTools::isEmpty, exceptionCreator); withRule(CollectionTools::isEmpty, e);
return this; return this;
} }

View File

@ -18,6 +18,18 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function; 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>> public class ComparablePropertyValidator<T, TProperty extends Comparable<TProperty>>
extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> { extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> {

View File

@ -19,6 +19,14 @@ 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;
/**
* 浮点数属性校验器
*
* <p>
* 内置对 {@code Double} 类型常用的校验规则
*
* @author ZhouXY
*/
public class DoublePropertyValidator<T> public class DoublePropertyValidator<T>
extends BaseComparablePropertyValidator<T, Double, DoublePropertyValidator<T>> { extends BaseComparablePropertyValidator<T, Double, DoublePropertyValidator<T>> {
@ -30,23 +38,50 @@ public class DoublePropertyValidator<T>
// #region - greater than // #region - greater than
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @return 属性校验器
*/
public DoublePropertyValidator<T> gt(double min) { public DoublePropertyValidator<T> gt(double min) {
return gt(min, () -> new IllegalArgumentException( return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%s'.", min))); String.format("The input must be greater than '%s'.", min)));
} }
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @return 属性校验器
*/
public DoublePropertyValidator<T> gt(double min, String errMsg) { 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; return this;
} }
@ -58,23 +93,50 @@ public class DoublePropertyValidator<T>
// #region - greater than or equal to // #region - greater than or equal to
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param min 最小值
* @return 属性校验器
*/
public DoublePropertyValidator<T> ge(double min) { public DoublePropertyValidator<T> ge(double min) {
return ge(min, () -> new IllegalArgumentException( return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%s'.", min))); 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) { 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; return this;
} }
@ -86,23 +148,50 @@ public class DoublePropertyValidator<T>
// #region - less than // #region - less than
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @return 属性校验器
*/
public DoublePropertyValidator<T> lt(double max) { public DoublePropertyValidator<T> lt(double max) {
return lt(max, () -> new IllegalArgumentException( return lt(max, () -> new IllegalArgumentException(
String.format("The input must be less than '%s'.", max))); String.format("The input must be less than '%s'.", max)));
} }
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @return 属性校验器
*/
public DoublePropertyValidator<T> lt(double max, String errMsg) { 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; return this;
} }
@ -114,23 +203,50 @@ public class DoublePropertyValidator<T>
// #region - less than or equal to // #region - less than or equal to
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param max 最大值
* @return 属性校验器
*/
public DoublePropertyValidator<T> le(double max) { public DoublePropertyValidator<T> le(double max) {
return le(max, () -> new IllegalArgumentException( return le(max, () -> new IllegalArgumentException(
String.format("The input must be less than or equal to '%s'.", max))); 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) { 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; return this;
} }

View File

@ -22,5 +22,10 @@ package xyz.zhouxy.plusone.validator;
*/ */
public interface IValidator<T> { public interface IValidator<T> {
/**
* 校验对象
*
* @param obj
*/
void validate(T obj); void validate(T obj);
} }

View File

@ -19,6 +19,14 @@ 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;
/**
* 整数属性校验器
*
* <p>
* 内置对 {@code Integer} 类型常用的校验规则
*
* @author ZhouXY
*/
public class IntPropertyValidator<T> public class IntPropertyValidator<T>
extends BaseComparablePropertyValidator<T, Integer, IntPropertyValidator<T>> { extends BaseComparablePropertyValidator<T, Integer, IntPropertyValidator<T>> {
@ -30,23 +38,50 @@ public class IntPropertyValidator<T>
// #region - greater than // #region - greater than
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @return 属性校验器
*/
public IntPropertyValidator<T> gt(int min) { public IntPropertyValidator<T> gt(int min) {
return gt(min, () -> new IllegalArgumentException( return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%d'.", min))); String.format("The input must be greater than '%d'.", min)));
} }
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @return 属性校验器
*/
public IntPropertyValidator<T> gt(int min, String errMsg) { 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; return this;
} }
@ -58,23 +93,50 @@ public class IntPropertyValidator<T>
// #region - greater than or equal to // #region - greater than or equal to
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param min 最小值
* @return 属性校验器
*/
public IntPropertyValidator<T> ge(int min) { public IntPropertyValidator<T> ge(int min) {
return ge(min, () -> new IllegalArgumentException( return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%d'.", min))); 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) { 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; return this;
} }
@ -86,23 +148,50 @@ public class IntPropertyValidator<T>
// #region - less than // #region - less than
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @return 属性校验器
*/
public IntPropertyValidator<T> lt(int max) { public IntPropertyValidator<T> lt(int max) {
return lt(max, () -> new IllegalArgumentException( return lt(max, () -> new IllegalArgumentException(
String.format("The input must be less than '%d'.", max))); String.format("The input must be less than '%d'.", max)));
} }
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @return 属性校验器
*/
public IntPropertyValidator<T> lt(int max, String errMsg) { 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; return this;
} }
@ -114,23 +203,50 @@ public class IntPropertyValidator<T>
// #region - less than or equal to // #region - less than or equal to
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param max 最大值
* @return 属性校验器
*/
public IntPropertyValidator<T> le(int max) { public IntPropertyValidator<T> le(int max) {
return le(max, () -> new IllegalArgumentException( return le(max, () -> new IllegalArgumentException(
String.format("The input must be less than or equal to '%d'.", max))); 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) { 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; return this;
} }

View File

@ -19,6 +19,14 @@ 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;
/**
* 长整数属性校验器
*
* <p>
* 内置对 {@code Long} 类型常用的校验规则
*
* @author ZhouXY
*/
public class LongPropertyValidator<T> public class LongPropertyValidator<T>
extends BaseComparablePropertyValidator<T, Long, LongPropertyValidator<T>> { extends BaseComparablePropertyValidator<T, Long, LongPropertyValidator<T>> {
@ -30,23 +38,50 @@ public class LongPropertyValidator<T>
// #region - greater than // #region - greater than
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @return 属性校验器
*/
public LongPropertyValidator<T> gt(long min) { public LongPropertyValidator<T> gt(long min) {
return gt(min, () -> new IllegalArgumentException( return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%d'.", min))); String.format("The input must be greater than '%d'.", min)));
} }
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @return 属性校验器
*/
public LongPropertyValidator<T> gt(long min, String errMsg) { 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; return this;
} }
@ -58,23 +93,50 @@ public class LongPropertyValidator<T>
// #region - greater than or equal to // #region - greater than or equal to
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param min 最小值
* @return 属性校验器
*/
public LongPropertyValidator<T> ge(long min) { public LongPropertyValidator<T> ge(long min) {
return ge(min, () -> new IllegalArgumentException( return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%d'.", min))); 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) { 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; return this;
} }
@ -86,23 +148,50 @@ public class LongPropertyValidator<T>
// #region - less than // #region - less than
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @return 属性校验器
*/
public LongPropertyValidator<T> lt(long max) { public LongPropertyValidator<T> lt(long max) {
return lt(max, () -> new IllegalArgumentException( return lt(max, () -> new IllegalArgumentException(
String.format("The input must be less than '%d'.", max))); String.format("The input must be less than '%d'.", max)));
} }
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @return 属性校验器
*/
public LongPropertyValidator<T> lt(long max, String errMsg) { 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; return this;
} }
@ -114,23 +203,50 @@ public class LongPropertyValidator<T>
// #region - less than or equal to // #region - less than or equal to
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param max 最大值
* @return 属性校验器
*/
public LongPropertyValidator<T> le(long max) { public LongPropertyValidator<T> le(long max) {
return le(max, () -> new IllegalArgumentException( return le(max, () -> new IllegalArgumentException(
String.format("The input must be less than or equal to '%d'.", max))); 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) { 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; return this;
} }

View File

@ -23,28 +23,67 @@ import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* Map 进行校验的校验器
*
* <p>
* 校验后拷贝出一个新的 Map 对象仅保留指定的 key
*
* @author ZhouXY
*/
public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> { public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
private final Set<K> keys; private final Set<K> keys;
/**
* 构造函数
*
* @param keys 要保留的 key 的集合
*/
protected MapValidator(K[] keys) { protected MapValidator(K[] keys) {
this(Arrays.asList(keys)); this(Arrays.asList(keys));
} }
/**
* 构造函数
* @param keys 要保留的 key 的集合
*/
protected MapValidator(Collection<K> keys) { protected MapValidator(Collection<K> keys) {
this.keys = keys.stream().collect(Collectors.toSet()); 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) { public final Map<K, V> validateAndCopy(Map<K, V> obj) {
return validateAndCopyInternal(obj, this.keys); 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) { public final Map<K, V> validateAndCopy(Map<K, V> obj, Collection<K> keys) {
return validateAndCopyInternal(obj, keys); return validateAndCopyInternal(obj, keys);
} }
/**
* 校验并拷贝仅保留指定 key 的属性
*
* @param obj 待校验的 map
* @param keys 待校验的 key
* @return 拷贝后的 map
*/
@SafeVarargs @SafeVarargs
public final Map<K, V> validateAndCopy(Map<K, V> obj, K... keys) { public final Map<K, V> validateAndCopy(Map<K, V> obj, K... keys) {
return validateAndCopyInternal(obj, Arrays.asList(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)); .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) { protected final ObjectPropertyValidator<Map<K, V>, V> ruleFor(K key) {
final Function<Map<K, V>, V> func = m -> m.get(key); final Function<Map<K, V>, V> func = m -> m.get(key);
return ruleFor(func); return ruleFor(func);
} }
/**
* 添加一个属性校验器对指定 key 对应的 {@code Integer} 类型的 value 进行校验
*
* @param key key
* @return 属性校验器
*/
protected final IntPropertyValidator<Map<K, V>> ruleForInt(K key) { protected final IntPropertyValidator<Map<K, V>> ruleForInt(K key) {
return ruleForInt(m -> (Integer) m.get(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) { protected final LongPropertyValidator<Map<K, V>> ruleForLong(K key) {
return ruleForLong(m -> (Long) m.get(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) { protected final DoublePropertyValidator<Map<K, V>> ruleForDouble(K key) {
return ruleForDouble(m -> (Double) m.get(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) { protected final BoolPropertyValidator<Map<K, V>> ruleForBool(K key) {
return ruleForBool(m -> (Boolean) m.get(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) { protected final StringPropertyValidator<Map<K, V>> ruleForString(K key) {
return ruleForString(m -> (String) m.get(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) { protected final <E extends Comparable<E>> ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Function<Map<K, V>, E> getter = m -> (E) m.get(key); Function<Map<K, V>, E> getter = m -> (E) m.get(key);
return ruleForComparable(getter); return ruleForComparable(getter);
} }
/**
* 添加一个属性校验器对指定 key 对应的集合类型的 value 进行校验
*
* @param <E> 集合元素的类型
* @param key key
* @return 属性校验器
*/
protected final <E> CollectionPropertyValidator<Map<K, V>, E> ruleForCollection(K key) { protected final <E> CollectionPropertyValidator<Map<K, V>, E> ruleForCollection(K key) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Function<Map<K, V>, Collection<E>> getter = m -> (Collection<E>) m.get(key); Function<Map<K, V>, Collection<E>> getter = m -> (Collection<E>) m.get(key);
return ruleForCollection(getter); return ruleForCollection(getter);
} }
// ================================
// #endregion - ruleFor
// ================================
} }

View File

@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function; import java.util.function.Function;
/**
* 通用类型属性校验器继承自 {@link BasePropertyValidator}包含针对属性的校验规则
*
* @author ZhouXY
*/
public class ObjectPropertyValidator<T, TProperty> public class ObjectPropertyValidator<T, TProperty>
extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> { extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> {

View File

@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* StringPropertyValidator * StringPropertyValidator
* *
* <p> * <p>
* 针对文本字段的 * 针对文本字段的验器
* </p> * </p>
* *
* @author ZhouXY * @author ZhouXY
@ -46,20 +46,41 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - matches // #region - matches
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否匹配正则表达式
*
* @param regex 正则表达式
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matches(Pattern regex, String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex, Pattern regex, Supplier<E> e) {
Supplier<E> exceptionCreator) { return matches(regex, convertToExceptionFunction(e));
return matches(regex, convertExceptionCreator(exceptionCreator));
} }
/**
* 添加一条校验属性的规则校验属性是否匹配正则表达式
*
* @param <E> 异常类型
* @param regex 正则表达式
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matches( public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex, Pattern regex, Function<String, E> e) {
Function<String, E> exceptionCreator) { withRule(input -> (input == null || RegexTools.matches(input, regex)), e);
withRule(input -> (input == null || RegexTools.matches(input, regex)), exceptionCreator);
return this; return this;
} }
@ -71,37 +92,79 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - matchesOne // #region - matchesOne
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesOne(Pattern[] regexs, String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs, Pattern[] regexs, Supplier<E> e) {
Supplier<E> exceptionCreator) { return matchesOne(regexs, convertToExceptionFunction(e));
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
} }
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @return
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne( public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs, Pattern[] regexs, Function<String, E> e) {
Function<String, E> exceptionCreator) { withRule(input -> input == null || RegexTools.matchesOne(input, regexs), e);
withRule(input -> input == null || RegexTools.matchesOne(input, regexs), exceptionCreator);
return this; return this;
} }
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesOne(List<Pattern> regexs, String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs, List<Pattern> regexs, Supplier<E> e) {
Supplier<E> exceptionCreator) { return matchesOne(regexs, convertToExceptionFunction(e));
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
} }
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne( public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs, List<Pattern> regexs, Function<String, E> e) {
Function<String, E> exceptionCreator) { withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), e);
withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this; return this;
} }
@ -113,37 +176,79 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - matchesAll // #region - matchesAll
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesAll(Pattern[] regexs, String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs, Pattern[] regexs, Supplier<E> e) {
Supplier<E> exceptionCreator) { return matchesAll(regexs, convertToExceptionFunction(e));
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
} }
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll( public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs, Pattern[] regexs, Function<String, E> e) {
Function<String, E> exceptionCreator) { withRule(input -> input == null || RegexTools.matchesAll(input, regexs), e);
withRule(input -> input == null || RegexTools.matchesAll(input, regexs), exceptionCreator);
return this; return this;
} }
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesAll(Collection<Pattern> regexs, String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs, Collection<Pattern> regexs, Supplier<E> e) {
Supplier<E> exceptionCreator) { return matchesAll(regexs, convertToExceptionFunction(e));
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
} }
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll( public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs, Collection<Pattern> regexs, Function<String, E> e) {
Function<String, E> exceptionCreator) { withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), e);
withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this; return this;
} }
@ -155,21 +260,45 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - notBlank // #region - notBlank
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否不为空白字符串
*
* @return 属性校验器
*/
public StringPropertyValidator<T> notBlank() { public StringPropertyValidator<T> notBlank() {
return notBlank("The input must not be blank."); return notBlank("The input must not be blank.");
} }
/**
* 添加一条校验属性的规则校验属性是否不为空白字符串
*
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> notBlank(String errMsg) { 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; return this;
} }
@ -181,22 +310,46 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - emailAddress // #region - emailAddress
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性是否是邮箱地址
*
* @return 属性校验器
*/
public StringPropertyValidator<T> emailAddress() { public StringPropertyValidator<T> emailAddress() {
return emailAddress("The input is not a valid email address."); return emailAddress("The input is not a valid email address.");
} }
/**
* 添加一条校验属性的规则校验属性是否是邮箱地址
*
* @param errMsg 校验失败的错误信息
* @return 属性校验器
*/
public StringPropertyValidator<T> emailAddress(String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(
Function<String, E> exceptionCreator) { Function<String, E> e) {
return matches(PatternConsts.EMAIL, exceptionCreator); return matches(PatternConsts.EMAIL, e);
} }
// ================================ // ================================
@ -207,21 +360,46 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - notEmpty // #region - notEmpty
// ================================ // ================================
/**
* 添加一条校验属性的规则校验字符串属性是否不为空
*
* @return 属性校验器
*/
public StringPropertyValidator<T> notEmpty() { public StringPropertyValidator<T> notEmpty() {
return notEmpty("The input must not be empty."); return notEmpty("The input must not be empty.");
} }
/**
* 添加一条校验属性的规则校验字符串属性是否不为空
*
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> notEmpty(String errMsg) { 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( public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(
Function<String, E> exceptionCreator) { Function<String, E> e) {
withRule(s -> s != null && !s.isEmpty(), exceptionCreator); withRule(s -> s != null && !s.isEmpty(), e);
return this; return this;
} }
@ -233,20 +411,41 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// #region - length // #region - length
// ================================ // ================================
/**
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param length 指定长度
* @param errMsg 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> length(int length, String errMsg) { 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, AssertTools.checkArgument(length >= 0,
"The expected length must be greater than or equal to 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; return this;
} }
@ -258,20 +457,44 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
return len >= min && len <= max; return len >= min && len <= max;
} }
/**
* 添加一条校验属性的规则校验属性的长度范围
*
* @param min 最小长度
* @param max 最大长度
* @param errMsg 错误信息
* @return 属性校验器
*/
public StringPropertyValidator<T> length(int min, int max, String errMsg) { 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, public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
Supplier<E> exceptionCreator) { Supplier<E> e) {
return length(min, max, convertExceptionCreator(exceptionCreator)); return length(min, max, convertToExceptionFunction(e));
} }
/**
* 添加一条校验属性的规则校验属性的长度范围
*
* @param min 最小长度
* @param max 最大长度
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max, 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 >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max."); 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; return this;
} }

View File

@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Function; import java.util.function.Function;
/**
* Function&lt;T, Boolean&gt;
*
* @author ZhouXY
*/
@FunctionalInterface @FunctionalInterface
public interface ToBoolObjectFunction<T> extends Function<T, Boolean>, Serializable { public interface ToBoolObjectFunction<T> extends Function<T, Boolean>, Serializable {
} }

View File

@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Function; import java.util.function.Function;
/**
* Function&lt;T, Double&gt;
*
* @author ZhouXY
*/
@FunctionalInterface @FunctionalInterface
public interface ToDoubleObjectFunction<T> extends Function<T, Double>, Serializable { public interface ToDoubleObjectFunction<T> extends Function<T, Double>, Serializable {
} }

View File

@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Function; import java.util.function.Function;
/**
* Function&lt;T, Integer&gt;
*
* @author ZhouXY
*/
@FunctionalInterface @FunctionalInterface
public interface ToIntegerFunction<T> extends Function<T, Integer>, Serializable { public interface ToIntegerFunction<T> extends Function<T, Integer>, Serializable {
} }

View File

@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Function; import java.util.function.Function;
/**
* Function&lt;T, Long&gt;
*
* @author ZhouXY
*/
@FunctionalInterface @FunctionalInterface
public interface ToLongObjectFunction<T> extends Function<T, Long>, Serializable { public interface ToLongObjectFunction<T> extends Function<T, Long>, Serializable {
} }

View File

@ -18,6 +18,11 @@ package xyz.zhouxy.plusone.validator.function;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Function; import java.util.function.Function;
/**
* Function&lt;T, String&gt;
*
* @author ZhouXY
*/
@FunctionalInterface @FunctionalInterface
public interface ToStringFunction<T> extends Function<T, String>, Serializable { public interface ToStringFunction<T> extends Function<T, String>, Serializable {
} }