优化泛型代码。
This commit is contained in:
parent
e651b40aa0
commit
07159d205a
@ -16,7 +16,7 @@ public abstract class BasePropertyValidator< //
|
|||||||
|
|
||||||
private final Function<TObj, ? extends TProperty> getter;
|
private final Function<TObj, ? extends TProperty> getter;
|
||||||
|
|
||||||
private final List<Consumer<TProperty>> consumers = new LinkedList<>();
|
private final List<Consumer<? super TProperty>> consumers = new LinkedList<>();
|
||||||
|
|
||||||
protected BasePropertyValidator(Function<TObj, ? extends TProperty> getter) {
|
protected BasePropertyValidator(Function<TObj, ? extends TProperty> getter) {
|
||||||
this.getter = getter;
|
this.getter = getter;
|
||||||
@ -47,7 +47,7 @@ public abstract class BasePropertyValidator< //
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final <T extends TObj> void validate(T obj) {
|
public final <T extends TObj> void validate(T obj) {
|
||||||
for (Consumer<TProperty> consumer : consumers) {
|
for (Consumer<? super TProperty> consumer : consumers) {
|
||||||
consumer.accept(getter.apply(obj));
|
consumer.accept(getter.apply(obj));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,22 +115,22 @@ public abstract class BasePropertyValidator< //
|
|||||||
|
|
||||||
// ===== isTrue =====
|
// ===== isTrue =====
|
||||||
|
|
||||||
public TPropertyValidator isTrue(Predicate<TProperty> condition) {
|
public TPropertyValidator isTrue(Predicate<? super TProperty> condition) {
|
||||||
return isTrue(condition, "无效的用户输入");
|
return isTrue(condition, "无效的用户输入");
|
||||||
}
|
}
|
||||||
|
|
||||||
public TPropertyValidator isTrue(Predicate<TProperty> condition, String errMsg) {
|
public TPropertyValidator isTrue(Predicate<? super TProperty> condition, String errMsg) {
|
||||||
return isTrue(condition, convertExceptionCreator(errMsg));
|
return isTrue(condition, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> TPropertyValidator isTrue(
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
Predicate<TProperty> condition,
|
Predicate<? super TProperty> condition,
|
||||||
Supplier<E> exceptionCreator) {
|
Supplier<E> exceptionCreator) {
|
||||||
return isTrue(condition, convertExceptionCreator(exceptionCreator));
|
return isTrue(condition, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> TPropertyValidator isTrue(
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
Predicate<TProperty> condition,
|
Predicate<? super TProperty> condition,
|
||||||
Function<TProperty, E> exceptionCreator) {
|
Function<TProperty, E> exceptionCreator) {
|
||||||
withRule(condition, exceptionCreator);
|
withRule(condition, exceptionCreator);
|
||||||
return thisObject();
|
return thisObject();
|
||||||
@ -138,24 +138,24 @@ public abstract class BasePropertyValidator< //
|
|||||||
|
|
||||||
// ===== isTrue =====
|
// ===== isTrue =====
|
||||||
|
|
||||||
public TPropertyValidator isTrue(Collection<Predicate<TProperty>> conditions) {
|
public TPropertyValidator isTrue(Collection<Predicate<? super TProperty>> conditions) {
|
||||||
return isTrue(conditions, "无效的用户输入");
|
return isTrue(conditions, "无效的用户输入");
|
||||||
}
|
}
|
||||||
|
|
||||||
public TPropertyValidator isTrue(Collection<Predicate<TProperty>> conditions, String errMsg) {
|
public TPropertyValidator isTrue(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
|
||||||
return isTrue(conditions, convertExceptionCreator(errMsg));
|
return isTrue(conditions, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> TPropertyValidator isTrue(
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
Collection<Predicate<TProperty>> conditions,
|
Collection<Predicate<? super TProperty>> conditions,
|
||||||
Supplier<E> exceptionCreator) {
|
Supplier<E> exceptionCreator) {
|
||||||
return isTrue(conditions, convertExceptionCreator(exceptionCreator));
|
return isTrue(conditions, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> TPropertyValidator isTrue(
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
Collection<Predicate<TProperty>> conditions,
|
Collection<Predicate<? super TProperty>> conditions,
|
||||||
Function<TProperty, E> exceptionCreator) {
|
Function<TProperty, E> exceptionCreator) {
|
||||||
for (Predicate<TProperty> condition : conditions) {
|
for (Predicate<? super TProperty> condition : conditions) {
|
||||||
withRule(condition, exceptionCreator);
|
withRule(condition, exceptionCreator);
|
||||||
}
|
}
|
||||||
return thisObject();
|
return thisObject();
|
||||||
|
@ -24,7 +24,7 @@ import java.util.function.Supplier;
|
|||||||
public abstract class BaseValidator<T> {
|
public abstract class BaseValidator<T> {
|
||||||
private final List<Consumer<? super T>> rules = new ArrayList<>();
|
private final List<Consumer<? super T>> rules = new ArrayList<>();
|
||||||
|
|
||||||
protected void withRule(final Predicate<T> rule, final String errorMessage) {
|
protected void withRule(final Predicate<? super T> rule, final String errorMessage) {
|
||||||
withRule(rule, () -> new IllegalArgumentException(errorMessage));
|
withRule(rule, () -> new IllegalArgumentException(errorMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,22 +38,22 @@ import java.util.function.Supplier;
|
|||||||
* @see BaseValidator
|
* @see BaseValidator
|
||||||
*/
|
*/
|
||||||
public final class Validator<T> extends BaseValidator<T> {
|
public final class Validator<T> extends BaseValidator<T> {
|
||||||
public final Validator<T> addRule(final Predicate<T> rule, final String errorMessage) {
|
public final Validator<T> addRule(final Predicate<? super T> rule, final String errorMessage) {
|
||||||
withRule(rule, errorMessage);
|
withRule(rule, errorMessage);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final <E extends RuntimeException> Validator<T> addRule(Predicate<T> rule, Supplier<E> exceptionCreator) {
|
public final <E extends RuntimeException> Validator<T> addRule(Predicate<? super T> rule, Supplier<E> exceptionCreator) {
|
||||||
withRule(rule, exceptionCreator);
|
withRule(rule, exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final <E extends RuntimeException> Validator<T> addRule(Predicate<T> rule, Function<T, E> exceptionCreator) {
|
public final <E extends RuntimeException> Validator<T> addRule(Predicate<? super T> rule, Function<T, E> exceptionCreator) {
|
||||||
withRule(rule, exceptionCreator);
|
withRule(rule, exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Validator<T> addRule(Consumer<T> rule) {
|
public final Validator<T> addRule(Consumer<? super T> rule) {
|
||||||
withRule(rule);
|
withRule(rule);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user