优化代码。
parent
3cac270574
commit
b03f978da9
|
@ -9,18 +9,18 @@ import java.util.function.Predicate;
|
|||
import java.util.function.Supplier;
|
||||
|
||||
public class BaseValidator<T> {
|
||||
private final List<Consumer<T>> rules = new ArrayList<>();
|
||||
private final List<Consumer<? super T>> rules = new ArrayList<>();
|
||||
|
||||
protected void withRule(final Predicate<T> rule, final String errorMessage) {
|
||||
withRule(rule, () -> new IllegalArgumentException(errorMessage));
|
||||
}
|
||||
|
||||
protected <E extends RuntimeException> void withRule(Predicate<T> rule, Supplier<E> exceptionBuilder) {
|
||||
protected <E extends RuntimeException> void withRule(Predicate<? super T> rule, Supplier<E> exceptionBuilder) {
|
||||
withRule(rule, value -> exceptionBuilder.get());
|
||||
}
|
||||
|
||||
protected <E extends RuntimeException> void withRule(
|
||||
Predicate<T> condition, Function<T, E> exceptionBuilder) {
|
||||
Predicate<? super T> condition, Function<T, E> exceptionBuilder) {
|
||||
withRule(value -> {
|
||||
if (!condition.test(value)) {
|
||||
throw exceptionBuilder.apply(value);
|
||||
|
@ -28,7 +28,7 @@ public class BaseValidator<T> {
|
|||
});
|
||||
}
|
||||
|
||||
protected void withRule(Consumer<T> rule) {
|
||||
protected void withRule(Consumer<? super T> rule) {
|
||||
this.rules.add(rule);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,19 +2,12 @@ package xyz.zhouxy.plusone.validator;
|
|||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class MapValidator<K, V> {
|
||||
|
||||
private final List<Consumer<Map<K, V>>> consumers = new LinkedList<>();
|
||||
public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
|
||||
private final Set<K> keys;
|
||||
|
||||
|
@ -48,68 +41,31 @@ public abstract class MapValidator<K, V> {
|
|||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
public final void validate(Map<K, V> obj) {
|
||||
this.consumers.forEach(consumer -> consumer.accept(obj));
|
||||
}
|
||||
|
||||
// ========== ruleFor ==========
|
||||
|
||||
protected final ObjectValidator<Map<K, V>, V> ruleFor(K key) {
|
||||
ObjectValidator<Map<K, V>, V> validator = new ObjectValidator<>(m -> m.get(key));
|
||||
this.consumers.add(validator::validate);
|
||||
return validator;
|
||||
return ruleFor(m -> m.get(key));
|
||||
}
|
||||
|
||||
protected final IntValidator<Map<K, V>> ruleForInt(K key) {
|
||||
IntValidator<Map<K, V>> validator = new IntValidator<>(m -> (Integer) m.get(key));
|
||||
this.consumers.add(validator::validate);
|
||||
return validator;
|
||||
return ruleForInt(m -> (Integer) m.get(key));
|
||||
}
|
||||
|
||||
protected final DoubleValidator<Map<K, V>> ruleForDouble(K key) {
|
||||
DoubleValidator<Map<K, V>> validator = new DoubleValidator<>(m -> (Double) m.get(key));
|
||||
this.consumers.add(validator::validate);
|
||||
return validator;
|
||||
return ruleForDouble(m -> (Double) m.get(key));
|
||||
}
|
||||
|
||||
protected final BoolValidator<Map<K, V>> ruleForBool(K key) {
|
||||
BoolValidator<Map<K, V>> validator = new BoolValidator<>(m -> (Boolean) m.get(key));
|
||||
this.consumers.add(validator::validate);
|
||||
return validator;
|
||||
return ruleForBool(m -> (Boolean) m.get(key));
|
||||
}
|
||||
|
||||
protected final StringValidator<Map<K, V>> ruleForString(K key) {
|
||||
StringValidator<Map<K, V>> validator = new StringValidator<>(m -> (String) m.get(key));
|
||||
this.consumers.add(validator::validate);
|
||||
return validator;
|
||||
return ruleForString(m -> (String) m.get(key));
|
||||
}
|
||||
|
||||
protected final <E> CollectionValidator<Map<K, V>, E> ruleForCollection(K key) {
|
||||
@SuppressWarnings("unchecked")
|
||||
CollectionValidator<Map<K, V>, E> validator = new CollectionValidator<>(m -> (Collection<E>) m.get(key));
|
||||
this.consumers.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
// ========== withRule ==========
|
||||
|
||||
protected final void withRule(Predicate<? super Map<K, V>> rule, String errMsg) {
|
||||
withRule(rule, map -> new IllegalArgumentException(errMsg));
|
||||
}
|
||||
|
||||
protected final void withRule(Predicate<? super Map<K, V>> rule, String errMsgFormat, Object... args) {
|
||||
withRule(rule, map -> new IllegalArgumentException(String.format(errMsgFormat, args)));
|
||||
}
|
||||
|
||||
protected final <E extends RuntimeException> void withRule(Predicate<? super Map<K, V>> rule, Supplier<E> e) {
|
||||
withRule(rule, map -> e.get());
|
||||
}
|
||||
|
||||
protected final <E extends RuntimeException> void withRule(Predicate<? super Map<K, V>> rule, Function<Map<K, V>, E> e) {
|
||||
this.consumers.add(map -> {
|
||||
if (!rule.test(map)) {
|
||||
throw e.apply(map);
|
||||
}
|
||||
});
|
||||
Function<Map<K, V>, Collection<E>> getter = m -> (Collection<E>) m.get(key);
|
||||
return ruleForCollection(getter);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue