使用 AssertTools 代替 Preconditions;使用 StringTools。
This commit is contained in:
parent
7d29027b91
commit
6da1dfbceb
@ -6,22 +6,29 @@ import java.util.function.Function;
|
|||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* StringValidator
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 针对文本字段的验证器。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
*/
|
||||||
public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, StringValidator<DTO>> {
|
public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, StringValidator<DTO>> {
|
||||||
|
|
||||||
StringValidator(Function<DTO, String> getter) {
|
StringValidator(Function<DTO, String> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====================
|
// ================================
|
||||||
// ====== String ======
|
// #region - matches
|
||||||
// ====================
|
// ================================
|
||||||
|
|
||||||
// ===== matches =====
|
|
||||||
|
|
||||||
public StringValidator<DTO> matches(Pattern regex, String errMsg) {
|
public StringValidator<DTO> matches(Pattern regex, String errMsg) {
|
||||||
return matches(regex, convertExceptionCreator(errMsg));
|
return matches(regex, convertExceptionCreator(errMsg));
|
||||||
@ -40,7 +47,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== matchesOne =====
|
// ================================
|
||||||
|
// #endregion - matches
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - matchesOne
|
||||||
|
// ================================
|
||||||
|
|
||||||
public StringValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
|
public StringValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
|
||||||
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||||
@ -76,7 +89,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== matchesAll =====
|
// ================================
|
||||||
|
// #endregion - matchesOne
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - matchesAll
|
||||||
|
// ================================
|
||||||
|
|
||||||
public StringValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
|
public StringValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
|
||||||
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||||
@ -112,19 +131,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== notBlank =====
|
// ================================
|
||||||
|
// #endregion - matchesAll
|
||||||
|
// ================================
|
||||||
|
|
||||||
static boolean isNotBlank(final String cs) {
|
// ================================
|
||||||
if (cs == null || cs.isEmpty()) {
|
// #region - notBlank
|
||||||
return false;
|
// ================================
|
||||||
}
|
|
||||||
for (int i = 0; i < cs.length(); i++) {
|
|
||||||
if (!Character.isWhitespace(cs.charAt(i))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public StringValidator<DTO> notBlank() {
|
public StringValidator<DTO> notBlank() {
|
||||||
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
||||||
@ -140,11 +153,17 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> notBlank(
|
public <E extends RuntimeException> StringValidator<DTO> notBlank(
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(StringValidator::isNotBlank, exceptionCreator);
|
withRule(StringTools::isNotBlank, exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== email =====
|
// ================================
|
||||||
|
// #endregion - notBlank
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - email
|
||||||
|
// ================================
|
||||||
|
|
||||||
public StringValidator<DTO> email() {
|
public StringValidator<DTO> email() {
|
||||||
return email("The value is not an email address.");
|
return email("The value is not an email address.");
|
||||||
@ -159,10 +178,17 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
|
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
|
||||||
|
// TODO [优化] 优化 email 校验
|
||||||
return matches(PatternConsts.EMAIL, exceptionCreator);
|
return matches(PatternConsts.EMAIL, exceptionCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====== notEmpty =====
|
// ================================
|
||||||
|
// #endregion - email
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - notEmpty
|
||||||
|
// ================================
|
||||||
|
|
||||||
public StringValidator<DTO> notEmpty(String errMsg) {
|
public StringValidator<DTO> notEmpty(String errMsg) {
|
||||||
return notEmpty(convertExceptionCreator(errMsg));
|
return notEmpty(convertExceptionCreator(errMsg));
|
||||||
@ -178,7 +204,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====== isNullOrEmpty =====
|
// ================================
|
||||||
|
// #endregion - notEmpty
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - isNullOrEmpty
|
||||||
|
// ================================
|
||||||
|
|
||||||
public StringValidator<DTO> isNullOrEmpty(String errMsg) {
|
public StringValidator<DTO> isNullOrEmpty(String errMsg) {
|
||||||
return isNullOrEmpty(convertExceptionCreator(errMsg));
|
return isNullOrEmpty(convertExceptionCreator(errMsg));
|
||||||
@ -194,7 +226,13 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====== length =====
|
// ================================
|
||||||
|
// #endregion - isNullOrEmpty
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - length
|
||||||
|
// ================================
|
||||||
|
|
||||||
public StringValidator<DTO> length(int length, String errMsg) {
|
public StringValidator<DTO> length(int length, String errMsg) {
|
||||||
return length(length, convertExceptionCreator(errMsg));
|
return length(length, convertExceptionCreator(errMsg));
|
||||||
@ -207,7 +245,7 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> length(int length,
|
public <E extends RuntimeException> StringValidator<DTO> length(int length,
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
Preconditions.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
|
AssertTools.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
|
||||||
withRule(s -> s != null && s.length() == length, exceptionCreator);
|
withRule(s -> s != null && s.length() == length, exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -231,12 +269,16 @@ public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, Str
|
|||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> length(int min, int max,
|
public <E extends RuntimeException> StringValidator<DTO> length(int min, int max,
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
Preconditions.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
|
AssertTools.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
|
||||||
Preconditions.checkArgument(min < max, "The minimum value must be less than the maximum value.");
|
AssertTools.checkArgument(min < max, "The minimum value must be less than the maximum value.");
|
||||||
withRule(s -> length(s, min, max), exceptionCreator);
|
withRule(s -> length(s, min, max), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - length
|
||||||
|
// ================================
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected StringValidator<DTO> thisObject() {
|
protected StringValidator<DTO> thisObject() {
|
||||||
return this;
|
return this;
|
||||||
|
@ -10,10 +10,9 @@ import java.util.regex.Pattern;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||||
import xyz.zhouxy.plusone.commons.function.PredicateTools;
|
import xyz.zhouxy.plusone.commons.function.PredicateTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||||
import xyz.zhouxy.plusone.validator.Validator;
|
import xyz.zhouxy.plusone.validator.Validator;
|
||||||
|
|
||||||
@ -43,14 +42,14 @@ class ValidatorTests {
|
|||||||
// 传入 rule
|
// 传入 rule
|
||||||
.addRule(command -> {
|
.addRule(command -> {
|
||||||
String code = command.getCode();
|
String code = command.getCode();
|
||||||
Preconditions.checkArgument(Objects.nonNull(code), "验证码不能为空");
|
AssertTools.checkArgument(Objects.nonNull(code), "验证码不能为空");
|
||||||
Preconditions.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范");
|
AssertTools.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范");
|
||||||
})
|
})
|
||||||
// 传入 rule
|
// 传入 rule
|
||||||
.addRule(command -> {
|
.addRule(command -> {
|
||||||
String password = command.getPassword();
|
String password = command.getPassword();
|
||||||
Preconditions.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空");
|
AssertTools.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空");
|
||||||
Preconditions.checkArgument(RegexTools.matches(password, PASSWORD), "密码不符合规范");
|
AssertTools.checkArgument(RegexTools.matches(password, PASSWORD), "密码不符合规范");
|
||||||
})
|
})
|
||||||
// 传入 predicate 和 Supplier<E extends RuntimeException>
|
// 传入 predicate 和 Supplier<E extends RuntimeException>
|
||||||
.addRule(command -> CollectionTools.isNotEmpty(command.getRoles()),
|
.addRule(command -> CollectionTools.isNotEmpty(command.getRoles()),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user