添加重载 state 方法。

0.4.x
ZhouXY108 2023-01-07 00:00:39 +08:00
parent 40f30f88a1
commit c6e9994b68
1 changed files with 26 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
@ -109,6 +110,31 @@ abstract class PropertyValidator<DTO, PROPERTY, THIS> {
return thisObject();
}
// ===== state =====
public THIS state(Collection<Predicate<PROPERTY>> conditions) {
return state(conditions, "无效的用户输入");
}
public THIS state(Collection<Predicate<PROPERTY>> conditions, String errMsg) {
return state(conditions, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> THIS state(
Collection<Predicate<PROPERTY>> conditions,
Supplier<E> exceptionCreator) {
return state(conditions, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> THIS state(
Collection<Predicate<PROPERTY>> conditions,
Function<PROPERTY, E> exceptionCreator) {
for (Predicate<PROPERTY> condition : conditions) {
withRule(condition, exceptionCreator);
}
return thisObject();
}
// ========================================================================
void validate(DTO obj) {