refactor: 重构属性校验器

This commit is contained in:
zhouxy108 2025-06-04 01:30:51 +08:00
parent 7f4f5748f8
commit e79d8e9ec8
18 changed files with 1110 additions and 1790 deletions

View File

@ -31,10 +31,10 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
*
* @author ZhouXY
*/
public class ArrayPropertyValidator<T, TElement>
extends BasePropertyValidator<T, TElement[], ArrayPropertyValidator<T, TElement>> {
public class ArrayPropertyValidator<T, E>
extends BasePropertyValidator<T, E[], ArrayPropertyValidator<T, E>> {
ArrayPropertyValidator(Function<T, TElement[]> getter) {
ArrayPropertyValidator(Function<T, E[]> getter) {
super(getter);
}
@ -47,42 +47,43 @@ public class ArrayPropertyValidator<T, TElement>
*
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> notEmpty() {
return notEmpty("The input must not be empty.");
public final ArrayPropertyValidator<T, E> notEmpty() {
return withRule(Conditions.notEmpty(), "The input must not be empty.");
}
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> notEmpty(String errMsg) {
return notEmpty(convertToExceptionFunction(errMsg));
public final ArrayPropertyValidator<T, E> notEmpty(
final String errorMessage) {
return withRule(Conditions.notEmpty(), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> notEmpty(
Supplier<E> e) {
return notEmpty(convertToExceptionFunction(e));
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> notEmpty(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.notEmpty(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> notEmpty(
Function<TElement[], E> e) {
return withRule(ArrayTools::isNotEmpty, e);
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> notEmpty(
final Function<E[], X> exceptionFunction) {
return withRule(Conditions.notEmpty(), exceptionFunction);
}
// ================================
@ -98,40 +99,41 @@ public class ArrayPropertyValidator<T, TElement>
*
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> isEmpty() {
return isEmpty("The input must be empty.");
public final ArrayPropertyValidator<T, E> isEmpty() {
return withRule(Conditions.isEmpty(), "The input must be empty.");
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> isEmpty(String errMsg) {
return isEmpty(convertToExceptionFunction(errMsg));
public final ArrayPropertyValidator<T, E> isEmpty(
final String errorMessage) {
return withRule(Conditions.isEmpty(), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> isEmpty(
Supplier<E> e) {
return isEmpty(convertToExceptionFunction(e));
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> isEmpty(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.isEmpty(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> isEmpty(
Function<TElement[], E> e) {
return withRule(ArrayTools::isEmpty, e);
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> isEmpty(
final Function<E[], X> exceptionFunction) {
return withRule(Conditions.isEmpty(), exceptionFunction);
}
// ================================
@ -148,46 +150,65 @@ public class ArrayPropertyValidator<T, TElement>
* @param condition 校验规则
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> allMatch(Predicate<TElement> condition) {
return allMatch(condition, convertToExceptionFunction("All elements must match the condition."));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param errMsg 异常信息
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> allMatch(Predicate<TElement> condition, String errMsg) {
return allMatch(condition, convertToExceptionFunction(errMsg));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> allMatch(
Predicate<TElement> condition, Supplier<E> e) {
return allMatch(condition, convertToExceptionFunction(e));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> allMatch(
Predicate<TElement> condition, Function<TElement, E> e) {
public final ArrayPropertyValidator<T, E> allMatch(final Predicate<E> condition) {
return withRule(c -> {
for (TElement element : c) {
for (E element : c) {
if (!condition.test(element)) {
throw e.apply(element);
throw ValidationException.withMessage("All elements must match the condition.");
}
}
});
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param errorMessage 异常信息
* @return 属性校验器
*/
public final ArrayPropertyValidator<T, E> allMatch(
final Predicate<E> condition, final String errorMessage) {
return withRule(c -> {
for (E element : c) {
if (!condition.test(element)) {
throw ValidationException.withMessage(errorMessage);
}
}
});
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> allMatch(
final Predicate<E> condition, final Supplier<X> exceptionSupplier) {
return withRule(c -> {
for (E element : c) {
if (!condition.test(element)) {
throw exceptionSupplier.get();
}
}
});
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> allMatch(
final Predicate<E> condition, final Function<E, X> exceptionFunction) {
return withRule(c -> {
for (E element : c) {
if (!condition.test(element)) {
throw exceptionFunction.apply(element);
}
}
});
@ -205,47 +226,38 @@ public class ArrayPropertyValidator<T, TElement>
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param length 指定长度
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> length(int length, String errMsg) {
return length(length, convertToExceptionFunction(errMsg));
public final ArrayPropertyValidator<T, E> length(
final int length, final String errorMessage) {
return withRule(Conditions.length(length), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param length 指定长度
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> length(
int length, Supplier<E> e) {
return length(length, convertToExceptionFunction(e));
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
final int length, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.length(length), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param length 指定长度
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> length(
int length, Function<TElement[], E> e) {
AssertTools.checkArgument(length >= 0,
"The expected length must be greater than or equal to 0.");
return withRule(s -> s == null || s.length == length, e);
}
static <TElement> boolean checkLength(TElement[] str, int min, int max) {
if (str == null) {
return true;
}
final int len = str.length;
return len >= min && len <= max;
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
final int length, final Function<E[], X> exceptionFunction) {
return withRule(Conditions.length(length), exceptionFunction);
}
/**
@ -253,11 +265,12 @@ public class ArrayPropertyValidator<T, TElement>
*
* @param min 最小长度
* @param max 最大长度
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public ArrayPropertyValidator<T, TElement> length(int min, int max, String errMsg) {
return length(min, max, convertToExceptionFunction(errMsg));
public final ArrayPropertyValidator<T, E> length(
final int min, final int max, final String errorMessage) {
return withRule(Conditions.length(min, max), errorMessage);
}
/**
@ -265,12 +278,12 @@ public class ArrayPropertyValidator<T, TElement>
*
* @param min 最小长度
* @param max 最大长度
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> length(
int min, int max, Supplier<E> e) {
return length(min, max, convertToExceptionFunction(e));
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
final int min, final int max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.length(min, max), exceptionSupplier);
}
/**
@ -278,22 +291,49 @@ public class ArrayPropertyValidator<T, TElement>
*
* @param min 最小长度
* @param max 最大长度
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> ArrayPropertyValidator<T, TElement> length(
int min, int max, Function<TElement[], E> e) {
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
return withRule(s -> checkLength(s, min, max), e);
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
final int min, final int max, final Function<E[], X> exceptionFunction) {
return withRule(Conditions.length(min, max), exceptionFunction);
}
// ================================
// #endregion - length
// ================================
private static class Conditions {
private static <T> Predicate<T[]> isEmpty() {
return ArrayTools::isEmpty;
}
private static <T> Predicate<T[]> notEmpty() {
return ArrayTools::isNotEmpty;
}
private static <T> Predicate<T[]> length(final int length) {
AssertTools.checkArgument(length >= 0,
"The expected length must be non-negative.");
return input -> input == null || input.length == length;
}
private static <T> Predicate<T[]> length(final int min, final int max) {
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
return input -> {
if (input == null) {
return true;
}
final int len = input.length;
return len >= min && len <= max;
};
}
}
@Override
protected ArrayPropertyValidator<T, TElement> thisObject() {
protected ArrayPropertyValidator<T, E> thisObject() {
return this;
}
}

View File

@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import com.google.common.collect.Range;
@ -33,7 +34,10 @@ import com.google.common.collect.Range;
* @see Range
* @author ZhouXY
*/
public abstract class BaseComparablePropertyValidator<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> {
BaseComparablePropertyValidator(Function<T, ? extends TProperty> getter) {
@ -46,47 +50,51 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
* @param range 区间
* @return 属性校验器
*/
public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value), value -> ValidationException.withMessage(
public final TPropertyValidator inRange(final Range<TProperty> range) {
return withRule(Conditions.inRange(range), value -> ValidationException.withMessage(
"The input must in the interval %s. You entered %s.", range, value));
return thisObject();
}
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public TPropertyValidator inRange(Range<TProperty> range, String errMsg) {
withRule(value -> value != null && range.contains(value), convertToExceptionFunction(errMsg));
return thisObject();
public final TPropertyValidator inRange(
final Range<TProperty> range, final String errorMessage) {
return withRule(Conditions.inRange(range), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range, Supplier<E> e) {
withRule(value -> value != null && range.contains(value), convertToExceptionFunction(e));
return thisObject();
public final <X extends RuntimeException> TPropertyValidator inRange(
final Range<TProperty> range, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.inRange(range), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否在给定的区间之内
*
* @param range 区间
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range, Function<TProperty, E> e) {
withRule(value -> value != null && range.contains(value), e);
return thisObject();
public final <X extends RuntimeException> TPropertyValidator inRange(
final Range<TProperty> range, final Function<TProperty, X> exceptionFunction) {
return withRule(Conditions.inRange(range), exceptionFunction);
}
private static class Conditions {
private static <TProperty extends Comparable<TProperty>> Predicate<TProperty> inRange(
final Range<TProperty> range) {
return value -> value == null || range.contains(value);
}
}
}

View File

@ -16,7 +16,6 @@
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@ -34,7 +33,10 @@ import java.util.function.Supplier;
*
* @author ZhouXY
*/
public abstract class BasePropertyValidator<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;
@ -47,15 +49,68 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
/**
* 添加一条校验属性的规则
*
* @param rule 校验规则
* @param e 自定义异常
* @param condition 校验条件
* @param errorMessage 异常信息
* @return 属性校验器
*/
protected final <E extends RuntimeException> TPropertyValidator withRule(
Predicate<? super TProperty> rule, Function<TProperty, E> e) {
return withRule(v -> {
if (!rule.test(v)) {
throw e.apply(v);
protected final TPropertyValidator withRule(
final Predicate<? super TProperty> condition,
final String errorMessage) {
return withRule(input -> {
if (!condition.test(input)) {
throw ValidationException.withMessage(errorMessage);
}
});
}
/**
* 添加一条校验属性的规则
*
* @param condition 校验条件
* @param errorMessageTemplate 错误信息模版
* @param errorMessageArgs 错误信息参数
* @return 属性校验器
*/
protected final TPropertyValidator withRule(
final Predicate<? super TProperty> condition,
final String errorMessageTemplate, Object... errorMessageArgs) {
return withRule(input -> {
if (!condition.test(input)) {
throw ValidationException.withMessage(errorMessageTemplate, errorMessageArgs);
}
});
}
/**
* 添加一条校验属性的规则
*
* @param condition 校验条件
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
protected final <X extends RuntimeException> TPropertyValidator withRule(
final Predicate<? super TProperty> condition,
final Supplier<X> exceptionSupplier) {
return withRule(input -> {
if (!condition.test(input)) {
throw exceptionSupplier.get();
}
});
}
/**
* 添加一条校验属性的规则
*
* @param condition 校验条件
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
protected final <X extends RuntimeException> TPropertyValidator withRule(
final Predicate<? super TProperty> condition,
final Function<? super TProperty, X> exceptionFunction) {
return withRule(input -> {
if (!condition.test(input)) {
throw exceptionFunction.apply(input);
}
});
}
@ -64,7 +119,6 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
* 添加一条校验属性的规则
*
* @param rule 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
protected final TPropertyValidator withRule(Consumer<? super TProperty> rule) {
@ -74,11 +128,12 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
/**
* 校验属性
* @param propertyValue 属性值
*
* @param obj 属性所在的对象
*/
public final void validate(T propertyValue) {
public final void validate(T obj) {
for (Consumer<? super TProperty> consumer : consumers) {
consumer.accept(getter.apply(propertyValue));
consumer.accept(getter.apply(obj));
}
}
@ -97,41 +152,42 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
*
* @return 属性校验器
*/
public TPropertyValidator notNull() {
return notNull("The input must not be null.");
public final TPropertyValidator notNull() {
return withRule(Objects::nonNull, "The input must not be null.");
}
/**
* 添加一条校验属性的规则校验属性是否不为空
*
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public TPropertyValidator notNull(String errMsg) {
return notNull(convertToExceptionFunction(errMsg));
public final TPropertyValidator notNull(final String errorMessage) {
return withRule(Objects::nonNull, errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否不为空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator notNull(Supplier<E> e) {
return notNull(convertToExceptionFunction(e));
public final <X extends RuntimeException> TPropertyValidator notNull(
final Supplier<X> exceptionSupplier) {
return withRule(Objects::nonNull, exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否不为空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator notNull(Function<TProperty, E> e) {
withRule(Objects::nonNull, e);
return thisObject();
public final <X extends RuntimeException> TPropertyValidator notNull(
final Function<TProperty, X> exceptionFunction) {
return withRule(Objects::nonNull, exceptionFunction);
}
// ================================
@ -147,41 +203,42 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
*
* @return 属性校验器
*/
public TPropertyValidator isNull() {
return isNull("The input must be null.");
public final TPropertyValidator isNull() {
return withRule(Objects::isNull, "The input must be null.");
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public TPropertyValidator isNull(String errMsg) {
return isNull(convertToExceptionFunction(errMsg));
public final TPropertyValidator isNull(final String errorMessage) {
return withRule(Objects::isNull, errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator isNull(Supplier<E> e) {
return isNull(convertToExceptionFunction(e));
public final <X extends RuntimeException> TPropertyValidator isNull(
final Supplier<X> exceptionSupplier) {
return withRule(Objects::isNull, exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator isNull(Function<TProperty, E> e) {
withRule(Objects::isNull, e);
return thisObject();
public final <X extends RuntimeException> TPropertyValidator isNull(
final Function<TProperty, X> exceptionFunction) {
return withRule(Objects::isNull, exceptionFunction);
}
// ================================
@ -189,7 +246,7 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// ================================
// ================================
// #region - equalTo
// #region - equal
// ================================
/**
@ -198,51 +255,51 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
* @param that 给定值
* @return 属性校验器
*/
public TPropertyValidator equalTo(Object that) {
return equalTo(that, value -> ValidationException
.withMessage("The input must be equal to '%s'.", that));
public final TPropertyValidator equal(Object that) {
return withRule(Conditions.equal(that),
"The input must be equal to '%s'.", that);
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param that 给定值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public TPropertyValidator equalTo(Object that, String errMsg) {
return equalTo(that, convertToExceptionFunction(errMsg));
public final TPropertyValidator equal(
final Object that, final String errorMessage) {
return withRule(Conditions.equal(that), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param <X> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Supplier<E> e) {
return equalTo(that, convertToExceptionFunction(e));
public final <X extends RuntimeException> TPropertyValidator equal(
final Object that, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.equal(that), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param <X> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Function<TProperty, E> e) {
withRule(value -> value == null || value.equals(that), e);
return thisObject();
public final <X extends RuntimeException> TPropertyValidator equal(
final Object that, final Function<TProperty, X> exceptionFunction) {
return withRule(Conditions.equal(that), exceptionFunction);
}
// ================================
// #endregion - equalTo
// #endregion - equal
// ================================
// ================================
@ -255,47 +312,46 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
* @param that 给定值
* @return 属性校验器
*/
public TPropertyValidator notEqual(Object that) {
return notEqual(that, value -> ValidationException
.withMessage("The input must not equal '%s'.", that));
public final TPropertyValidator notEqual(final Object that) {
return withRule(Conditions.notEqual(that),
"The input must not equal '%s'.", that);
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param that 给定值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public TPropertyValidator notEqual(Object that, String errMsg) {
return notEqual(that, convertToExceptionFunction(errMsg));
public final TPropertyValidator notEqual(final Object that, final String errorMessage) {
return withRule(Conditions.notEqual(that), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param <X> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator notEqual(
Object that, Supplier<E> e) {
return notEqual(that, convertToExceptionFunction(e));
public final <X extends RuntimeException> TPropertyValidator notEqual(
final Object that, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.notEqual(that), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param <X> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator notEqual(
Object that, Function<TProperty, E> e) {
withRule(value -> value == null || !value.equals(that), e);
return thisObject();
public final <X extends RuntimeException> TPropertyValidator notEqual(
final Object that, final Function<TProperty, X> exceptionFunction) {
return withRule(Conditions.notEqual(that), exceptionFunction);
}
// ================================
@ -309,119 +365,76 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
/**
* 添加一条校验属性的规则校验属性是否满足给定的条件
*
* @param condition 校验规则
* @param condition 校验条件
* @return 属性校验器
*/
public TPropertyValidator must(Predicate<? super TProperty> condition) {
return must(condition, "The specified condition was not met for the input.");
public final TPropertyValidator must(final Predicate<? super TProperty> condition) {
return withRule(condition,
"The specified condition was not met for the input.");
}
/**
* 添加一条校验属性的规则校验属性是否满足给定的条件
*
* @param condition 校验规则
* @param errMsg 错误信息
* @param condition 校验条件
* @param errorMessage 异常信息
* @return 属性校验器
*/
public TPropertyValidator must(Predicate<? super TProperty> condition, String errMsg) {
return must(condition, convertToExceptionFunction(errMsg));
public final TPropertyValidator must(
final Predicate<? super TProperty> condition,
final String errorMessage) {
return withRule(condition, errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否满足给定的条件
*
* @param <E> 自定义异常类型
* @param <X> 自定义异常类型
* @param condition 校验规则
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator must(
Predicate<? super TProperty> condition,
Supplier<E> e) {
return must(condition, convertToExceptionFunction(e));
public final <X extends RuntimeException> TPropertyValidator must(
final Predicate<? super TProperty> condition,
final Supplier<X> exceptionSupplier) {
return withRule(condition, exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否满足给定的条件
*
* @param <E> 自定义异常类型
* @param <X> 自定义异常类型
* @param condition 校验规则
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator must(
Predicate<? super TProperty> condition,
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, convertToExceptionFunction(errMsg));
}
/**
* 添加多条校验属性的规则校验属性是否满足给定的所有条件
*
* @param <E> 自定义异常类型
* @param conditions 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator must(
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> e) {
for (Predicate<? super TProperty> condition : conditions) {
withRule(condition, e);
}
return thisObject();
public final <X extends RuntimeException> TPropertyValidator must(
final Predicate<? super TProperty> condition,
final Function<TProperty, X> exceptionFunction) {
return withRule(condition, exceptionFunction);
}
// ================================
// #endregion - must
// ================================
static <V> Function<V, ValidationException> convertToExceptionFunction(String errMsg) {
return value -> ValidationException.withMessage(errMsg);
// ================================
// #region - conditions
// ================================
private static class Conditions {
private static <TProperty> Predicate<TProperty> equal(Object obj) {
return input -> input == null || input.equals(obj);
}
private static <TProperty> Predicate<TProperty> notEqual(Object obj) {
return input -> input == null || !input.equals(obj);
}
}
static <V> Function<V, ValidationException> convertToExceptionFunction(
String errorMessageTemplate, Object... errorMessageArgs) {
return value -> ValidationException.withMessage(errorMessageTemplate, errorMessageArgs);
}
static <V, E extends RuntimeException> Function<V, E> convertToExceptionFunction(
Supplier<E> exceptionSupplier) {
return value -> exceptionSupplier.get();
}
// ================================
// #endregion - conditions
// ================================
}

View File

@ -45,37 +45,37 @@ public abstract class BaseValidator<T> implements IValidator<T> {
/**
* 添加一个校验规则
*
* @param rule 校验规则
* @param errorMessage 错误信息
*/
protected final void withRule(final Predicate<? super T> rule, final String errorMessage) {
withRule(rule, () -> ValidationException.withMessage(errorMessage));
}
/**
* 添加一个校验规则
*
* @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 自定义异常
* @param errorMessage 异常信息
*/
protected final <E extends RuntimeException> void withRule(
final Predicate<? super T> condition, final Function<T, E> e) {
protected final void withRule(final Predicate<? super T> condition, final String errorMessage) {
withRule(condition, () -> ValidationException.withMessage(errorMessage));
}
/**
* 添加一个校验规则
*
* @param <X> 自定义异常类型
* @param condition 校验条件
* @param exceptionSupplier 自定义异常
*/
protected final <X extends RuntimeException> void withRule(
final Predicate<? super T> condition, final Supplier<X> exceptionSupplier) {
withRule(condition, value -> exceptionSupplier.get());
}
/**
* 添加一个校验规则
*
* @param <X> 自定义异常类型
* @param condition 校验条件
* @param exceptionFunction 自定义异常
*/
protected final <X extends RuntimeException> void withRule(
final Predicate<? super T> condition, final Function<T, X> exceptionFunction) {
withRule(value -> {
if (!condition.test(value)) {
throw e.apply(value);
throw exceptionFunction.apply(value);
}
});
}

View File

@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
@ -28,7 +29,8 @@ import java.util.function.Supplier;
*
* @author ZhouXY
*/
public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
public class BoolPropertyValidator<T>
extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
BoolPropertyValidator(Function<T, Boolean> getter) {
super(getter);
@ -41,40 +43,42 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
*
* @return 属性校验器
*/
public BoolPropertyValidator<T> isTrueValue() {
return isTrueValue("The input must be true.");
public final BoolPropertyValidator<T> isTrueValue() {
return withRule(Conditions.isTrueValue(), "The input must be true.");
}
/**
* 添加一条判断属性值是否为 {@code true} 的校验规则
*
* @param errMsg 校验失败的错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public BoolPropertyValidator<T> isTrueValue(String errMsg) {
return isTrueValue(convertToExceptionFunction(errMsg));
public final BoolPropertyValidator<T> isTrueValue(final String errorMessage) {
return withRule(Conditions.isTrueValue(), errorMessage);
}
/**
* 添加一条判断属性值是否为 {@code true} 的校验规则
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(Supplier<E> e) {
return isTrueValue(convertToExceptionFunction(e));
public final <X extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.isTrueValue(), exceptionSupplier);
}
/**
* 添加一条判断属性值是否为 {@code true} 的校验规则
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(Function<Boolean, E> e) {
return withRule(Boolean.TRUE::equals, e);
public final <X extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
Function<Boolean, X> exceptionFunction) {
return withRule(Conditions.isTrueValue(), exceptionFunction);
}
// ====== isFalseValue ======
@ -84,40 +88,53 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
*
* @return 属性校验器
*/
public BoolPropertyValidator<T> isFalseValue() {
return isFalseValue("The input must be false.");
public final BoolPropertyValidator<T> isFalseValue() {
return withRule(Conditions.isFalseValue(), "The input must be false.");
}
/**
* 添加一条判断属性值是否为 {@code false} 的校验规则
*
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public BoolPropertyValidator<T> isFalseValue(String errMsg) {
return isFalseValue(convertToExceptionFunction(errMsg));
public final BoolPropertyValidator<T> isFalseValue(final String errorMessage) {
return withRule(Conditions.isFalseValue(), errorMessage);
}
/**
* 添加一条判断属性值是否为 {@code false} 的校验规则
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(Supplier<E> e) {
return isFalseValue(convertToExceptionFunction(e));
public final <X extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.isFalseValue(), exceptionSupplier);
}
/**
* 添加一条判断属性值是否为 {@code false} 的校验规则
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(Function<Boolean, E> e) {
return withRule(Boolean.FALSE::equals, e);
public final <X extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
final Function<Boolean, X> exceptionFunction) {
return withRule(Conditions.isFalseValue(), exceptionFunction);
}
private static class Conditions {
private static <TProperty> Predicate<TProperty> isTrueValue() {
return Boolean.TRUE::equals;
}
private static <TProperty> Predicate<TProperty> isFalseValue() {
return Boolean.FALSE::equals;
}
}
@Override

View File

@ -32,10 +32,10 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
*
* @author ZhouXY
*/
public class CollectionPropertyValidator<T, TElement>
extends BasePropertyValidator<T, Collection<TElement>, CollectionPropertyValidator<T, TElement>> {
public class CollectionPropertyValidator<T, E>
extends BasePropertyValidator<T, Collection<E>, CollectionPropertyValidator<T, E>> {
CollectionPropertyValidator(Function<T, Collection<TElement>> getter) {
CollectionPropertyValidator(Function<T, Collection<E>> getter) {
super(getter);
}
@ -48,42 +48,42 @@ public class CollectionPropertyValidator<T, TElement>
*
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> notEmpty() {
return notEmpty("The input must not be empty.");
public final CollectionPropertyValidator<T, E> notEmpty() {
return withRule(Conditions.notEmpty(), "The input must not be empty.");
}
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> notEmpty(String errMsg) {
return notEmpty(convertToExceptionFunction(errMsg));
public final CollectionPropertyValidator<T, E> notEmpty(final String errorMessage) {
return withRule(Conditions.notEmpty(), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
Supplier<E> e) {
return notEmpty(convertToExceptionFunction(e));
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> notEmpty(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.notEmpty(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否非空
*
* @param <E> 自定义异常类型
* @param e 自定义异常
* @param <X> 自定义异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
Function<Collection<TElement>, E> e) {
return withRule(CollectionTools::isNotEmpty, e);
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> notEmpty(
final Function<Collection<E>, X> exceptionFunction) {
return withRule(Conditions.notEmpty(), exceptionFunction);
}
// ================================
@ -99,40 +99,41 @@ public class CollectionPropertyValidator<T, TElement>
*
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> isEmpty() {
return isEmpty("The input must be empty.");
public final CollectionPropertyValidator<T, E> isEmpty() {
return withRule(Conditions.isEmpty(), "The input must be empty.");
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> isEmpty(String errMsg) {
return isEmpty(convertToExceptionFunction(errMsg));
public final CollectionPropertyValidator<T, E> isEmpty(
final String errorMessage) {
return withRule(Conditions.isEmpty(), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
Supplier<E> e) {
return isEmpty(convertToExceptionFunction(e));
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> isEmpty(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.isEmpty(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否为空
*
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
Function<Collection<TElement>, E> e) {
return withRule(CollectionTools::isEmpty, e);
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> isEmpty(
final Function<Collection<E>, X> exceptionFunction) {
return withRule(Conditions.isEmpty(), exceptionFunction);
}
// ================================
@ -146,48 +147,62 @@ public class CollectionPropertyValidator<T, TElement>
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param condition 校验条件
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> allMatch(Predicate<TElement> condition) {
return allMatch(condition, convertToExceptionFunction("All elements must match the condition."));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param errMsg 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> allMatch(Predicate<TElement> condition, String errMsg) {
return allMatch(condition, convertToExceptionFunction(errMsg));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> allMatch(
Predicate<TElement> condition, Supplier<E> e) {
return allMatch(condition, convertToExceptionFunction(e));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> allMatch(
Predicate<TElement> condition, Function<TElement, E> e) {
public final CollectionPropertyValidator<T, E> allMatch(
final Predicate<E> condition) {
return withRule(c -> c.forEach(element -> {
if (!condition.test(element)) {
throw e.apply(element);
throw ValidationException.withMessage("All elements must match the condition.");
}
}));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验规则
* @param errorMessage 异常信息
* @return 属性校验器
*/
public final CollectionPropertyValidator<T, E> allMatch(
final Predicate<E> condition, final String errorMessage) {
return withRule(c -> c.forEach(element -> {
if (!condition.test(element)) {
throw ValidationException.withMessage(errorMessage);
}
}));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验条件
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> allMatch(
final Predicate<E> condition, final Supplier<X> exceptionSupplier) {
return withRule(c -> c.forEach(element -> {
if (!condition.test(element)) {
throw exceptionSupplier.get();
}
}));
}
/**
* 添加一条校验属性的规则校验是否所有元素都满足条件
*
* @param condition 校验条件
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> allMatch(
final Predicate<E> condition, final Function<E, X> exceptionFunction) {
return withRule(c -> c.forEach(element -> {
if (!condition.test(element)) {
throw exceptionFunction.apply(element);
}
}));
}
@ -204,47 +219,38 @@ public class CollectionPropertyValidator<T, TElement>
* 添加一条校验属性的规则校验属性大小是否等于指定大小
*
* @param size 指定大小
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> size(int size, String errMsg) {
return size(size, convertToExceptionFunction(errMsg));
public final CollectionPropertyValidator<T, E> size(
final int size, final String errorMessage) {
return withRule(Conditions.size(size), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性大小是否等于指定大小
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param size 指定大小
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> size(
int size, Supplier<E> e) {
return size(size, convertToExceptionFunction(e));
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
final int size, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.size(size), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性大小是否等于指定大小
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param size 指定大小
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> size(
int size, Function<Collection<TElement>, E> e) {
AssertTools.checkArgument(size >= 0,
"The expected size must be greater than or equal to 0.");
return withRule(s -> s == null || s.size() == size, e);
}
static <TElement> boolean checkSize(Collection<TElement> str, int min, int max) {
if (str == null) {
return true;
}
final int size = str.size();
return size >= min && size <= max;
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
final int size, final Function<Collection<E>, X> exceptionFunction) {
return withRule(Conditions.size(size), exceptionFunction);
}
/**
@ -252,11 +258,12 @@ public class CollectionPropertyValidator<T, TElement>
*
* @param min 最小大小
* @param max 最大大小
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public CollectionPropertyValidator<T, TElement> size(int min, int max, String errMsg) {
return size(min, max, convertToExceptionFunction(errMsg));
public final CollectionPropertyValidator<T, E> size(
final int min, final int max, final String errorMessage) {
return withRule(Conditions.size(min, max), errorMessage);
}
/**
@ -264,12 +271,12 @@ public class CollectionPropertyValidator<T, TElement>
*
* @param min 最小大小
* @param max 最大大小
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> size(
int min, int max, Supplier<E> e) {
return size(min, max, convertToExceptionFunction(e));
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
final int min, final int max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.size(min, max), exceptionSupplier);
}
/**
@ -277,22 +284,49 @@ public class CollectionPropertyValidator<T, TElement>
*
* @param min 最小大小
* @param max 最大大小
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> size(
int min, int max, Function<Collection<TElement>, E> e) {
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
return withRule(s -> checkSize(s, min, max), e);
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
final int min, final int max, final Function<Collection<E>, X> exceptionFunction) {
return withRule(Conditions.size(min, max), exceptionFunction);
}
// ================================
// #endregion - size
// ================================
private static class Conditions {
private static Predicate<Collection<?>> isEmpty() {
return CollectionTools::isEmpty;
}
private static Predicate<Collection<?>> notEmpty() {
return CollectionTools::isNotEmpty;
}
private static Predicate<Collection<?>> size(int size) {
AssertTools.checkArgument(size >= 0,
"The expected size must be non-negative.");
return collection -> collection == null || collection.size() == size;
}
private static Predicate<Collection<?>> size(int min, int max) {
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
return collection -> {
if (collection == null) {
return true;
}
int size = collection.size();
return size >= min && size <= max;
};
}
}
@Override
protected CollectionPropertyValidator<T, TElement> thisObject() {
protected CollectionPropertyValidator<T, E> thisObject() {
return this;
}
}

View File

@ -26,8 +26,7 @@ import java.util.function.Function;
*
* @param <T> 待校验对象类型
* @param <TProperty> 属性类型
* @param <TPropertyValidator> 当前属性校验器类型用于链式调用
* @see Range
* @see com.google.common.collect.Range
* @author ZhouXY
*/
public class ComparablePropertyValidator<T, TProperty extends Comparable<TProperty>>

View File

@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
@ -44,43 +45,47 @@ public class DoublePropertyValidator<T>
* @param min 最小值
* @return 属性校验器
*/
public DoublePropertyValidator<T> gt(double min) {
return gt(min, convertToExceptionFunction("The input must be greater than '%s'.", min));
public final DoublePropertyValidator<T> gt(final double min) {
return withRule(Conditions.greaterThan(min),
"The input must be greater than '%s'.", min);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public DoublePropertyValidator<T> gt(double min, String errMsg) {
return gt(min, convertToExceptionFunction(errMsg));
public final DoublePropertyValidator<T> gt(
final double min, final String errorMessage) {
return withRule(Conditions.greaterThan(min), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 错误信息
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> gt(double min, Supplier<E> e) {
return gt(min, convertToExceptionFunction(e));
public final <X extends RuntimeException> DoublePropertyValidator<T> gt(
final double min, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.greaterThan(min), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> gt(double min, Function<Double, E> e) {
return withRule(value -> (value != null && value > min), e);
public final <X extends RuntimeException> DoublePropertyValidator<T> gt(
final double min, final Function<Double, X> exceptionFunction) {
return withRule(Conditions.greaterThan(min), exceptionFunction);
}
// ================================
@ -97,43 +102,47 @@ public class DoublePropertyValidator<T>
* @param min 最小值
* @return 属性校验器
*/
public DoublePropertyValidator<T> ge(double min) {
return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%s'.", min));
public final DoublePropertyValidator<T> ge(final double min) {
return withRule(Conditions.greaterThanOrEqualTo(min),
"The input must be greater than or equal to '%s'.", min);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public DoublePropertyValidator<T> ge(double min, String errMsg) {
return ge(min, convertToExceptionFunction(errMsg));
public final DoublePropertyValidator<T> ge(
final double min, final String errorMessage) {
return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> ge(double min, Supplier<E> e) {
return ge(min, convertToExceptionFunction(e));
public final <X extends RuntimeException> DoublePropertyValidator<T> ge(
final double min, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> ge(double min, Function<Double, E> e) {
return withRule(value -> (value != null && value >= min), e);
public final <X extends RuntimeException> DoublePropertyValidator<T> ge(
final double min, final Function<Double, X> exceptionFunction) {
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction);
}
// ================================
@ -150,43 +159,47 @@ public class DoublePropertyValidator<T>
* @param max 最大值
* @return 属性校验器
*/
public DoublePropertyValidator<T> lt(double max) {
return lt(max, convertToExceptionFunction("The input must be less than '%s'.", max));
public final DoublePropertyValidator<T> lt(final double max) {
return withRule(Conditions.lessThan(max),
"The input must be less than '%s'.", max);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public DoublePropertyValidator<T> lt(double max, String errMsg) {
return lt(max, convertToExceptionFunction(errMsg));
public final DoublePropertyValidator<T> lt(
final double max, final String errorMessage) {
return withRule(Conditions.lessThan(max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> lt(double max, Supplier<E> e) {
return lt(max, convertToExceptionFunction(e));
public final <X extends RuntimeException> DoublePropertyValidator<T> lt(
final double max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.lessThan(max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> lt(double max, Function<Double, E> e) {
return withRule(value -> (value != null && value < max), e);
public final <X extends RuntimeException> DoublePropertyValidator<T> lt(
final double max, final Function<Double, X> exceptionFunction) {
return withRule(Conditions.lessThan(max), exceptionFunction);
}
// ================================
@ -203,43 +216,47 @@ public class DoublePropertyValidator<T>
* @param max 最大值
* @return 属性校验器
*/
public DoublePropertyValidator<T> le(double max) {
return le(max, convertToExceptionFunction("The input must be less than or equal to '%s'.", max));
public final DoublePropertyValidator<T> le(final double max) {
return withRule(Conditions.lessThanOrEqualTo(max),
"The input must be less than or equal to '%s'.", max);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public DoublePropertyValidator<T> le(double max, String errMsg) {
return le(max, convertToExceptionFunction(errMsg));
public final DoublePropertyValidator<T> le(
final double max, final String errorMessage) {
return withRule(Conditions.lessThanOrEqualTo(max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> le(double max, Supplier<E> e) {
return le(max, convertToExceptionFunction(e));
public final <X extends RuntimeException> DoublePropertyValidator<T> le(
final double max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> le(double max, Function<Double, E> e) {
return withRule(value -> (value != null && value <= max), e);
public final <X extends RuntimeException> DoublePropertyValidator<T> le(
final double max, final Function<Double, X> exceptionFunction) {
return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction);
}
// ================================
@ -250,4 +267,23 @@ public class DoublePropertyValidator<T>
protected DoublePropertyValidator<T> thisObject() {
return this;
}
private static class Conditions {
private static Predicate<Double> greaterThan(double min) {
return input -> input == null || input > min;
}
private static Predicate<Double> greaterThanOrEqualTo(double min) {
return input -> input == null || input >= min;
}
private static Predicate<Double> lessThan(double max) {
return input -> input == null || input < max;
}
private static Predicate<Double> lessThanOrEqualTo(double max) {
return input -> input == null || input <= max;
}
}
}

View File

@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.Predicate;
/**
* 整数属性校验器
@ -44,43 +45,47 @@ public class IntPropertyValidator<T>
* @param min 最小值
* @return 属性校验器
*/
public IntPropertyValidator<T> gt(int min) {
return gt(min, convertToExceptionFunction("The input must be greater than '%d'.", min));
public IntPropertyValidator<T> gt(final int min) {
return withRule(Conditions.greaterThan(min),
"The input must be greater than '%d'.", min);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public IntPropertyValidator<T> gt(int min, String errMsg) {
return gt(min, convertToExceptionFunction(errMsg));
public IntPropertyValidator<T> gt(
final int min, final String errorMessage) {
return withRule(Conditions.greaterThan(min), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 错误信息
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> gt(int min, Supplier<E> e) {
return gt(min, convertToExceptionFunction(e));
public <X extends RuntimeException> IntPropertyValidator<T> gt(
final int min, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.greaterThan(min), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> gt(int min, Function<Integer, E> e) {
return withRule(value -> (value != null && value > min), e);
public <X extends RuntimeException> IntPropertyValidator<T> gt(
final int min, final Function<Integer, X> exceptionFunction) {
return withRule(Conditions.greaterThan(min), exceptionFunction);
}
// ================================
@ -97,43 +102,46 @@ public class IntPropertyValidator<T>
* @param min 最小值
* @return 属性校验器
*/
public IntPropertyValidator<T> ge(int min) {
return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%d'.", min));
public IntPropertyValidator<T> ge(final int min) {
return withRule(Conditions.greaterThanOrEqualTo(min),
"The input must be greater than or equal to '%d'.", min);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public IntPropertyValidator<T> ge(int min, String errMsg) {
return ge(min, convertToExceptionFunction(errMsg));
public IntPropertyValidator<T> ge(final int min, final String errorMessage) {
return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> ge(int min, Supplier<E> e) {
return ge(min, convertToExceptionFunction(e));
public <X extends RuntimeException> IntPropertyValidator<T> ge(
final int min, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> ge(int min, Function<Integer, E> e) {
return withRule(value -> (value != null && value >= min), e);
public <X extends RuntimeException> IntPropertyValidator<T> ge(
final int min, final Function<Integer, X> exceptionFunction) {
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction);
}
// ================================
@ -150,43 +158,47 @@ public class IntPropertyValidator<T>
* @param max 最大值
* @return 属性校验器
*/
public IntPropertyValidator<T> lt(int max) {
return lt(max, convertToExceptionFunction("The input must be less than '%d'.", max));
public IntPropertyValidator<T> lt(final int max) {
return withRule(Conditions.lessThan(max),
"The input must be less than '%d'.", max);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public IntPropertyValidator<T> lt(int max, String errMsg) {
return lt(max, convertToExceptionFunction(errMsg));
public IntPropertyValidator<T> lt(
final int max, final String errorMessage) {
return withRule(Conditions.lessThan(max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> lt(int max, Supplier<E> e) {
return lt(max, convertToExceptionFunction(e));
public <X extends RuntimeException> IntPropertyValidator<T> lt(
final int max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.lessThan(max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> lt(int max, Function<Integer, E> e) {
return withRule(value -> (value != null && value < max), e);
public <X extends RuntimeException> IntPropertyValidator<T> lt(
final int max, final Function<Integer, X> exceptionFunction) {
return withRule(Conditions.lessThan(max), exceptionFunction);
}
// ================================
@ -203,43 +215,47 @@ public class IntPropertyValidator<T>
* @param max 最大值
* @return 属性校验器
*/
public IntPropertyValidator<T> le(int max) {
return le(max, convertToExceptionFunction("The input must be less than or equal to '%d'.", max));
public IntPropertyValidator<T> le(final int max) {
return withRule(Conditions.lessThanOrEqualTo(max),
"The input must be less than or equal to '%d'.", max);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public IntPropertyValidator<T> le(int max, String errMsg) {
return le(max, convertToExceptionFunction(errMsg));
public IntPropertyValidator<T> le(
final int max, final String errorMessage) {
return withRule(Conditions.lessThanOrEqualTo(max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> le(int max, Supplier<E> e) {
return le(max, convertToExceptionFunction(e));
public <X extends RuntimeException> IntPropertyValidator<T> le(
final int max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> le(int max, Function<Integer, E> e) {
return withRule(value -> (value != null && value <= max), e);
public <X extends RuntimeException> IntPropertyValidator<T> le(
final int max, final Function<Integer, X> exceptionFunction) {
return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction);
}
// ================================
@ -250,4 +266,23 @@ public class IntPropertyValidator<T>
protected IntPropertyValidator<T> thisObject() {
return this;
}
private static class Conditions {
private static Predicate<Integer> greaterThan(int min) {
return input -> input == null || input > min;
}
private static Predicate<Integer> greaterThanOrEqualTo(int min) {
return input -> input == null || input >= min;
}
private static Predicate<Integer> lessThan(int max) {
return input -> input == null || input < max;
}
private static Predicate<Integer> lessThanOrEqualTo(int max) {
return input -> input == null || input <= max;
}
}
}

View File

@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
@ -44,43 +45,47 @@ public class LongPropertyValidator<T>
* @param min 最小值
* @return 属性校验器
*/
public LongPropertyValidator<T> gt(long min) {
return gt(min, convertToExceptionFunction("The input must be greater than '%d'.", min));
public final LongPropertyValidator<T> gt(final long min) {
return withRule(Conditions.greaterThan(min),
"The input must be greater than '%d'.", min);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public LongPropertyValidator<T> gt(long min, String errMsg) {
return gt(min, convertToExceptionFunction(errMsg));
public final LongPropertyValidator<T> gt(
final long min, final String errorMessage) {
return withRule(Conditions.greaterThan(min), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 错误信息
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> gt(long min, Supplier<E> e) {
return gt(min, convertToExceptionFunction(e));
public final <X extends RuntimeException> LongPropertyValidator<T> gt(
final long min, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.greaterThan(min), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否大于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> gt(long min, Function<Long, E> e) {
return withRule(value -> (value != null && value > min), e);
public final <X extends RuntimeException> LongPropertyValidator<T> gt(
final long min, final Function<Long, X> exceptionFunction) {
return withRule(Conditions.greaterThan(min), exceptionFunction);
}
// ================================
@ -97,43 +102,47 @@ public class LongPropertyValidator<T>
* @param min 最小值
* @return 属性校验器
*/
public LongPropertyValidator<T> ge(long min) {
return ge(min, convertToExceptionFunction("The input must be greater than or equal to '%d'.", min));
public final LongPropertyValidator<T> ge(final long min) {
return withRule(Conditions.greaterThanOrEqualTo(min),
"The input must be greater than or equal to '%d'.", min);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param min 最小值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public LongPropertyValidator<T> ge(long min, String errMsg) {
return ge(min, convertToExceptionFunction(errMsg));
public final LongPropertyValidator<T> ge(
final long min, final String errorMessage) {
return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> ge(long min, Supplier<E> e) {
return ge(min, convertToExceptionFunction(e));
public final <X extends RuntimeException> LongPropertyValidator<T> ge(
final long min, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否大于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param min 最小值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> ge(long min, Function<Long, E> e) {
return withRule(value -> (value != null && value >= min), e);
public final <X extends RuntimeException> LongPropertyValidator<T> ge(
final long min, final Function<Long, X> exceptionFunction) {
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction);
}
// ================================
@ -150,43 +159,47 @@ public class LongPropertyValidator<T>
* @param max 最大值
* @return 属性校验器
*/
public LongPropertyValidator<T> lt(long max) {
return lt(max, convertToExceptionFunction("The input must be less than '%d'.", max));
public final LongPropertyValidator<T> lt(final long max) {
return withRule(Conditions.lessThan(max),
"The input must be less than '%d'.", max);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public LongPropertyValidator<T> lt(long max, String errMsg) {
return lt(max, convertToExceptionFunction(errMsg));
public final LongPropertyValidator<T> lt(
final long max, final String errorMessage) {
return withRule(Conditions.lessThan(max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> lt(long max, Supplier<E> e) {
return lt(max, convertToExceptionFunction(e));
public final <X extends RuntimeException> LongPropertyValidator<T> lt(
final long max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.lessThan(max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否小于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> lt(long max, Function<Long, E> e) {
return withRule(value -> (value != null && value < max), e);
public final <X extends RuntimeException> LongPropertyValidator<T> lt(
final long max, final Function<Long, X> exceptionFunction) {
return withRule(Conditions.lessThan(max), exceptionFunction);
}
// ================================
@ -203,43 +216,47 @@ public class LongPropertyValidator<T>
* @param max 最大值
* @return 属性校验器
*/
public LongPropertyValidator<T> le(long max) {
return le(max, convertToExceptionFunction("The input must be less than or equal to '%d'.", max));
public final LongPropertyValidator<T> le(final long max) {
return withRule(Conditions.lessThanOrEqualTo(max),
"The input must be less than or equal to '%d'.", max);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param max 最大值
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public LongPropertyValidator<T> le(long max, String errMsg) {
return le(max, convertToExceptionFunction(errMsg));
public final LongPropertyValidator<T> le(
final long max, final String errorMessage) {
return withRule(Conditions.lessThanOrEqualTo(max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> le(long max, Supplier<E> e) {
return le(max, convertToExceptionFunction(e));
public final <X extends RuntimeException> LongPropertyValidator<T> le(
final long max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否小于等于给定值
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param max 最大值
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> le(long max, Function<Long, E> e) {
return withRule(value -> (value != null && value <= max), e);
public final <X extends RuntimeException> LongPropertyValidator<T> le(
final long max, final Function<Long, X> exceptionFunction) {
return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction);
}
// ================================
@ -250,4 +267,23 @@ public class LongPropertyValidator<T>
protected LongPropertyValidator<T> thisObject() {
return this;
}
private static class Conditions {
private static Predicate<Long> greaterThan(long min) {
return input -> input == null || input > min;
}
private static Predicate<Long> greaterThanOrEqualTo(long min) {
return input -> input == null || input >= min;
}
private static Predicate<Long> lessThan(long max) {
return input -> input == null || input < max;
}
private static Predicate<Long> lessThanOrEqualTo(long max) {
return input -> input == null || input <= max;
}
}
}

View File

@ -36,7 +36,7 @@ public class PairPropertyValidator<T, V1, V2>
/**
* 添加一条校验属性的规则校验二元组是否满足给定的条件
*
* @param condition 校验规则
* @param condition 校验条件
* @return 属性校验器
*/
public final PairPropertyValidator<T, V1, V2> must(BiPredicate<V1, V2> condition) {
@ -46,37 +46,37 @@ public class PairPropertyValidator<T, V1, V2>
/**
* 添加一条校验属性的规则校验二元组是否满足给定的条件
*
* @param condition 校验规则
* @param errMsg 错误信息
* @param condition 校验条件
* @param errorMessage 异常信息
* @return 属性校验器
*/
public final PairPropertyValidator<T, V1, V2> must(BiPredicate<V1, V2> condition, String errMsg) {
return must(pair -> condition.test(pair.getKey(), pair.getValue()), errMsg);
public final PairPropertyValidator<T, V1, V2> must(BiPredicate<V1, V2> condition, String errorMessage) {
return must(pair -> condition.test(pair.getKey(), pair.getValue()), errorMessage);
}
/**
* 添加一条校验属性的规则校验二元组是否满足给定的条件
*
* @param condition 校验规则
* @param e 自定义异常
* @param condition 校验条件
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public final <E extends RuntimeException> PairPropertyValidator<T, V1, V2> must(
BiPredicate<V1, V2> condition, Supplier<E> e) {
return must(pair -> condition.test(pair.getKey(), pair.getValue()), e);
public final <X extends RuntimeException> PairPropertyValidator<T, V1, V2> must(
BiPredicate<V1, V2> condition, Supplier<X> exceptionSupplier) {
return must(pair -> condition.test(pair.getKey(), pair.getValue()), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验二元组是否满足给定的条件
*
* @param condition 校验规则
* @param e 自定义异常
* @param condition 校验条件
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public final <E extends RuntimeException> PairPropertyValidator<T, V1, V2> must(
BiPredicate<V1, V2> condition, BiFunction<V1, V2, E> e) {
public final <X extends RuntimeException> PairPropertyValidator<T, V1, V2> must(
BiPredicate<V1, V2> condition, BiFunction<V1, V2, X> exceptionFunction) {
return must(pair -> condition.test(pair.getKey(), pair.getValue()),
pair -> e.apply(pair.getKey(), pair.getValue()));
pair -> exceptionFunction.apply(pair.getKey(), pair.getValue()));
}
@Override

View File

@ -17,26 +17,24 @@
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.util.ArrayTools;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.RegexTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
* StringPropertyValidator
*
* <p>
* 针对文本字段的校验器
* </p>
* 针对文本类型的属性校验器
*
* @author ZhouXY
*/
public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<T, String, StringPropertyValidator<T>> {
public class StringPropertyValidator<T>
extends BaseComparablePropertyValidator<T, String, StringPropertyValidator<T>> {
StringPropertyValidator(Function<T, String> getter) {
super(getter);
@ -49,38 +47,39 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
/**
* 添加一条校验属性的规则校验属性是否匹配正则表达式
*
* @param regex 正则表达式
* @param errMsg 异常信息
* @param pattern 正则表达式
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matches(Pattern regex, String errMsg) {
return matches(regex, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> matches(
final Pattern pattern, final String errorMessage) {
return withRule(Conditions.matches(pattern), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否匹配正则表达式
*
* @param <E> 异常类型
* @param regex 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param pattern 正则表达式
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex, Supplier<E> e) {
return matches(regex, convertToExceptionFunction(e));
public <X extends RuntimeException> StringPropertyValidator<T> matches(
final Pattern pattern, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.matches(pattern), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否匹配正则表达式
*
* @param <E> 异常类型
* @param regex 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param pattern 正则表达式
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex, Function<String, E> e) {
return withRule(input -> (input == null || RegexTools.matches(input, regex)), e);
public final <X extends RuntimeException> StringPropertyValidator<T> matches(
final Pattern pattern, final Function<String, X> exceptionFunction) {
return withRule(Conditions.matches(pattern), exceptionFunction);
}
// ================================
@ -94,75 +93,77 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @param patterns 正则表达式
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesOne(Pattern[] regexs, String errMsg) {
return matchesOne(regexs, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> matchesOne(
final Pattern[] patterns, final String errorMessage) {
return withRule(Conditions.matchesOne(patterns), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs, Supplier<E> e) {
return matchesOne(regexs, convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> matchesOne(
final Pattern[] patterns, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.matchesOne(patterns), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @return
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs, Function<String, E> e) {
return withRule(input -> input == null || RegexTools.matchesOne(input, regexs), e);
public final <X extends RuntimeException> StringPropertyValidator<T> matchesOne(
final Pattern[] patterns, final Function<String, X> exceptionFunction) {
return withRule(Conditions.matchesOne(patterns), exceptionFunction);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @param patterns 正则表达式
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesOne(List<Pattern> regexs, String errMsg) {
return matchesOne(regexs, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> matchesOne(
final Collection<Pattern> patterns, final String errorMessage) {
return withRule(Conditions.matchesOne(patterns), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs, Supplier<E> e) {
return matchesOne(regexs, convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> matchesOne(
final Collection<Pattern> patterns, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.matchesOne(patterns), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的多个正则表达式的其中一个
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs, Function<String, E> e) {
return withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), e);
public final <X extends RuntimeException> StringPropertyValidator<T> matchesOne(
final Collection<Pattern> patterns, final Function<String, X> exceptionFunction) {
return withRule(Conditions.matchesOne(patterns), exceptionFunction);
}
// ================================
@ -176,75 +177,77 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @param patterns 正则表达式
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesAll(Pattern[] regexs, String errMsg) {
return matchesAll(regexs, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> matchesAll(
final Pattern[] patterns, final String errorMessage) {
return withRule(Conditions.matchesAll(patterns), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs, Supplier<E> e) {
return matchesAll(regexs, convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
final Pattern[] patterns, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.matchesAll(patterns), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs, Function<String, E> e) {
return withRule(input -> input == null || RegexTools.matchesAll(input, regexs), e);
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
final Pattern[] patterns, final Function<String, X> exceptionFunction) {
return withRule(Conditions.matchesAll(patterns), exceptionFunction);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param regexs 正则表达式
* @param errMsg 异常信息
* @param patterns 正则表达式
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> matchesAll(Collection<Pattern> regexs, String errMsg) {
return matchesAll(regexs, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> matchesAll(
final Collection<Pattern> patterns, final String errorMessage) {
return withRule(Conditions.matchesAll(patterns), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs, Supplier<E> e) {
return matchesAll(regexs, convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
final Collection<Pattern> patterns, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.matchesAll(patterns), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否匹配指定的所有正则表达式
*
* @param <E> 异常类型
* @param regexs 正则表达式
* @param e 自定义异常
* @param <X> 异常类型
* @param patterns 正则表达式
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs, Function<String, E> e) {
return withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), e);
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
final Collection<Pattern> patterns, final Function<String, X> exceptionFunction) {
return withRule(Conditions.matchesAll(patterns), exceptionFunction);
}
// ================================
@ -260,40 +263,43 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*
* @return 属性校验器
*/
public StringPropertyValidator<T> notBlank() {
return notBlank("The input must not be blank.");
public final StringPropertyValidator<T> notBlank() {
return withRule(Conditions.notBlank(),
"The input must not be blank.");
}
/**
* 添加一条校验属性的规则校验属性是否不为空白字符串
*
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> notBlank(String errMsg) {
return notBlank(convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> notBlank(final String errorMessage) {
return withRule(Conditions.notBlank(), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否不为空白字符串
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Supplier<E> e) {
return notBlank(convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> notBlank(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.notBlank(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否不为空白字符串
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Function<String, E> e) {
return withRule(StringTools::isNotBlank, e);
public final <X extends RuntimeException> StringPropertyValidator<T> notBlank(
final Function<String, X> exceptionFunction) {
return withRule(Conditions.notBlank(), exceptionFunction);
}
// ================================
@ -309,41 +315,43 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*
* @return 属性校验器
*/
public StringPropertyValidator<T> emailAddress() {
return emailAddress("The input is not a valid email address.");
public final StringPropertyValidator<T> emailAddress() {
return withRule(Conditions.emailAddress(),
"The input is not a valid email address.");
}
/**
* 添加一条校验属性的规则校验属性是否是邮箱地址
*
* @param errMsg 校验失败的错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> emailAddress(String errMsg) {
return emailAddress(convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> emailAddress(final String errorMessage) {
return withRule(Conditions.emailAddress(), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性是否是邮箱地址
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(Supplier<E> e) {
return emailAddress(convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> emailAddress(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.emailAddress(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性是否是邮箱地址
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(
Function<String, E> e) {
return matches(PatternConsts.EMAIL, e);
public final <X extends RuntimeException> StringPropertyValidator<T> emailAddress(
final Function<String, X> exceptionFunction) {
return withRule(Conditions.emailAddress(), exceptionFunction);
}
// ================================
@ -359,41 +367,43 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*
* @return 属性校验器
*/
public StringPropertyValidator<T> notEmpty() {
return notEmpty("The input must not be empty.");
public final StringPropertyValidator<T> notEmpty() {
return withRule(Conditions.notEmpty(),
"The input must not be empty.");
}
/**
* 添加一条校验属性的规则校验字符串属性是否不为空
*
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> notEmpty(String errMsg) {
return notEmpty(convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> notEmpty(final String errorMessage) {
return withRule(Conditions.notEmpty(), errorMessage);
}
/**
* 添加一条校验属性的规则校验字符串属性是否不为空
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(Supplier<E> e) {
return notEmpty(convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> notEmpty(
final Supplier<X> exceptionSupplier) {
return withRule(Conditions.notEmpty(), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验字符串属性是否不为空
*
* @param <E> 异常类型
* @param e 自定义异常
* @param <X> 异常类型
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(
Function<String, E> e) {
return withRule(s -> s != null && !s.isEmpty(), e);
public final <X extends RuntimeException> StringPropertyValidator<T> notEmpty(
final Function<String, X> exceptionFunction) {
return withRule(Conditions.notEmpty(), exceptionFunction);
}
// ================================
@ -408,45 +418,38 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param length 指定长度
* @param errMsg 异常信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> length(int length, String errMsg) {
return length(length, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> length(
final int length, final String errorMessage) {
return withRule(Conditions.length(length), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param length 指定长度
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> length(int length, Supplier<E> e) {
return length(length, convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> length(
final int length, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.length(length), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性长度是否等于指定长度
*
* @param <E> 异常类型
* @param <X> 异常类型
* @param length 指定长度
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @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.");
return withRule(s -> s == null || s.length() == length, e);
}
static boolean checkLength(String str, int min, int max) {
if (str == null) {
return true;
}
final int len = str.length();
return len >= min && len <= max;
public final <X extends RuntimeException> StringPropertyValidator<T> length(
final int length, final Function<String, X> exceptionFunction) {
return withRule(Conditions.length(length), exceptionFunction);
}
/**
@ -454,39 +457,40 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*
* @param min 最小长度
* @param max 最大长度
* @param errMsg 错误信息
* @param errorMessage 异常信息
* @return 属性校验器
*/
public StringPropertyValidator<T> length(int min, int max, String errMsg) {
return length(min, max, convertToExceptionFunction(errMsg));
public final StringPropertyValidator<T> length(
final int min, final int max, final String errorMessage) {
return withRule(Conditions.length(min, max), errorMessage);
}
/**
* 添加一条校验属性的规则校验属性的长度范围
*
* @param <X> 异常类型
* @param min 最小长度
* @param max 最大长度
* @param e 自定义异常
* @param exceptionSupplier 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
Supplier<E> e) {
return length(min, max, convertToExceptionFunction(e));
public final <X extends RuntimeException> StringPropertyValidator<T> length(
final int min, final int max, final Supplier<X> exceptionSupplier) {
return withRule(Conditions.length(min, max), exceptionSupplier);
}
/**
* 添加一条校验属性的规则校验属性的长度范围
*
* @param <X> 异常类型
* @param min 最小长度
* @param max 最大长度
* @param e 自定义异常
* @param exceptionFunction 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
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.");
return withRule(s -> checkLength(s, min, max), e);
public final <X extends RuntimeException> StringPropertyValidator<T> length(
final int min, final int max, final Function<String, X> exceptionFunction) {
return withRule(Conditions.length(min, max), exceptionFunction);
}
// ================================
@ -497,4 +501,63 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
protected StringPropertyValidator<T> thisObject() {
return this;
}
private static class Conditions {
private static Predicate<String> notEmpty() {
return StringTools::isNotEmpty;
}
private static Predicate<String> notBlank() {
return StringTools::isNotBlank;
}
private static Predicate<String> matches(Pattern pattern) {
AssertTools.checkArgumentNotNull(pattern);
return input -> input == null || RegexTools.matches(input, pattern);
}
private static Predicate<String> matchesOne(Pattern[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return input -> input == null || RegexTools.matchesOne(input, patterns);
}
private static Predicate<String> matchesOne(Collection<Pattern> patterns) {
AssertTools.checkArgumentNotNull(patterns, "patterns must not be null.");
return input -> input == null || RegexTools.matchesOne(input, patterns.toArray(new Pattern[0]));
}
private static Predicate<String> matchesAll(Pattern[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return input -> input == null || RegexTools.matchesAll(input, patterns);
}
private static Predicate<String> matchesAll(Collection<Pattern> patterns) {
AssertTools.checkArgumentNotNull(patterns, "patterns must not be null.");
return input -> input == null || RegexTools.matchesAll(input, patterns.toArray(new Pattern[0]));
}
private static Predicate<String> emailAddress() {
return input -> input == null || RegexTools.matches(input, PatternConsts.EMAIL);
}
private static Predicate<String> length(int length) {
AssertTools.checkArgument(length >= 0,
"The expected length must be non-negative.");
return input -> input == null || input.length() == length;
}
private static Predicate<String> length(int min, int max) {
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
return input -> {
if (input == null) {
return true;
}
final int len = input.length();
return len >= min && len <= max;
};
}
}
}

View File

@ -60,8 +60,8 @@ public class ValidationException extends RuntimeException {
/**
* 创建一个验证失败异常
*
* @param errorMessageTemplate 错误信息模版
* @param errorMessageArgs 错误信息参数
* @param errorMessageTemplate 异常信息模版
* @param errorMessageArgs 异常信息参数
* @return 异常
*/
public static ValidationException withMessage(String errorMessageTemplate, Object... errorMessageArgs) {

View File

@ -65,8 +65,9 @@ public class ComparablePropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithComparableProperty(18, 10000000000L, MIN);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -156,89 +157,6 @@ public class ComparablePropertyValidatorTests {
// #endregion - not in the interval
// ================================
// ================================
// #region - null
// ================================
@Test
void inRange_default_valueIsNull() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE);
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
ValidationException e = assertThrows(
ValidationException.class,
() -> validator.validate(command));
final String expected = String.format("The input must in the interval %s. You entered null.", DATE_TIME_RANGE);
assertEquals(expected, e.getMessage());
}
@Test
void inRange_message_valueIsNull() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, MESSAGE);
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
ValidationException e = assertThrows(
ValidationException.class,
() -> validator.validate(command));
assertEquals(MESSAGE, e.getMessage());
}
@Test
void inRange_exceptionSupplier_valueIsNull() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, () -> ExampleException.withMessage(MESSAGE));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
assertEquals(MESSAGE, e.getMessage());
}
@Test
void inRange_exceptionFunction_valueIsNull() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForComparable(ExampleCommand::getDateTimeProperty)
.inRange(DATE_TIME_RANGE, property -> ExampleException.withMessage(
"The dateTimeProperty should in the interval [%s,%s), but it is %s", MIN, MAX, property));
}
};
ExampleCommand command = exampleCommandWithComparableProperty(null, null, null);
ExampleException e = assertThrows(
ExampleException.class,
() -> validator.validate(command));
final String expected = String.format("The dateTimeProperty should in the interval [%s,%s), but it is null", MIN, MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - null
// ================================
static ExampleCommand exampleCommandWithComparableProperty(
Integer intProperty,
Long longProperty,

View File

@ -18,7 +18,6 @@ package xyz.zhouxy.plusone.example.validator;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@ -61,8 +60,9 @@ public class DoublePropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithDoubleProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -147,80 +147,6 @@ public class DoublePropertyValidatorTests {
// #endregion - gt_invalidValue
// ================================
// ================================
// #region - gt_null
// ================================
@Test
void gt_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.gt(MIN);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage());
}
@Test
void gt_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.gt(MIN, MESSAGE_GT);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GT, e.getMessage());
}
@Test
void gt_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.gt(MIN, () -> ExampleException.withMessage(MESSAGE_GT));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GT, e.getMessage());
}
@Test
void gt_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.gt(MIN, property -> ExampleException.withMessage(
"The doubleProperty should be greater than %s, but it is %s", MIN, property));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The doubleProperty should be greater than %s, but it is null", MIN);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - gt_null
// ================================
// ================================
// #region - ge_validValue
// ================================
@ -243,8 +169,9 @@ public class DoublePropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithDoubleProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -329,80 +256,6 @@ public class DoublePropertyValidatorTests {
// #endregion - ge_invalidValue
// ================================
// ================================
// #region - ge_null
// ================================
@Test
void ge_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.ge(MIN);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage());
}
@Test
void ge_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.ge(MIN, MESSAGE_GE);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GE, e.getMessage());
}
@Test
void ge_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.ge(MIN, () -> ExampleException.withMessage(MESSAGE_GE));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GE, e.getMessage());
}
@Test
void ge_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.ge(MIN, property -> ExampleException.withMessage(
"The doubleProperty should be greater than or equal to %s, but it is %s", MIN, property));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The doubleProperty should be greater than or equal to %s, but it is null", MIN);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - ge_null
// ================================
// ================================
// #region - lt_validValue
// ================================
@ -425,8 +278,9 @@ public class DoublePropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithDoubleProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -511,80 +365,6 @@ public class DoublePropertyValidatorTests {
// #endregion - lt_invalidValue
// ================================
// ================================
// #region - lt_null
// ================================
@Test
void lt_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.lt(MAX);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage());
}
@Test
void lt_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.lt(MAX, MESSAGE_LT);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LT, e.getMessage());
}
@Test
void lt_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.lt(MAX, () -> ExampleException.withMessage(MESSAGE_LT));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LT, e.getMessage());
}
@Test
void lt_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.lt(MAX, property -> ExampleException.withMessage(
"The doubleProperty should be less than %s, but it is %s", MAX, property));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The doubleProperty should be less than %s, but it is null", MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - lt_null
// ================================
// ================================
// #region - le_validValue
// ================================
@ -607,8 +387,9 @@ public class DoublePropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithDoubleProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -693,80 +474,6 @@ public class DoublePropertyValidatorTests {
// #endregion - le_invalidValue
// ================================
// ================================
// #region - le_null
// ================================
@Test
void le_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.le(MAX);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage());
}
@Test
void le_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.le(MAX, MESSAGE_LE);
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LE, e.getMessage());
}
@Test
void le_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.le(MAX, () -> ExampleException.withMessage(MESSAGE_LE));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LE, e.getMessage());
}
@Test
void le_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForDouble(ExampleCommand::getDoubleProperty)
.le(MAX, property -> ExampleException.withMessage(
"The doubleProperty should be less than or equal to %s, but it is %s", MAX, property));
}
};
ExampleCommand command = exampleCommandWithDoubleProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The doubleProperty should be less than or equal to %s, but it is null", MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - le_null
// ================================
static ExampleCommand exampleCommandWithDoubleProperty(Double doubleProperty) {
ExampleCommand exampleCommand = new ExampleCommand();
exampleCommand.setDoubleProperty(doubleProperty);

View File

@ -17,7 +17,6 @@ package xyz.zhouxy.plusone.example.validator;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@ -60,8 +59,9 @@ public class IntPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithIntProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -146,80 +146,6 @@ public class IntPropertyValidatorTests {
// #endregion - gt_invalidValue
// ================================
// ================================
// #region - gt_null
// ================================
@Test
void gt_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.gt(MIN);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage());
}
@Test
void gt_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.gt(MIN, MESSAGE_GT);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GT, e.getMessage());
}
@Test
void gt_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.gt(MIN, () -> ExampleException.withMessage(MESSAGE_GT));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GT, e.getMessage());
}
@Test
void gt_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.gt(MIN, property -> ExampleException.withMessage(
"The intProperty should be greater than %d, but it is %d", MIN, property));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The intProperty should be greater than %d, but it is null", MIN);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - gt_null
// ================================
// ================================
// #region - ge_validValue
// ================================
@ -242,8 +168,9 @@ public class IntPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithIntProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -328,80 +255,6 @@ public class IntPropertyValidatorTests {
// #endregion - ge_invalidValue
// ================================
// ================================
// #region - ge_null
// ================================
@Test
void ge_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.ge(MIN);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage());
}
@Test
void ge_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.ge(MIN, MESSAGE_GE);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GE, e.getMessage());
}
@Test
void ge_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.ge(MIN, () -> ExampleException.withMessage(MESSAGE_GE));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GE, e.getMessage());
}
@Test
void ge_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.ge(MIN, property -> ExampleException.withMessage(
"The intProperty should be greater than or equal to %d, but it is %d", MIN, property));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The intProperty should be greater than or equal to %d, but it is null", MIN);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - ge_null
// ================================
// ================================
// #region - lt_validValue
// ================================
@ -424,8 +277,9 @@ public class IntPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithIntProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -510,80 +364,6 @@ public class IntPropertyValidatorTests {
// #endregion - lt_invalidValue
// ================================
// ================================
// #region - lt_null
// ================================
@Test
void lt_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.lt(MAX);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage());
}
@Test
void lt_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.lt(MAX, MESSAGE_LT);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LT, e.getMessage());
}
@Test
void lt_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.lt(MAX, () -> ExampleException.withMessage(MESSAGE_LT));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LT, e.getMessage());
}
@Test
void lt_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.lt(MAX, property -> ExampleException.withMessage(
"The intProperty should be less than %d, but it is %d", MAX, property));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The intProperty should be less than %d, but it is null", MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - lt_null
// ================================
// ================================
// #region - le_validValue
// ================================
@ -606,8 +386,9 @@ public class IntPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithIntProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -692,80 +473,6 @@ public class IntPropertyValidatorTests {
// #endregion - le_invalidValue
// ================================
// ================================
// #region - le_null
// ================================
@Test
void le_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.le(MAX);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage());
}
@Test
void le_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.le(MAX, MESSAGE_LE);
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LE, e.getMessage());
}
@Test
void le_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.le(MAX, () -> ExampleException.withMessage(MESSAGE_LE));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LE, e.getMessage());
}
@Test
void le_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForInt(ExampleCommand::getIntProperty)
.le(MAX, property -> ExampleException.withMessage(
"The intProperty should be less than or equal to %d, but it is %d", MAX, property));
}
};
ExampleCommand command = exampleCommandWithIntProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The intProperty should be less than or equal to %d, but it is null", MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - le_null
// ================================
static ExampleCommand exampleCommandWithIntProperty(Integer intProperty) {
ExampleCommand exampleCommand = new ExampleCommand();
exampleCommand.setIntProperty(intProperty);

View File

@ -17,7 +17,6 @@ package xyz.zhouxy.plusone.example.validator;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@ -61,8 +60,9 @@ public class LongPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithLongProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -147,80 +147,6 @@ public class LongPropertyValidatorTests {
// #endregion - gt_invalidValue
// ================================
// ================================
// #region - gt_null
// ================================
@Test
void gt_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.gt(MIN);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be greater than '%d'.", MIN), e.getMessage());
}
@Test
void gt_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.gt(MIN, MESSAGE_GT);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GT, e.getMessage());
}
@Test
void gt_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.gt(MIN, () -> ExampleException.withMessage(MESSAGE_GT));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GT, e.getMessage());
}
@Test
void gt_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.gt(MIN, property -> ExampleException.withMessage(
"The longProperty should be greater than %d, but it is %d", MIN, property));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The longProperty should be greater than %d, but it is null", MIN);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - gt_null
// ================================
// ================================
// #region - ge_validValue
// ================================
@ -243,8 +169,9 @@ public class LongPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithLongProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -329,80 +256,6 @@ public class LongPropertyValidatorTests {
// #endregion - ge_invalidValue
// ================================
// ================================
// #region - ge_null
// ================================
@Test
void ge_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.ge(MIN);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be greater than or equal to '%d'.", MIN), e.getMessage());
}
@Test
void ge_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.ge(MIN, MESSAGE_GE);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GE, e.getMessage());
}
@Test
void ge_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.ge(MIN, () -> ExampleException.withMessage(MESSAGE_GE));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_GE, e.getMessage());
}
@Test
void ge_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.ge(MIN, property -> ExampleException.withMessage(
"The longProperty should be greater than or equal to %d, but it is %d", MIN, property));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The longProperty should be greater than or equal to %d, but it is null", MIN);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - ge_null
// ================================
// ================================
// #region - lt_validValue
// ================================
@ -425,8 +278,9 @@ public class LongPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithLongProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -511,80 +365,6 @@ public class LongPropertyValidatorTests {
// #endregion - lt_invalidValue
// ================================
// ================================
// #region - lt_null
// ================================
@Test
void lt_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.lt(MAX);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be less than '%d'.", MAX), e.getMessage());
}
@Test
void lt_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.lt(MAX, MESSAGE_LT);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LT, e.getMessage());
}
@Test
void lt_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.lt(MAX, () -> ExampleException.withMessage(MESSAGE_LT));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LT, e.getMessage());
}
@Test
void lt_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.lt(MAX, property -> ExampleException.withMessage(
"The longProperty should be less than %d, but it is %d", MAX, property));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The longProperty should be less than %d, but it is null", MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - lt_null
// ================================
// ================================
// #region - le_validValue
// ================================
@ -607,8 +387,9 @@ public class LongPropertyValidatorTests {
};
ExampleCommand command = exampleCommandWithLongProperty(value);
assertDoesNotThrow(() -> validator.validate(command));
assertDoesNotThrow(() -> validator.validate(new ExampleCommand()));
}
// ================================
@ -693,80 +474,6 @@ public class LongPropertyValidatorTests {
// #endregion - le_invalidValue
// ================================
// ================================
// #region - le_null
// ================================
@Test
void le_default_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.le(MAX);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(String.format("The input must be less than or equal to '%d'.", MAX), e.getMessage());
}
@Test
void le_message_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.le(MAX, MESSAGE_LE);
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ValidationException e = assertThrows(
ValidationException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LE, e.getMessage());
}
@Test
void le_exceptionSupplier_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.le(MAX, () -> ExampleException.withMessage(MESSAGE_LE));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
assertEquals(MESSAGE_LE, e.getMessage());
}
@Test
void le_exceptionFunction_null() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForLong(ExampleCommand::getLongProperty)
.le(MAX, property -> ExampleException.withMessage(
"The longProperty should be less than or equal to %d, but it is %d", MAX, property));
}
};
ExampleCommand command = exampleCommandWithLongProperty(null);
ExampleException e = assertThrows(
ExampleException.class, () -> validator.validate(command));
final String expected = String.format("The longProperty should be less than or equal to %d, but it is null", MAX);
assertEquals(expected, e.getMessage());
}
// ================================
// #endregion - le_null
// ================================
static ExampleCommand exampleCommandWithLongProperty(Long longProperty) {
ExampleCommand exampleCommand = new ExampleCommand();
exampleCommand.setLongProperty(longProperty);

View File

@ -26,10 +26,10 @@ import java.util.Objects;
import org.junit.jupiter.api.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import xyz.zhouxy.plusone.ExampleException;
import xyz.zhouxy.plusone.commons.function.PredicateTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
import xyz.zhouxy.plusone.example.ExampleCommand;
import xyz.zhouxy.plusone.example.Foo;
@ -214,19 +214,19 @@ public class ObjectPropertyValidatorTests {
// ================================
// ================================
// #region - equalTo
// #region - equal
// ================================
@Test
void equalTo_validInput() {
void equal_validInput() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.equalTo("Foo")
.equalTo("Foo", "The stringProperty should be equal to 'Foo'.")
.equalTo("Foo", () ->
.equal("Foo")
.equal("Foo", "The stringProperty should be equal to 'Foo'.")
.equal("Foo", () ->
ExampleException.withMessage("The stringProperty should be equal to 'Foo'."))
.equalTo("Foo", str ->
.equal("Foo", str ->
ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str));
}
};
@ -240,13 +240,13 @@ public class ObjectPropertyValidatorTests {
}
@Test
void equalTo_invalidInput() {
void equal_invalidInput() {
ExampleCommand command = new ExampleCommand();
command.setStringProperty("Bar");
IValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo");
ruleForString(ExampleCommand::getStringProperty).equal("Foo");
}
};
ValidationException eWithDefaultMessage = assertThrows(
@ -255,7 +255,7 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
ruleForString(ExampleCommand::getStringProperty).equal("Foo",
"The stringProperty should be equal to 'Foo'.");
}
};
@ -265,7 +265,7 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
ruleForString(ExampleCommand::getStringProperty).equal("Foo",
() -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'."));
}
};
@ -275,7 +275,7 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
ruleForString(ExampleCommand::getStringProperty).equal("Foo",
str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str));
}
};
@ -285,7 +285,7 @@ public class ObjectPropertyValidatorTests {
}
// ================================
// #endregion - equalTo
// #endregion - equal
// ================================
// ================================
@ -440,10 +440,10 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")))
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), "The stringProperty must be equal to 'Foo'.")
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'."))
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str));
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals))
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), "The stringProperty must be equal to 'Foo'.")
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), () -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'."))
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals), str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str));
}
};
assertDoesNotThrow(() -> ruleWithDefaultMessage.validate(command));
@ -457,17 +457,17 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithDefaultMessage = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")));
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals));
}
};
ValidationException eWithDefaultMessage = assertThrows(
ValidationException.class, () -> ruleWithDefaultMessage.validate(command));
assertEquals("The specified conditions were not met for the input.", eWithDefaultMessage.getMessage());
assertEquals("The specified condition was not met for the input.", eWithDefaultMessage.getMessage());
IValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")),
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals),
"The stringProperty must be equal to 'Foo'.");
}
};
@ -478,7 +478,7 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")),
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals),
() -> ExampleException.withMessage("The stringProperty must be equal to 'Foo'."));
}
};
@ -489,7 +489,7 @@ public class ObjectPropertyValidatorTests {
IValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.must(ImmutableList.of(StringTools::isNotEmpty, str -> Objects.equals(str, "Foo")),
.must(PredicateTools.from(StringTools::isNotEmpty).and("Foo"::equals),
str -> ExampleException.withMessage("The stringProperty must be equal to 'Foo', but is was '%s'.", str));
}
};