Compare commits

...

3 Commits

Author SHA1 Message Date
ae970cb393 feat: 重载校验规则
`BasePropertyValidator#notNull`、`CollectionPropertyValidator#notEmpty` 和 `CollectionPropertyValidator#isEmpty` 提供无参的版本
2025-05-27 23:53:58 +08:00
a28a6135a8 refactor: 优化错误提示 2025-05-27 23:46:09 +08:00
9f2ade6252 refactor: 修改类型参数名称 2025-05-27 23:33:03 +08:00
10 changed files with 212 additions and 168 deletions

View File

@@ -21,19 +21,17 @@ import java.util.function.Supplier;
import com.google.common.collect.Range;
public abstract
class BaseComparablePropertyValidator<TObj,
TProperty extends Comparable<TProperty>,
TPropertyValidator extends BaseComparablePropertyValidator<TObj, TProperty, TPropertyValidator>>
extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
public abstract class BaseComparablePropertyValidator<T, TProperty extends Comparable<TProperty>, TPropertyValidator extends BaseComparablePropertyValidator<T, TProperty, TPropertyValidator>>
extends BasePropertyValidator<T, TProperty, TPropertyValidator> {
BaseComparablePropertyValidator(Function<TObj, ? extends TProperty> getter) {
BaseComparablePropertyValidator(Function<T, ? extends TProperty> getter) {
super(getter);
}
public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value),
convertExceptionCreator("The value is not in the interval " + range.toString()));
value -> new IllegalArgumentException(
String.format("The input must in the interval %s. You entered %s.", range, value)));
return thisObject();
}

View File

@@ -25,16 +25,13 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public abstract class BasePropertyValidator< //
TObj, //
TProperty, //
TPropertyValidator extends BasePropertyValidator<TObj, TProperty, TPropertyValidator>> {
public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator extends BasePropertyValidator<T, TProperty, TPropertyValidator>> {
private final Function<TObj, ? extends TProperty> getter;
private final Function<T, ? extends TProperty> getter;
private final List<Consumer<? super TProperty>> consumers = new LinkedList<>();
protected BasePropertyValidator(Function<TObj, ? extends TProperty> getter) {
protected BasePropertyValidator(Function<T, ? extends TProperty> getter) {
this.getter = getter;
}
@@ -62,7 +59,7 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
public final <T extends TObj> void validate(T obj) {
public final void validate(T obj) {
for (Consumer<? super TProperty> consumer : consumers) {
consumer.accept(getter.apply(obj));
}
@@ -74,10 +71,12 @@ public abstract class BasePropertyValidator< //
// ====== Object ======
// ====================
// ====== notNull =====
// ================================
// #region - notNull
// ================================
public TPropertyValidator notNull() {
return notNull("Value could not be null.");
return notNull("The input must not be null.");
}
public TPropertyValidator notNull(String errMsg) {
@@ -93,7 +92,17 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
// ====== isNull =====
// ================================
// #endregion - notNull
// ================================
// ================================
// #region - isNull
// ================================
public TPropertyValidator isNull() {
return isNull("The input must be null.");
}
public TPropertyValidator isNull(String errMsg) {
return isNull(convertExceptionCreator(errMsg));
@@ -108,10 +117,17 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
// ===== equals =====
// ================================
// #endregion - isNull
// ================================
// ================================
// #region - equals
// ================================
public TPropertyValidator equalsThat(Object that) {
return equalsThat(that, value -> new IllegalArgumentException(String.format("(%s) 必须与 (%s) 相等", value, that)));
return equalsThat(that,
value -> new IllegalArgumentException(String.format("The input must be equal to '%s'.", that)));
}
public TPropertyValidator equalsThat(Object that, String errMsg) {
@@ -129,10 +145,16 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
// ===== must =====
// ================================
// #endregion - equals
// ================================
// ================================
// #region - must
// ================================
public TPropertyValidator must(Predicate<? super TProperty> condition) {
return must(condition, "无效的用户输入");
return must(condition, "The specified condition was not met for the input.");
}
public TPropertyValidator must(Predicate<? super TProperty> condition, String errMsg) {
@@ -152,10 +174,8 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
// ===== must =====
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) {
return must(conditions, "无效的用户输入");
return must(conditions, "The specified conditions were not met for the input.");
}
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
@@ -177,6 +197,10 @@ public abstract class BasePropertyValidator< //
return thisObject();
}
// ================================
// #endregion - must
// ================================
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
}

View File

@@ -19,28 +19,28 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class BoolPropertyValidator<DTO> extends BasePropertyValidator<DTO, Boolean, BoolPropertyValidator<DTO>> {
public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
BoolPropertyValidator(Function<DTO, Boolean> getter) {
BoolPropertyValidator(Function<T, Boolean> getter) {
super(getter);
}
// ====== isTrueValue ======
public BoolPropertyValidator<DTO> isTrueValue() {
return isTrueValue("The value must be true.");
public BoolPropertyValidator<T> isTrueValue() {
return isTrueValue("The input must be true.");
}
public BoolPropertyValidator<DTO> isTrueValue(String errMsg) {
public BoolPropertyValidator<T> isTrueValue(String errMsg) {
return isTrueValue(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> BoolPropertyValidator<DTO> isTrueValue(
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
Supplier<E> exceptionCreator) {
return isTrueValue(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> BoolPropertyValidator<DTO> isTrueValue(
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
Function<Boolean, E> exceptionCreator) {
withRule(Boolean.TRUE::equals, exceptionCreator);
return this;
@@ -48,27 +48,27 @@ public class BoolPropertyValidator<DTO> extends BasePropertyValidator<DTO, Boole
// ====== isFalseValue ======
public BoolPropertyValidator<DTO> isFalseValue() {
return isFalseValue("The value must be false.");
public BoolPropertyValidator<T> isFalseValue() {
return isFalseValue("The input must be false.");
}
public BoolPropertyValidator<DTO> isFalseValue(String errMsg) {
public BoolPropertyValidator<T> isFalseValue(String errMsg) {
return isFalseValue(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> BoolPropertyValidator<DTO> isFalseValue(
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
Supplier<E> exceptionCreator) {
return isFalseValue(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> BoolPropertyValidator<DTO> isFalseValue(
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
Function<Boolean, E> exceptionCreator) {
withRule(Boolean.FALSE::equals, exceptionCreator);
return this;
}
@Override
protected BoolPropertyValidator<DTO> thisObject() {
protected BoolPropertyValidator<T> thisObject() {
return this;
}
}

View File

@@ -22,47 +22,57 @@ import java.util.function.Supplier;
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
public class CollectionPropertyValidator<DTO, T>
extends BasePropertyValidator<DTO, Collection<T>, CollectionPropertyValidator<DTO, T>> {
public class CollectionPropertyValidator<T, TElement>
extends BasePropertyValidator<T, Collection<TElement>, CollectionPropertyValidator<T, TElement>> {
CollectionPropertyValidator(Function<DTO, Collection<T>> getter) {
CollectionPropertyValidator(Function<T, Collection<TElement>> getter) {
super(getter);
}
// ====== notEmpty =====
public CollectionPropertyValidator<DTO, T> notEmpty(String errMsg) {
public CollectionPropertyValidator<T, TElement> notEmpty() {
return notEmpty("The input must not be empty.");
}
public CollectionPropertyValidator<T, TElement> notEmpty(String errMsg) {
return notEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> CollectionPropertyValidator<DTO, T> notEmpty(Supplier<E> exceptionCreator) {
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
Supplier<E> exceptionCreator) {
return notEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> CollectionPropertyValidator<DTO, T> notEmpty(
Function<Collection<T>, E> exceptionCreator) {
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> notEmpty(
Function<Collection<TElement>, E> exceptionCreator) {
withRule(CollectionTools::isNotEmpty, exceptionCreator);
return this;
}
// ====== isEmpty =====
public CollectionPropertyValidator<DTO, T> isEmpty(String errMsg) {
public CollectionPropertyValidator<T, TElement> isEmpty() {
return isEmpty("The input must be empty.");
}
public CollectionPropertyValidator<T, TElement> isEmpty(String errMsg) {
return isEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> CollectionPropertyValidator<DTO, T> isEmpty(Supplier<E> exceptionCreator) {
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
Supplier<E> exceptionCreator) {
return isEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> CollectionPropertyValidator<DTO, T> isEmpty(
Function<Collection<T>, E> exceptionCreator) {
public <E extends RuntimeException> CollectionPropertyValidator<T, TElement> isEmpty(
Function<Collection<TElement>, E> exceptionCreator) {
withRule(CollectionTools::isEmpty, exceptionCreator);
return this;
}
@Override
protected CollectionPropertyValidator<DTO, T> thisObject() {
protected CollectionPropertyValidator<T, TElement> thisObject() {
return this;
}
}

View File

@@ -18,15 +18,15 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
public class ComparablePropertyValidator<TObj, TProperty extends Comparable<TProperty>>
extends BaseComparablePropertyValidator<TObj, TProperty, ComparablePropertyValidator<TObj, TProperty>> {
public class ComparablePropertyValidator<T, TProperty extends Comparable<TProperty>>
extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> {
ComparablePropertyValidator(Function<TObj, ? extends TProperty> getter) {
ComparablePropertyValidator(Function<T, ? extends TProperty> getter) {
super(getter);
}
@Override
protected ComparablePropertyValidator<TObj, TProperty> thisObject() {
protected ComparablePropertyValidator<T, TProperty> thisObject() {
return this;
}
}

View File

@@ -19,10 +19,10 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class DoublePropertyValidator<DTO>
extends BaseComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
public class DoublePropertyValidator<T>
extends BaseComparablePropertyValidator<T, Double, DoublePropertyValidator<T>> {
DoublePropertyValidator(Function<DTO, Double> getter) {
DoublePropertyValidator(Function<T, Double> getter) {
super(getter);
}
@@ -30,20 +30,21 @@ public class DoublePropertyValidator<DTO>
// #region - greater than
// ================================
public DoublePropertyValidator<DTO> gt(double min) {
return gt(min, String.format("The value should be greater than %s", min));
public DoublePropertyValidator<T> gt(double min) {
return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%s'.", min)));
}
public DoublePropertyValidator<DTO> gt(double min, String errMsg) {
public DoublePropertyValidator<T> gt(double min, String errMsg) {
return gt(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> gt(
public <E extends RuntimeException> DoublePropertyValidator<T> gt(
double min, Supplier<E> exceptionCreator) {
return gt(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> gt(
public <E extends RuntimeException> DoublePropertyValidator<T> gt(
double min, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value > min), exceptionCreator);
return this;
@@ -57,20 +58,21 @@ public class DoublePropertyValidator<DTO>
// #region - greater than or equal to
// ================================
public DoublePropertyValidator<DTO> ge(double min) {
return ge(min, String.format("The value should be greater than or equal to %s", min));
public DoublePropertyValidator<T> ge(double min) {
return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%s'.", min)));
}
public DoublePropertyValidator<DTO> ge(double min, String errMsg) {
public DoublePropertyValidator<T> ge(double min, String errMsg) {
return ge(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> ge(
public <E extends RuntimeException> DoublePropertyValidator<T> ge(
double min, Supplier<E> exceptionCreator) {
return ge(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> ge(
public <E extends RuntimeException> DoublePropertyValidator<T> ge(
double min, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value >= min), exceptionCreator);
return this;
@@ -84,20 +86,21 @@ public class DoublePropertyValidator<DTO>
// #region - less than
// ================================
public DoublePropertyValidator<DTO> lt(double max) {
return lt(max, String.format("The value should be less than %s", max));
public DoublePropertyValidator<T> lt(double max) {
return lt(max, () -> new IllegalArgumentException(
String.format("The input must be less than '%s'.", max)));
}
public DoublePropertyValidator<DTO> lt(double max, String errMsg) {
public DoublePropertyValidator<T> lt(double max, String errMsg) {
return lt(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> lt(
public <E extends RuntimeException> DoublePropertyValidator<T> lt(
double max, Supplier<E> exceptionCreator) {
return lt(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> lt(
public <E extends RuntimeException> DoublePropertyValidator<T> lt(
double max, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value < max), exceptionCreator);
return this;
@@ -111,20 +114,21 @@ public class DoublePropertyValidator<DTO>
// #region - less than or equal to
// ================================
public DoublePropertyValidator<DTO> le(double max) {
return le(max, String.format("The value should be less than or equal to %s", max));
public DoublePropertyValidator<T> le(double max) {
return le(max, () -> new IllegalArgumentException(
String.format("The input must be less than or equal to '%s'.", max)));
}
public DoublePropertyValidator<DTO> le(double max, String errMsg) {
public DoublePropertyValidator<T> le(double max, String errMsg) {
return le(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> le(
public <E extends RuntimeException> DoublePropertyValidator<T> le(
double max, Supplier<E> exceptionCreator) {
return le(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> le(
public <E extends RuntimeException> DoublePropertyValidator<T> le(
double max, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value <= max), exceptionCreator);
return this;
@@ -135,7 +139,7 @@ public class DoublePropertyValidator<DTO>
// ================================
@Override
protected DoublePropertyValidator<DTO> thisObject() {
protected DoublePropertyValidator<T> thisObject() {
return this;
}
}

View File

@@ -19,10 +19,10 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class IntPropertyValidator<DTO>
extends BaseComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
public class IntPropertyValidator<T>
extends BaseComparablePropertyValidator<T, Integer, IntPropertyValidator<T>> {
IntPropertyValidator(Function<DTO, Integer> getter) {
IntPropertyValidator(Function<T, Integer> getter) {
super(getter);
}
@@ -30,20 +30,21 @@ public class IntPropertyValidator<DTO>
// #region - greater than
// ================================
public IntPropertyValidator<DTO> gt(int min) {
return gt(min, String.format("The value should be greater than %d", min));
public IntPropertyValidator<T> gt(int min) {
return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%d'.", min)));
}
public IntPropertyValidator<DTO> gt(int min, String errMsg) {
public IntPropertyValidator<T> gt(int min, String errMsg) {
return gt(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> gt(
public <E extends RuntimeException> IntPropertyValidator<T> gt(
int min, Supplier<E> exceptionCreator) {
return gt(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> gt(
public <E extends RuntimeException> IntPropertyValidator<T> gt(
int min, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value > min), exceptionCreator);
return this;
@@ -57,20 +58,21 @@ public class IntPropertyValidator<DTO>
// #region - greater than or equal to
// ================================
public IntPropertyValidator<DTO> ge(int min) {
return ge(min, String.format("The value should be greater than or equal to %d", min));
public IntPropertyValidator<T> ge(int min) {
return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%d'.", min)));
}
public IntPropertyValidator<DTO> ge(int min, String errMsg) {
public IntPropertyValidator<T> ge(int min, String errMsg) {
return ge(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> ge(
public <E extends RuntimeException> IntPropertyValidator<T> ge(
int min, Supplier<E> exceptionCreator) {
return ge(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> ge(
public <E extends RuntimeException> IntPropertyValidator<T> ge(
int min, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value >= min), exceptionCreator);
return this;
@@ -84,20 +86,21 @@ public class IntPropertyValidator<DTO>
// #region - less than
// ================================
public IntPropertyValidator<DTO> lt(int max) {
return lt(max, String.format("The value should be less than %d", max));
public IntPropertyValidator<T> lt(int max) {
return lt(max, () -> new IllegalArgumentException(
String.format("The input must be less than '%d'.", max)));
}
public IntPropertyValidator<DTO> lt(int max, String errMsg) {
public IntPropertyValidator<T> lt(int max, String errMsg) {
return lt(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> lt(
public <E extends RuntimeException> IntPropertyValidator<T> lt(
int max, Supplier<E> exceptionCreator) {
return lt(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> lt(
public <E extends RuntimeException> IntPropertyValidator<T> lt(
int max, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value < max), exceptionCreator);
return this;
@@ -111,20 +114,21 @@ public class IntPropertyValidator<DTO>
// #region - less than or equal to
// ================================
public IntPropertyValidator<DTO> le(int max) {
return le(max, String.format("The value should be less than or equal to %d", max));
public IntPropertyValidator<T> le(int max) {
return le(max, () -> new IllegalArgumentException(
String.format("The input must be less than or equal to '%d'.", max)));
}
public IntPropertyValidator<DTO> le(int max, String errMsg) {
public IntPropertyValidator<T> le(int max, String errMsg) {
return le(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> le(
public <E extends RuntimeException> IntPropertyValidator<T> le(
int max, Supplier<E> exceptionCreator) {
return le(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> le(
public <E extends RuntimeException> IntPropertyValidator<T> le(
int max, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value <= max), exceptionCreator);
return this;
@@ -135,7 +139,7 @@ public class IntPropertyValidator<DTO>
// ================================
@Override
protected IntPropertyValidator<DTO> thisObject() {
protected IntPropertyValidator<T> thisObject() {
return this;
}
}

View File

@@ -19,10 +19,10 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class LongPropertyValidator<DTO>
extends BaseComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
public class LongPropertyValidator<T>
extends BaseComparablePropertyValidator<T, Long, LongPropertyValidator<T>> {
LongPropertyValidator(Function<DTO, Long> getter) {
LongPropertyValidator(Function<T, Long> getter) {
super(getter);
}
@@ -30,20 +30,21 @@ public class LongPropertyValidator<DTO>
// #region - greater than
// ================================
public LongPropertyValidator<DTO> gt(long min) {
return gt(min, String.format("The value should be greater than %d", min));
public LongPropertyValidator<T> gt(long min) {
return gt(min, () -> new IllegalArgumentException(
String.format("The input must be greater than '%d'.", min)));
}
public LongPropertyValidator<DTO> gt(long min, String errMsg) {
public LongPropertyValidator<T> gt(long min, String errMsg) {
return gt(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> gt(
public <E extends RuntimeException> LongPropertyValidator<T> gt(
long min, Supplier<E> exceptionCreator) {
return gt(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> gt(
public <E extends RuntimeException> LongPropertyValidator<T> gt(
long min, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value > min), exceptionCreator);
return this;
@@ -57,20 +58,21 @@ public class LongPropertyValidator<DTO>
// #region - greater than or equal to
// ================================
public LongPropertyValidator<DTO> ge(long min) {
return ge(min, String.format("The value should be greater than or equal to %d", min));
public LongPropertyValidator<T> ge(long min) {
return ge(min, () -> new IllegalArgumentException(
String.format("The input must be greater than or equal to '%d'.", min)));
}
public LongPropertyValidator<DTO> ge(long min, String errMsg) {
public LongPropertyValidator<T> ge(long min, String errMsg) {
return ge(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> ge(
public <E extends RuntimeException> LongPropertyValidator<T> ge(
long min, Supplier<E> exceptionCreator) {
return ge(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> ge(
public <E extends RuntimeException> LongPropertyValidator<T> ge(
long min, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value >= min), exceptionCreator);
return this;
@@ -84,20 +86,21 @@ public class LongPropertyValidator<DTO>
// #region - less than
// ================================
public LongPropertyValidator<DTO> lt(long max) {
return lt(max, String.format("The value should be less than %d", max));
public LongPropertyValidator<T> lt(long max) {
return lt(max, () -> new IllegalArgumentException(
String.format("The input must be less than '%d'.", max)));
}
public LongPropertyValidator<DTO> lt(long max, String errMsg) {
public LongPropertyValidator<T> lt(long max, String errMsg) {
return lt(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> lt(
public <E extends RuntimeException> LongPropertyValidator<T> lt(
long max, Supplier<E> exceptionCreator) {
return lt(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> lt(
public <E extends RuntimeException> LongPropertyValidator<T> lt(
long max, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value < max), exceptionCreator);
return this;
@@ -111,20 +114,21 @@ public class LongPropertyValidator<DTO>
// #region - less than or equal to
// ================================
public LongPropertyValidator<DTO> le(long max) {
return le(max, String.format("The value should be less than or equal to %d", max));
public LongPropertyValidator<T> le(long max) {
return le(max, () -> new IllegalArgumentException(
String.format("The input must be less than or equal to '%d'.", max)));
}
public LongPropertyValidator<DTO> le(long max, String errMsg) {
public LongPropertyValidator<T> le(long max, String errMsg) {
return le(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> le(
public <E extends RuntimeException> LongPropertyValidator<T> le(
long max, Supplier<E> exceptionCreator) {
return le(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> le(
public <E extends RuntimeException> LongPropertyValidator<T> le(
long max, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value <= max), exceptionCreator);
return this;
@@ -135,7 +139,7 @@ public class LongPropertyValidator<DTO>
// ================================
@Override
protected LongPropertyValidator<DTO> thisObject() {
protected LongPropertyValidator<T> thisObject() {
return this;
}
}

View File

@@ -18,14 +18,14 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
public class ObjectPropertyValidator<DTO, T> extends BasePropertyValidator<DTO, T, ObjectPropertyValidator<DTO, T>> {
public class ObjectPropertyValidator<T, TProperty> extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> {
ObjectPropertyValidator(Function<DTO, T> getter) {
ObjectPropertyValidator(Function<T, TProperty> getter) {
super(getter);
}
@Override
protected ObjectPropertyValidator<DTO, T> thisObject() {
protected ObjectPropertyValidator<T, TProperty> thisObject() {
return this;
}
}

View File

@@ -36,9 +36,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, String, StringPropertyValidator<DTO>> {
public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<T, String, StringPropertyValidator<T>> {
StringPropertyValidator(Function<DTO, String> getter) {
StringPropertyValidator(Function<T, String> getter) {
super(getter);
}
@@ -46,17 +46,17 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - matches
// ================================
public StringPropertyValidator<DTO> matches(Pattern regex, String errMsg) {
public StringPropertyValidator<T> matches(Pattern regex, String errMsg) {
return matches(regex, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matches(
public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex,
Supplier<E> exceptionCreator) {
return matches(regex, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matches(
public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex,
Function<String, E> exceptionCreator) {
withRule(input -> (input == null || RegexTools.matches(input, regex)), exceptionCreator);
@@ -71,34 +71,34 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - matchesOne
// ================================
public StringPropertyValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
public StringPropertyValidator<T> matchesOne(Pattern[] regexs, String errMsg) {
return matchesOne(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesOne(
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs,
Supplier<E> exceptionCreator) {
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesOne(
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matchesOne(input, regexs), exceptionCreator);
return this;
}
public StringPropertyValidator<DTO> matchesOne(List<Pattern> regexs, String errMsg) {
public StringPropertyValidator<T> matchesOne(List<Pattern> regexs, String errMsg) {
return matchesOne(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesOne(
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs,
Supplier<E> exceptionCreator) {
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesOne(
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs,
Function<String, E> exceptionCreator) {
withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
@@ -113,34 +113,34 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - matchesAll
// ================================
public StringPropertyValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
public StringPropertyValidator<T> matchesAll(Pattern[] regexs, String errMsg) {
return matchesAll(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesAll(
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs,
Supplier<E> exceptionCreator) {
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesAll(
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matchesAll(input, regexs), exceptionCreator);
return this;
}
public StringPropertyValidator<DTO> matchesAll(Collection<Pattern> regexs, String errMsg) {
public StringPropertyValidator<T> matchesAll(Collection<Pattern> regexs, String errMsg) {
return matchesAll(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesAll(
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs,
Supplier<E> exceptionCreator) {
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> matchesAll(
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs,
Function<String, E> exceptionCreator) {
withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
@@ -155,19 +155,19 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - notBlank
// ================================
public StringPropertyValidator<DTO> notBlank() {
return notBlank("The value must have text; it must not be null, empty, or blank.");
public StringPropertyValidator<T> notBlank() {
return notBlank("The input must not be blank.");
}
public StringPropertyValidator<DTO> notBlank(String errMsg) {
public StringPropertyValidator<T> notBlank(String errMsg) {
return notBlank(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> notBlank(Supplier<E> exceptionCreator) {
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Supplier<E> exceptionCreator) {
return notBlank(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> notBlank(
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(
Function<String, E> exceptionCreator) {
withRule(StringTools::isNotBlank, exceptionCreator);
return this;
@@ -181,20 +181,20 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - emailAddress
// ================================
public StringPropertyValidator<DTO> emailAddress() {
return emailAddress("The value is not an email address.");
public StringPropertyValidator<T> emailAddress() {
return emailAddress("The input is not a valid email address.");
}
public StringPropertyValidator<DTO> emailAddress(String errMsg) {
public StringPropertyValidator<T> emailAddress(String errMsg) {
return emailAddress(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> emailAddress(
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(
Supplier<E> exceptionCreator) {
return emailAddress(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> emailAddress(
public <E extends RuntimeException> StringPropertyValidator<T> emailAddress(
Function<String, E> exceptionCreator) {
return matches(PatternConsts.EMAIL, exceptionCreator);
}
@@ -207,19 +207,19 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - notEmpty
// ================================
public StringPropertyValidator<DTO> notEmpty() {
return notEmpty("The value must not be empty.");
public StringPropertyValidator<T> notEmpty() {
return notEmpty("The input must not be empty.");
}
public StringPropertyValidator<DTO> notEmpty(String errMsg) {
public StringPropertyValidator<T> notEmpty(String errMsg) {
return notEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> notEmpty(Supplier<E> exceptionCreator) {
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(Supplier<E> exceptionCreator) {
return notEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> notEmpty(
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(
Function<String, E> exceptionCreator) {
withRule(s -> s != null && !s.isEmpty(), exceptionCreator);
return this;
@@ -233,19 +233,19 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// #region - length
// ================================
public StringPropertyValidator<DTO> length(int length, String errMsg) {
public StringPropertyValidator<T> length(int length, String errMsg) {
return length(length, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> length(int length,
public <E extends RuntimeException> StringPropertyValidator<T> length(int length,
Supplier<E> exceptionCreator) {
return length(length, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> length(int length,
public <E extends RuntimeException> StringPropertyValidator<T> length(int length,
Function<String, E> exceptionCreator) {
AssertTools.checkArgument(length >= 0,
"The required length must be greater than or equal to 0.");
"The expected length must be greater than or equal to 0.");
withRule(s -> s == null || s.length() == length, exceptionCreator);
return this;
}
@@ -258,16 +258,16 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
return len >= min && len <= max;
}
public StringPropertyValidator<DTO> length(int min, int max, String errMsg) {
public StringPropertyValidator<T> length(int min, int max, String errMsg) {
return length(min, max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> length(int min, int max,
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
Supplier<E> exceptionCreator) {
return length(min, max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringPropertyValidator<DTO> length(int min, int max,
public <E extends RuntimeException> StringPropertyValidator<T> length(int min, int max,
Function<String, E> exceptionCreator) {
AssertTools.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
AssertTools.checkArgument(min < max, "The minimum value must be less than the maximum value.");
@@ -280,7 +280,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
// ================================
@Override
protected StringPropertyValidator<DTO> thisObject() {
protected StringPropertyValidator<T> thisObject() {
return this;
}
}