优化代码,修改方法名。
parent
0e2b1ffd48
commit
7ae8a5b6d3
|
@ -87,46 +87,46 @@ abstract class PropertyValidator<DTO, PROPERTY, THIS> {
|
|||
return thisObject();
|
||||
}
|
||||
|
||||
// ===== state =====
|
||||
// ===== isTrue =====
|
||||
|
||||
public THIS state(Predicate<PROPERTY> condition) {
|
||||
return state(condition, "无效的用户输入");
|
||||
public THIS isTrue(Predicate<PROPERTY> condition) {
|
||||
return isTrue(condition, "无效的用户输入");
|
||||
}
|
||||
|
||||
public THIS state(Predicate<PROPERTY> condition, String errMsg) {
|
||||
return state(condition, convertExceptionCreator(errMsg));
|
||||
public THIS isTrue(Predicate<PROPERTY> condition, String errMsg) {
|
||||
return isTrue(condition, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> THIS state(
|
||||
public <E extends RuntimeException> THIS isTrue(
|
||||
Predicate<PROPERTY> condition,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return state(condition, convertExceptionCreator(exceptionCreator));
|
||||
return isTrue(condition, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> THIS state(
|
||||
public <E extends RuntimeException> THIS isTrue(
|
||||
Predicate<PROPERTY> condition,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(condition, exceptionCreator);
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
// ===== state =====
|
||||
// ===== isTrue =====
|
||||
|
||||
public THIS state(Collection<Predicate<PROPERTY>> conditions) {
|
||||
return state(conditions, "无效的用户输入");
|
||||
public THIS isTrue(Collection<Predicate<PROPERTY>> conditions) {
|
||||
return isTrue(conditions, "无效的用户输入");
|
||||
}
|
||||
|
||||
public THIS state(Collection<Predicate<PROPERTY>> conditions, String errMsg) {
|
||||
return state(conditions, convertExceptionCreator(errMsg));
|
||||
public THIS isTrue(Collection<Predicate<PROPERTY>> conditions, String errMsg) {
|
||||
return isTrue(conditions, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> THIS state(
|
||||
public <E extends RuntimeException> THIS isTrue(
|
||||
Collection<Predicate<PROPERTY>> conditions,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return state(conditions, convertExceptionCreator(exceptionCreator));
|
||||
return isTrue(conditions, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> THIS state(
|
||||
public <E extends RuntimeException> THIS isTrue(
|
||||
Collection<Predicate<PROPERTY>> conditions,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
for (Predicate<PROPERTY> condition : conditions) {
|
||||
|
|
|
@ -40,75 +40,75 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
|||
return this;
|
||||
}
|
||||
|
||||
// ===== matchesOr =====
|
||||
// ===== matchesOne =====
|
||||
|
||||
public StringValidator<DTO> matchesOr(Pattern[] regexs, String errMsg) {
|
||||
return matchesOr(regexs, convertExceptionCreator(errMsg));
|
||||
public StringValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
|
||||
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||
Pattern[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesOr(regexs, convertExceptionCreator(exceptionCreator));
|
||||
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||
Pattern[] regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator);
|
||||
withRule(input -> RegexUtil.matchesOne(input, regexs), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringValidator<DTO> matchesOr(List<Pattern> regexs, String errMsg) {
|
||||
return matchesOr(regexs, convertExceptionCreator(errMsg));
|
||||
public StringValidator<DTO> matchesOne(List<Pattern> regexs, String errMsg) {
|
||||
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||
List<Pattern> regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesOr(regexs, convertExceptionCreator(exceptionCreator));
|
||||
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||
List<Pattern> regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
withRule(input -> RegexUtil.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== matchesAnd =====
|
||||
// ===== matchesAll =====
|
||||
|
||||
public StringValidator<DTO> matchesAnd(Pattern[] regexs, String errMsg) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(errMsg));
|
||||
public StringValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
|
||||
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
|
||||
Pattern[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(exceptionCreator));
|
||||
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
|
||||
Pattern[] regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesAnd(input, regexs), exceptionCreator);
|
||||
withRule(input -> RegexUtil.matchesAll(input, regexs), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringValidator<DTO> matchesAnd(Collection<Pattern> regexs, String errMsg) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(errMsg));
|
||||
public StringValidator<DTO> matchesAll(Collection<Pattern> regexs, String errMsg) {
|
||||
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
|
||||
Collection<Pattern> regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(exceptionCreator));
|
||||
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
|
||||
Collection<Pattern> regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesAnd(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
withRule(input -> RegexUtil.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ package xyz.zhouxy.plusone.validator.test;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -34,7 +33,7 @@ class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
|
|||
username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username)));
|
||||
ruleForString(RegisterCommand::getAccount)
|
||||
.notNull("请输入邮箱地址或手机号")
|
||||
.matchesOr(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号");
|
||||
.matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号");
|
||||
ruleForString(RegisterCommand::getCode)
|
||||
.notNull("验证码不能为空")
|
||||
.matches(PatternConsts.CAPTCHA, "验证码不符合规范");
|
||||
|
|
|
@ -3,37 +3,36 @@ package xyz.zhouxy.plusone.validator2.test;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.function.Predicates;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexUtil;
|
||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
|
||||
class BaseValidator2Test {
|
||||
@Test
|
||||
void testValidate() {
|
||||
RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", "A1b2C3d4",
|
||||
"A1b2C3d4",
|
||||
Arrays.asList(new String[] { "admin", "editor" }));
|
||||
RegisterCommandValidator2.INSTANCE.validate(registerCommand);
|
||||
RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336",
|
||||
"A1b2C3d4", "A1b2C3d4", Arrays.asList(new String[] { "admin", "editor" }));
|
||||
RegisterCommandValidator.INSTANCE.validate(registerCommand);
|
||||
System.out.println(registerCommand);
|
||||
}
|
||||
}
|
||||
|
||||
class RegisterCommandValidator2 extends BaseValidator<RegisterCommand> {
|
||||
class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
|
||||
|
||||
static final RegisterCommandValidator2 INSTANCE = new RegisterCommandValidator2();
|
||||
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
|
||||
|
||||
private RegisterCommandValidator2() {
|
||||
private RegisterCommandValidator() {
|
||||
ruleForString(RegisterCommand::getUsername)
|
||||
.state(((Predicate<String>) Objects::nonNull)
|
||||
.isTrue(Predicates.<String>of(Objects::nonNull)
|
||||
.and(StringUtils::isNotEmpty)
|
||||
.and(StringUtils::isNotBlank)
|
||||
.and(username -> Pattern.matches(RegexConsts.EMAIL, username)),
|
||||
(username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username))));
|
||||
.and(username -> RegexUtil.matches(username, PatternConsts.EMAIL)),
|
||||
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue