优化代码。

for-mfp
ZhouXY108 2024-03-14 16:28:32 +08:00
parent 65c33d3a28
commit e4221bc18b
2 changed files with 9 additions and 6 deletions

View File

@ -93,7 +93,7 @@ public class BaseValidator<T> {
this.getter = getter; this.getter = getter;
} }
<E extends RuntimeException> void withRule(Predicate<PROPERTY> condition, <E extends RuntimeException> void withRule(Predicate<? super PROPERTY> condition,
Function<PROPERTY, E> exceptionCreator) { Function<PROPERTY, E> exceptionCreator) {
withRule(value -> { withRule(value -> {
if (!condition.test(value)) { if (!condition.test(value)) {
@ -167,22 +167,22 @@ public class BaseValidator<T> {
// ===== isTrue ===== // ===== isTrue =====
public THIS isTrue(Predicate<PROPERTY> condition) { public THIS isTrue(Predicate<? super PROPERTY> condition) {
return isTrue(condition, "无效的用户输入"); return isTrue(condition, "无效的用户输入");
} }
public THIS isTrue(Predicate<PROPERTY> condition, String errMsg) { public THIS isTrue(Predicate<? super PROPERTY> condition, String errMsg) {
return isTrue(condition, convertExceptionCreator(errMsg)); return isTrue(condition, convertExceptionCreator(errMsg));
} }
public <E extends RuntimeException> THIS isTrue( public <E extends RuntimeException> THIS isTrue(
Predicate<PROPERTY> condition, Predicate<? super PROPERTY> condition,
Supplier<E> exceptionCreator) { Supplier<E> exceptionCreator) {
return isTrue(condition, convertExceptionCreator(exceptionCreator)); return isTrue(condition, convertExceptionCreator(exceptionCreator));
} }
public <E extends RuntimeException> THIS isTrue( public <E extends RuntimeException> THIS isTrue(
Predicate<PROPERTY> condition, Predicate<? super PROPERTY> condition,
Function<PROPERTY, E> exceptionCreator) { Function<PROPERTY, E> exceptionCreator) {
withRule(condition, exceptionCreator); withRule(condition, exceptionCreator);
return thisObject(); return thisObject();

View File

@ -6,8 +6,10 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import xyz.zhouxy.plusone.commons.util.Predicates;
import xyz.zhouxy.plusone.constant.Patterns; import xyz.zhouxy.plusone.constant.Patterns;
import xyz.zhouxy.plusone.validator.MapValidator; import xyz.zhouxy.plusone.validator.MapValidator;
@ -42,7 +44,8 @@ class TestValidator extends MapValidator {
TestValidator() { TestValidator() {
super(Arrays.asList(USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST)); super(Arrays.asList(USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST));
this.ruleForString(USERNAME) this.ruleForString(USERNAME)
.notEmpty() .isTrue(Predicates.of(StringUtils::isNotEmpty)
.and(Objects::nonNull))
.matches(Patterns.USERNAME.getPattern(), .matches(Patterns.USERNAME.getPattern(),
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
this.ruleForString(ACCOUNT) this.ruleForString(ACCOUNT)