diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java
index 52ba86f..a88cf26 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java
@@ -21,6 +21,18 @@ import java.util.function.Supplier;
import com.google.common.collect.Range;
+/**
+ * 针对 {@code Comparable} 类型的属性校验器基类
+ *
+ *
+ * 内置了判断属性是否在给定区间内的校验规则。
+ *
+ * @param 待校验对象类型
+ * @param 属性类型
+ * @param 当前属性校验器类型,用于链式调用
+ * @see Range
+ * @author ZhouXY
+ */
public abstract class BaseComparablePropertyValidator, TPropertyValidator extends BaseComparablePropertyValidator>
extends BasePropertyValidator {
@@ -28,6 +40,12 @@ public abstract class BaseComparablePropertyValidator range) {
withRule(value -> value != null && range.contains(value),
value -> new IllegalArgumentException(
@@ -35,30 +53,41 @@ public abstract class BaseComparablePropertyValidator range, String errMsg) {
- withRule(value -> value != null && range.contains(value), convertExceptionCreator(errMsg));
+ withRule(value -> value != null && range.contains(value), convertToExceptionFunction(errMsg));
return thisObject();
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否在给定的区间之内
+ *
+ * @param range 区间
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator inRange(
- Range range,
- Supplier exceptionCreator) {
- withRule(value -> value != null && range.contains(value), exceptionCreator);
+ Range range, Supplier e) {
+ withRule(value -> value != null && range.contains(value), e);
return thisObject();
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否在给定的区间之内
+ *
+ * @param range 区间
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator inRange(
- Range range,
- Function exceptionCreator) {
- withRule(value -> value != null && range.contains(value), exceptionCreator);
+ Range range, Function e) {
+ withRule(value -> value != null && range.contains(value), e);
return thisObject();
}
-
- static Function convertExceptionCreator(String errMsg) {
- return value -> new IllegalArgumentException(errMsg);
- }
-
- static Function convertExceptionCreator(Supplier exceptionSupplier) {
- return value -> exceptionSupplier.get();
- }
}
diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java
index 3f511be..34dd961 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java
@@ -25,6 +25,15 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
+/**
+ * 属性校验器。包含针对属性的校验规则。
+ *
+ *
+ * 内置基础的校验规则。
+ *
+ *
+ * @author ZhouXY
+ */
public abstract class BasePropertyValidator> {
private final Function getter;
@@ -35,20 +44,47 @@ public abstract class BasePropertyValidator rule) {
return withRule(rule, v -> new IllegalArgumentException());
}
+ /**
+ * 添加一条校验属性的规则
+ *
+ * @param rule 校验规则
+ * @param errMsg 校验失败的错误信息
+ * @return 属性校验器
+ */
public final TPropertyValidator withRule(
Predicate super TProperty> rule, String errMsg) {
- return withRule(rule, convertExceptionCreator(errMsg));
+ return withRule(rule, convertToExceptionFunction(errMsg));
}
+ /**
+ * 添加一条校验属性的规则
+ *
+ * @param rule 校验规则
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public final TPropertyValidator withRule(
Predicate super TProperty> rule, Supplier e) {
- return withRule(rule, convertExceptionCreator(e));
+ return withRule(rule, convertToExceptionFunction(e));
}
+ /**
+ * 添加一条校验属性的规则
+ *
+ * @param rule 校验规则
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public final TPropertyValidator withRule(
Predicate super TProperty> rule, Function e) {
this.consumers.add(v -> {
@@ -59,9 +95,13 @@ public abstract class BasePropertyValidator consumer : consumers) {
- consumer.accept(getter.apply(obj));
+ consumer.accept(getter.apply(propertyValue));
}
}
@@ -75,20 +115,45 @@ public abstract class BasePropertyValidator TPropertyValidator notNull(Supplier exceptionCreator) {
- return notNull(convertExceptionCreator(exceptionCreator));
+ /**
+ * 添加一条校验属性的规则,校验属性是否不为空
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public TPropertyValidator notNull(Supplier e) {
+ return notNull(convertToExceptionFunction(e));
}
- public TPropertyValidator notNull(Function exceptionCreator) {
- withRule(Objects::nonNull, exceptionCreator);
+ /**
+ * 添加一条校验属性的规则,校验属性是否不为空
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public TPropertyValidator notNull(Function e) {
+ withRule(Objects::nonNull, e);
return thisObject();
}
@@ -100,20 +165,45 @@ public abstract class BasePropertyValidator TPropertyValidator isNull(Supplier exceptionCreator) {
- return isNull(convertExceptionCreator(exceptionCreator));
+ /**
+ * 添加一条校验属性的规则,校验属性是否为空
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public TPropertyValidator isNull(Supplier e) {
+ return isNull(convertToExceptionFunction(e));
}
- public TPropertyValidator isNull(Function exceptionCreator) {
- withRule(Objects::isNull, exceptionCreator);
+ /**
+ * 添加一条校验属性的规则,校验属性是否为空
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public TPropertyValidator isNull(Function e) {
+ withRule(Objects::isNull, e);
return thisObject();
}
@@ -125,23 +215,52 @@ public abstract class BasePropertyValidator new IllegalArgumentException(String.format("The input must be equal to '%s'.", that)));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否等于给定值
+ *
+ * @param that 给定值
+ * @param errMsg 错误信息
+ * @return 属性校验器
+ */
public TPropertyValidator equalTo(Object that, String errMsg) {
- return equalTo(that, convertExceptionCreator(errMsg));
+ return equalTo(that, convertToExceptionFunction(errMsg));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否等于给定值
+ *
+ * @param 自定义异常类型
+ * @param that 给定值
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator equalTo(
- Object that, Supplier exceptionCreator) {
- return equalTo(that, convertExceptionCreator(exceptionCreator));
+ Object that, Supplier e) {
+ return equalTo(that, convertToExceptionFunction(e));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否等于给定值
+ *
+ * @param 自定义异常类型
+ * @param that 给定值
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator equalTo(
- Object that, Function exceptionCreator) {
- withRule(value -> Objects.equals(value, that), exceptionCreator);
+ Object that, Function e) {
+ withRule(value -> Objects.equals(value, that), e);
return thisObject();
}
@@ -153,46 +272,103 @@ public abstract class BasePropertyValidator condition) {
return must(condition, "The specified condition was not met for the input.");
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否满足给定的条件
+ *
+ * @param condition 校验规则
+ * @param errMsg 错误信息
+ * @return 属性校验器
+ */
public TPropertyValidator must(Predicate super TProperty> condition, String errMsg) {
- return must(condition, convertExceptionCreator(errMsg));
+ return must(condition, convertToExceptionFunction(errMsg));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否满足给定的条件
+ *
+ * @param 自定义异常类型
+ * @param condition 校验规则
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator must(
Predicate super TProperty> condition,
- Supplier exceptionCreator) {
- return must(condition, convertExceptionCreator(exceptionCreator));
+ Supplier e) {
+ return must(condition, convertToExceptionFunction(e));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否满足给定的条件
+ *
+ * @param 自定义异常类型
+ * @param condition 校验规则
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator must(
Predicate super TProperty> condition,
- Function exceptionCreator) {
- withRule(condition, exceptionCreator);
+ Function e) {
+ withRule(condition, e);
return thisObject();
}
+ /**
+ * 添加多条校验属性的规则,校验属性是否满足给定的所有条件
+ *
+ * @param conditions 校验规则
+ * @return 属性校验器
+ */
public TPropertyValidator must(Collection> conditions) {
return must(conditions, "The specified conditions were not met for the input.");
}
+ /**
+ * 添加多条校验属性的规则,校验属性是否满足给定的所有条件
+ *
+ * @param conditions 校验规则
+ * @param errMsg 错误信息
+ * @return 属性校验器
+ */
public TPropertyValidator must(Collection> conditions, String errMsg) {
- return must(conditions, convertExceptionCreator(errMsg));
+ return must(conditions, convertToExceptionFunction(errMsg));
}
+ /**
+ * 添加多条校验属性的规则,校验属性是否满足给定的所有条件
+ *
+ * @param 自定义异常类型
+ * @param conditions 校验规则
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator must(
- Collection> conditions,
- Supplier exceptionCreator) {
- return must(conditions, convertExceptionCreator(exceptionCreator));
+ Collection> conditions, Supplier e) {
+ return must(conditions, convertToExceptionFunction(e));
}
+ /**
+ * 添加多条校验属性的规则,校验属性是否满足给定的所有条件
+ *
+ * @param 自定义异常类型
+ * @param conditions 校验规则
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public TPropertyValidator must(
Collection> conditions,
- Function exceptionCreator) {
+ Function e) {
for (Predicate super TProperty> condition : conditions) {
- withRule(condition, exceptionCreator);
+ withRule(condition, e);
}
return thisObject();
}
@@ -201,11 +377,12 @@ public abstract class BasePropertyValidator Function convertExceptionCreator(String errMsg) {
+ static Function convertToExceptionFunction(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
}
- static Function convertExceptionCreator(Supplier exceptionSupplier) {
+ static Function convertToExceptionFunction(
+ Supplier exceptionSupplier) {
return value -> exceptionSupplier.get();
}
}
diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
index 7bd1f61..bcb95ea 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
@@ -27,120 +27,226 @@ import java.util.function.Supplier;
import xyz.zhouxy.plusone.validator.function.*;
/**
- * BaseValidator
- *
- *
* 校验器的基类
- *
*
*
- * NOTE: content.
- *
+ * 通过继承 {@code BaseValidator},可以自定义一个针对特定类型的校验器,包含对该类型的校验逻辑。
+ *
* @author ZhouXY
- * @since 0.0.1
*/
public abstract class BaseValidator implements IValidator {
+
+ /**
+ * 规则集合
+ */
private final List> rules = new ArrayList<>();
+ /**
+ * 添加一个校验规则
+ *
+ * @param rule 校验规则
+ * @param errorMessage 错误信息
+ */
protected final void withRule(final Predicate super T> rule, final String errorMessage) {
withRule(rule, () -> new IllegalArgumentException(errorMessage));
}
- protected final void withRule(Predicate super T> rule, Supplier exceptionBuilder) {
- withRule(rule, value -> exceptionBuilder.get());
+ /**
+ * 添加一个校验规则
+ *
+ * @param 自定义异常类型
+ * @param rule 校验规则
+ * @param e 自定义异常
+ */
+ protected final void withRule(
+ final Predicate super T> rule, final Supplier e) {
+ withRule(rule, value -> e.get());
}
+ /**
+ * 添加一个校验规则
+ *
+ * @param 自定义异常类型
+ * @param condition 校验条件
+ * @param e 自定义异常
+ */
protected final void withRule(
- Predicate super T> condition, Function exceptionBuilder) {
+ final Predicate super T> condition, final Function e) {
withRule(value -> {
if (!condition.test(value)) {
- throw exceptionBuilder.apply(value);
+ throw e.apply(value);
}
});
}
+ /**
+ * 添加一个校验规则
+ *
+ * @param rule 校验规则。内部包含断言条件,如果条件不满足,则抛出异常。
+ */
protected final void withRule(Consumer super T> rule) {
this.rules.add(rule);
}
+ /**
+ * 添加一个属性校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final ObjectPropertyValidator ruleFor(Function getter) {
ObjectPropertyValidator validator = new ObjectPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
- protected final > ComparablePropertyValidator ruleForComparable(Function getter) {
+ /**
+ * 添加一个针对 {@code Comparable} 属性的校验器
+ *
+ * @param 属性类型
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
+ protected final > ComparablePropertyValidator ruleForComparable(
+ Function getter) {
ComparablePropertyValidator validator = new ComparablePropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Integer} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final IntPropertyValidator ruleForInt(Function getter) {
IntPropertyValidator validator = new IntPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Integer} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final IntPropertyValidator ruleFor(ToIntegerFunction getter) {
IntPropertyValidator validator = new IntPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Long} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final LongPropertyValidator ruleForLong(Function getter) {
LongPropertyValidator validator = new LongPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Long} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final LongPropertyValidator ruleFor(ToLongObjectFunction getter) {
LongPropertyValidator validator = new LongPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Double} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final DoublePropertyValidator ruleForDouble(Function getter) {
DoublePropertyValidator validator = new DoublePropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Double} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final DoublePropertyValidator ruleFor(ToDoubleObjectFunction getter) {
DoublePropertyValidator validator = new DoublePropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Boolean} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final BoolPropertyValidator ruleForBool(Function getter) {
BoolPropertyValidator validator = new BoolPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Boolean} 属性的校验器
+ *
+ * @param getter 属性获取函数
+ * @return 属性校验器
+ */
protected final BoolPropertyValidator ruleFor(ToBoolObjectFunction getter) {
BoolPropertyValidator validator = new BoolPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code String} 属性的校验器
+ *
+ * @param getter 获取属性值的函数
+ * @return 属性校验器
+ */
protected final StringPropertyValidator ruleForString(Function getter) {
StringPropertyValidator validator = new StringPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code String} 属性的校验器
+ *
+ * @param getter 获取属性值的函数
+ * @return 属性校验器
+ */
protected final StringPropertyValidator ruleFor(ToStringFunction getter) {
StringPropertyValidator validator = new StringPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /**
+ * 添加一个针对 {@code Collection} 属性的校验器
+ *
+ * @param getter 获取属性值的函数
+ * @return 集合属性校验器
+ */
protected final CollectionPropertyValidator ruleForCollection(Function> getter) {
CollectionPropertyValidator validator = new CollectionPropertyValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
+ /** {@inheritDoc} */
@Override
public void validate(T obj) {
this.rules.forEach(rule -> rule.accept(obj));
diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java
index 2357381..b2bfc5c 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java
@@ -19,6 +19,15 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
+/**
+ * 针对 {@code Boolean} 类型的属性校验器
+ *
+ *
+ * 内置了判断 Boolean 值是否为 true 或 false 的校验规则。
+ *
+ *
+ * @author ZhouXY
+ */
public class BoolPropertyValidator extends BasePropertyValidator> {
BoolPropertyValidator(Function getter) {
@@ -27,43 +36,89 @@ public class BoolPropertyValidator extends BasePropertyValidator isTrueValue() {
return isTrueValue("The input must be true.");
}
+ /**
+ * 添加一条判断属性值是否为 {@code true} 的校验规则
+ *
+ * @param errMsg 校验失败的错误信息
+ * @return 属性校验器
+ */
public BoolPropertyValidator isTrueValue(String errMsg) {
- return isTrueValue(convertExceptionCreator(errMsg));
+ return isTrueValue(convertToExceptionFunction(errMsg));
}
- public BoolPropertyValidator isTrueValue(
- Supplier exceptionCreator) {
- return isTrueValue(convertExceptionCreator(exceptionCreator));
+ /**
+ * 添加一条判断属性值是否为 {@code true} 的校验规则
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public BoolPropertyValidator isTrueValue(Supplier e) {
+ return isTrueValue(convertToExceptionFunction(e));
}
- public BoolPropertyValidator isTrueValue(
- Function exceptionCreator) {
- withRule(Boolean.TRUE::equals, exceptionCreator);
+ /**
+ * 添加一条判断属性值是否为 {@code true} 的校验规则
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public BoolPropertyValidator isTrueValue(Function e) {
+ withRule(Boolean.TRUE::equals, e);
return this;
}
// ====== isFalseValue ======
+ /**
+ * 添加一条判断属性值是否为 {@code false} 的校验规则
+ *
+ * @return 属性校验器
+ */
public BoolPropertyValidator isFalseValue() {
return isFalseValue("The input must be false.");
}
+ /**
+ * 添加一条判断属性值是否为 {@code false} 的校验规则
+ *
+ * @param errMsg 错误信息
+ * @return 属性校验器
+ */
public BoolPropertyValidator isFalseValue(String errMsg) {
- return isFalseValue(convertExceptionCreator(errMsg));
+ return isFalseValue(convertToExceptionFunction(errMsg));
}
- public BoolPropertyValidator isFalseValue(
- Supplier exceptionCreator) {
- return isFalseValue(convertExceptionCreator(exceptionCreator));
+ /**
+ * 添加一条判断属性值是否为 {@code false} 的校验规则
+ *
+ * @param 异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public BoolPropertyValidator isFalseValue(Supplier e) {
+ return isFalseValue(convertToExceptionFunction(e));
}
- public BoolPropertyValidator isFalseValue(
- Function exceptionCreator) {
- withRule(Boolean.FALSE::equals, exceptionCreator);
+ /**
+ * 添加一条判断属性值是否为 {@code false} 的校验规则
+ *
+ * @param 异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public BoolPropertyValidator isFalseValue(Function e) {
+ withRule(Boolean.FALSE::equals, e);
return this;
}
diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java
index 8b657e4..e5d42fc 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java
@@ -22,6 +22,14 @@ import java.util.function.Supplier;
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
+/**
+ * 针对集合类型的属性校验器
+ *
+ *
+ * 内置判断集合是否为空的校验规则。
+ *
+ * @author ZhouXY
+ */
public class CollectionPropertyValidator
extends BasePropertyValidator, CollectionPropertyValidator> {
@@ -31,43 +39,91 @@ public class CollectionPropertyValidator
// ====== notEmpty =====
+ /**
+ * 添加一条校验属性的规则,校验属性是否非空
+ *
+ * @return 属性校验器
+ */
public CollectionPropertyValidator notEmpty() {
return notEmpty("The input must not be empty.");
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否非空
+ *
+ * @param errMsg 异常信息
+ * @return 属性校验器
+ */
public CollectionPropertyValidator notEmpty(String errMsg) {
- return notEmpty(convertExceptionCreator(errMsg));
+ return notEmpty(convertToExceptionFunction(errMsg));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否非空
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public CollectionPropertyValidator notEmpty(
- Supplier exceptionCreator) {
- return notEmpty(convertExceptionCreator(exceptionCreator));
+ Supplier e) {
+ return notEmpty(convertToExceptionFunction(e));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否非空
+ *
+ * @param 自定义异常类型
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public CollectionPropertyValidator notEmpty(
- Function, E> exceptionCreator) {
- withRule(CollectionTools::isNotEmpty, exceptionCreator);
+ Function, E> e) {
+ withRule(CollectionTools::isNotEmpty, e);
return this;
}
// ====== isEmpty =====
+ /**
+ * 添加一条校验属性的规则,校验属性是否为空
+ *
+ * @return 属性校验器
+ */
public CollectionPropertyValidator isEmpty() {
return isEmpty("The input must be empty.");
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否为空
+ *
+ * @param errMsg 异常信息
+ * @return 属性校验器
+ */
public CollectionPropertyValidator isEmpty(String errMsg) {
- return isEmpty(convertExceptionCreator(errMsg));
+ return isEmpty(convertToExceptionFunction(errMsg));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否为空
+ *
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public CollectionPropertyValidator isEmpty(
- Supplier exceptionCreator) {
- return isEmpty(convertExceptionCreator(exceptionCreator));
+ Supplier e) {
+ return isEmpty(convertToExceptionFunction(e));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否为空
+ *
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
public CollectionPropertyValidator isEmpty(
- Function, E> exceptionCreator) {
- withRule(CollectionTools::isEmpty, exceptionCreator);
+ Function, E> e) {
+ withRule(CollectionTools::isEmpty, e);
return this;
}
diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java
index 3fc8820..63621db 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java
@@ -18,6 +18,18 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
+/**
+ * 针对 {@code Comparable} 类型的默认属性校验器
+ *
+ *
+ * 继承自 {@link BaseComparablePropertyValidator},内置了判断属性是否在给定区间内的校验规则。
+ *
+ * @param 待校验对象类型
+ * @param 属性类型
+ * @param 当前属性校验器类型,用于链式调用
+ * @see Range
+ * @author ZhouXY
+ */
public class ComparablePropertyValidator>
extends BaseComparablePropertyValidator> {
diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java
index c48a8f5..8c37068 100644
--- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java
+++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java
@@ -19,6 +19,14 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
+/**
+ * 浮点数属性校验器
+ *
+ *
+ * 内置对 {@code Double} 类型常用的校验规则。
+ *
+ * @author ZhouXY
+ */
public class DoublePropertyValidator
extends BaseComparablePropertyValidator> {
@@ -30,23 +38,50 @@ public class DoublePropertyValidator
// #region - greater than
// ================================
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于给定值
+ *
+ * @param min 最小值
+ * @return 属性校验器
+ */
public DoublePropertyValidator gt(double min) {
return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%s'.", min)));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于给定值
+ *
+ * @param min 最小值
+ * @param errMsg 错误信息
+ * @return 属性校验器
+ */
public DoublePropertyValidator gt(double min, String errMsg) {
- return gt(min, convertExceptionCreator(errMsg));
+ return gt(min, convertToExceptionFunction(errMsg));
}
- public DoublePropertyValidator gt(
- double min, Supplier exceptionCreator) {
- return gt(min, convertExceptionCreator(exceptionCreator));
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于给定值
+ *
+ * @param 异常类型
+ * @param min 最小值
+ * @param e 错误信息
+ * @return 属性校验器
+ */
+ public DoublePropertyValidator gt(double min, Supplier e) {
+ return gt(min, convertToExceptionFunction(e));
}
- public DoublePropertyValidator gt(
- double min, Function exceptionCreator) {
- withRule(value -> (value != null && value > min), exceptionCreator);
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于给定值
+ *
+ * @param 异常类型
+ * @param min 最小值
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public DoublePropertyValidator gt(double min, Function e) {
+ withRule(value -> (value != null && value > min), e);
return this;
}
@@ -58,23 +93,50 @@ public class DoublePropertyValidator
// #region - greater than or equal to
// ================================
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于等于给定值
+ *
+ * @param min 最小值
+ * @return 属性校验器
+ */
public DoublePropertyValidator ge(double min) {
return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%s'.", min)));
}
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于等于给定值
+ *
+ * @param min 最小值
+ * @param errMsg 错误信息
+ * @return 属性校验器
+ */
public DoublePropertyValidator ge(double min, String errMsg) {
- return ge(min, convertExceptionCreator(errMsg));
+ return ge(min, convertToExceptionFunction(errMsg));
}
- public DoublePropertyValidator ge(
- double min, Supplier exceptionCreator) {
- return ge(min, convertExceptionCreator(exceptionCreator));
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于等于给定值
+ *
+ * @param 异常类型
+ * @param min 最小值
+ * @param e 自定义异常
+ * @return 属性校验器
+ */
+ public DoublePropertyValidator ge(double min, Supplier e) {
+ return ge(min, convertToExceptionFunction(e));
}
- public DoublePropertyValidator ge(
- double min, Function exceptionCreator) {
- withRule(value -> (value != null && value >= min), exceptionCreator);
+ /**
+ * 添加一条校验属性的规则,校验属性是否大于等于给定值
+ *
+ * @param