fix: 修复 ruleForComparable
无法指定具体类型参数的 BUG
#19
@@ -33,7 +33,7 @@ import com.google.common.collect.Range;
|
|||||||
*/
|
*/
|
||||||
public abstract class BaseComparablePropertyValidator<
|
public abstract class BaseComparablePropertyValidator<
|
||||||
T,
|
T,
|
||||||
TProperty extends Comparable<TProperty>,
|
TProperty extends Comparable<? super TProperty>,
|
||||||
TPropertyValidator extends BaseComparablePropertyValidator<T, TProperty, TPropertyValidator>>
|
TPropertyValidator extends BaseComparablePropertyValidator<T, TProperty, TPropertyValidator>>
|
||||||
extends BasePropertyValidator<T, TProperty, TPropertyValidator> {
|
extends BasePropertyValidator<T, TProperty, TPropertyValidator> {
|
||||||
|
|
||||||
@@ -47,9 +47,9 @@ public abstract class BaseComparablePropertyValidator<
|
|||||||
* @param range 区间
|
* @param range 区间
|
||||||
* @return 当前校验器实例,用于链式调用
|
* @return 当前校验器实例,用于链式调用
|
||||||
*/
|
*/
|
||||||
public final TPropertyValidator inRange(final Range<TProperty> range) {
|
public final TPropertyValidator inRange(final Range<? super TProperty> range) {
|
||||||
return withRule(Conditions.inRange(range), value -> ValidationException.withMessage(
|
return withRule(Conditions.inRange(range), value -> ValidationException.withMessage(
|
||||||
"The input must in the interval %s. You entered %s.", range, value));
|
"The input must in the interval %s. You entered %s.", range, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,7 +60,7 @@ public abstract class BaseComparablePropertyValidator<
|
|||||||
* @return 当前校验器实例,用于链式调用
|
* @return 当前校验器实例,用于链式调用
|
||||||
*/
|
*/
|
||||||
public final TPropertyValidator inRange(
|
public final TPropertyValidator inRange(
|
||||||
final Range<TProperty> range, final String errorMessage) {
|
final Range<? super TProperty> range, final String errorMessage) {
|
||||||
return withRule(Conditions.inRange(range), errorMessage);
|
return withRule(Conditions.inRange(range), errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ public abstract class BaseComparablePropertyValidator<
|
|||||||
* @return 当前校验器实例,用于链式调用
|
* @return 当前校验器实例,用于链式调用
|
||||||
*/
|
*/
|
||||||
public final <X extends RuntimeException> TPropertyValidator inRange(
|
public final <X extends RuntimeException> TPropertyValidator inRange(
|
||||||
final Range<TProperty> range, final Supplier<X> exceptionSupplier) {
|
final Range<? super TProperty> range, final Supplier<X> exceptionSupplier) {
|
||||||
return withRule(Conditions.inRange(range), exceptionSupplier);
|
return withRule(Conditions.inRange(range), exceptionSupplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ public abstract class BaseComparablePropertyValidator<
|
|||||||
* @return 当前校验器实例,用于链式调用
|
* @return 当前校验器实例,用于链式调用
|
||||||
*/
|
*/
|
||||||
public final <X extends RuntimeException> TPropertyValidator inRange(
|
public final <X extends RuntimeException> TPropertyValidator inRange(
|
||||||
final Range<TProperty> range, final Function<TProperty, X> exceptionFunction) {
|
final Range<? super TProperty> range, final Function<TProperty, X> exceptionFunction) {
|
||||||
return withRule(Conditions.inRange(range), exceptionFunction);
|
return withRule(Conditions.inRange(range), exceptionFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,8 +95,8 @@ public abstract class BaseComparablePropertyValidator<
|
|||||||
*/
|
*/
|
||||||
private static class Conditions {
|
private static class Conditions {
|
||||||
|
|
||||||
private static <TProperty extends Comparable<TProperty>> Predicate<TProperty> inRange(
|
private static <TProperty extends Comparable<? super TProperty>> Predicate<TProperty> inRange(
|
||||||
final Range<TProperty> range) {
|
final Range<? super TProperty> range) {
|
||||||
return value -> value == null || range.contains(value);
|
return value -> value == null || range.contains(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -109,7 +109,7 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
|||||||
* 示例:{@code Person::getName}。
|
* 示例:{@code Person::getName}。
|
||||||
* @return {@code ComparablePropertyValidator}。用于添加针对该属性的校验规则。
|
* @return {@code ComparablePropertyValidator}。用于添加针对该属性的校验规则。
|
||||||
*/
|
*/
|
||||||
protected final <R extends Comparable<R>> ComparablePropertyValidator<T, R> ruleForComparable(
|
protected final <R extends Comparable<? super R>> ComparablePropertyValidator<T, R> ruleForComparable(
|
||||||
Function<T, R> getter) {
|
Function<T, R> getter) {
|
||||||
ComparablePropertyValidator<T, R> validator = new ComparablePropertyValidator<>(getter);
|
ComparablePropertyValidator<T, R> validator = new ComparablePropertyValidator<>(getter);
|
||||||
this.rules.add(validator::validate);
|
this.rules.add(validator::validate);
|
||||||
|
@@ -26,7 +26,7 @@ import java.util.function.Function;
|
|||||||
* @see com.google.common.collect.Range
|
* @see com.google.common.collect.Range
|
||||||
* @author ZhouXY
|
* @author ZhouXY
|
||||||
*/
|
*/
|
||||||
public class ComparablePropertyValidator<T, TProperty extends Comparable<TProperty>>
|
public class ComparablePropertyValidator<T, TProperty extends Comparable<? super TProperty>>
|
||||||
extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> {
|
extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> {
|
||||||
|
|
||||||
ComparablePropertyValidator(Function<T, ? extends TProperty> getter) {
|
ComparablePropertyValidator(Function<T, ? extends TProperty> getter) {
|
||||||
|
@@ -173,7 +173,8 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
|||||||
* @param key key
|
* @param key key
|
||||||
* @return 属性校验器
|
* @return 属性校验器
|
||||||
*/
|
*/
|
||||||
protected final <E extends Comparable<E>> ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
|
protected final <E extends Comparable<? super E>> //
|
||||||
|
ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Function<Map<K, V>, E> getter = m -> (E) m.get(key);
|
Function<Map<K, V>, E> getter = m -> (E) m.get(key);
|
||||||
return ruleForComparable(getter);
|
return ruleForComparable(getter);
|
||||||
|
@@ -122,14 +122,14 @@ class ParamsValidator extends MapValidator<String, Object> {
|
|||||||
.notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d));
|
.notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d));
|
||||||
ruleForString(STRING_PROPERTY)
|
ruleForString(STRING_PROPERTY)
|
||||||
.notNull();
|
.notNull();
|
||||||
ruleForComparable(DATE_TIME_PROPERTY)
|
this.<LocalDateTime>ruleForComparable(DATE_TIME_PROPERTY)
|
||||||
.notNull("The dateTimeProperty cannot be null");
|
.notNull("The dateTimeProperty cannot be null");
|
||||||
ruleFor(OBJECT_PROPERTY)
|
ruleFor(OBJECT_PROPERTY)
|
||||||
.notNull(() -> ExampleException.withMessage("The objectProperty cannot be null"));
|
.notNull(() -> ExampleException.withMessage("The objectProperty cannot be null"));
|
||||||
ruleForCollection(STRING_LIST_PROPERTY)
|
this.<String>ruleForCollection(STRING_LIST_PROPERTY)
|
||||||
.notNull(d -> ExampleException.withMessage("The stringListProperty cannot be null, but it was %s", d));
|
.notNull(d -> ExampleException.withMessage("The stringListProperty cannot be null, but it was %s", d));
|
||||||
|
|
||||||
ruleForPair(STRING_PROPERTY, STRING_PROPERTY2)
|
this.<String, String>ruleForPair(STRING_PROPERTY, STRING_PROPERTY2)
|
||||||
.must((str1, str2) -> str1 != null && str1.equals(str2),
|
.must((str1, str2) -> str1 != null && str1.equals(str2),
|
||||||
"'stringProperty' must be equal to 'stringProperty2'.");
|
"'stringProperty' must be equal to 'stringProperty2'.");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user