新增 LongValidator。
This commit is contained in:
parent
875150aa6e
commit
011788b112
@ -57,6 +57,12 @@ public abstract class BaseValidator<T> {
|
||||
return validator;
|
||||
}
|
||||
|
||||
protected final LongValidator<T> ruleForLong(Function<T, Long> getter) {
|
||||
LongValidator<T> validator = new LongValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
protected final DoubleValidator<T> ruleForDouble(Function<T, Double> getter) {
|
||||
DoubleValidator<T> validator = new DoubleValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
|
@ -0,0 +1,35 @@
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LongValidator<DTO> extends BasePropertyValidator<DTO, Long, LongValidator<DTO>> {
|
||||
|
||||
LongValidator(Function<DTO, Long> getter) {
|
||||
super(getter);
|
||||
}
|
||||
|
||||
public LongValidator<DTO> between(long min, long max) {
|
||||
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
|
||||
}
|
||||
|
||||
public LongValidator<DTO> between(long min, long max, String errMsg) {
|
||||
return between(min, max, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongValidator<DTO> between(long min, long max,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return between(min, max, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> LongValidator<DTO> between(long min, long max,
|
||||
Function<Long, E> exceptionCreator) {
|
||||
withRule(value -> (value >= min && value < max), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LongValidator<DTO> thisObject() {
|
||||
return this;
|
||||
}
|
||||
}
|
@ -51,6 +51,10 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
return ruleForInt(m -> (Integer) m.get(key));
|
||||
}
|
||||
|
||||
protected final LongValidator<Map<K, V>> ruleForLong(K key) {
|
||||
return ruleForLong(m -> (Long) m.get(key));
|
||||
}
|
||||
|
||||
protected final DoubleValidator<Map<K, V>> ruleForDouble(K key) {
|
||||
return ruleForDouble(m -> (Double) m.get(key));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user