diff --git a/pom.xml b/pom.xml
index cdab09f..2a5a8ad 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,6 +1,7 @@
-
4.0.0
@@ -9,8 +10,7 @@
0.0.1-SNAPSHOT
plusone-validator
-
- http://www.example.com
+ http://www.zhouxy.xyz
UTF-8
@@ -20,27 +20,37 @@
- xyz.zhouxy.plusone
- plusone-commons
- 0.0.1-SNAPSHOT
+ com.google.guava
+ guava
+ 32.0.1-jre
- junit
- junit
- 4.11
+ org.apache.commons
+ commons-lang3
+ 3.13.0
+ test
+
+
+ xyz.zhouxy.plusone
+ plusone-commons
+ 0.1.0-SNAPSHOT
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.9.2
test
-
+
-
maven-clean-plugin
3.1.0
-
maven-resources-plugin
3.0.2
@@ -65,7 +75,6 @@
maven-deploy-plugin
2.8.2
-
maven-site-plugin
3.7.1
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
deleted file mode 100644
index 08f13d0..0000000
--- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
+++ /dev/null
@@ -1,86 +0,0 @@
-package xyz.zhouxy.plusone.validator;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.function.Supplier;
-
-/**
- * 校验器
- *
- *
- * 可以使用以下方式初始化一个校验器:
- *
- *
- *
- * BaseValidator<Integer> validator = new BaseValidator<>() {
- * {
- * withRule(value -> Objects.nonNull(value), "value 不能为空");
- * withRule(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
- * }
- * };
- *
- *
- *
- * 也可以通过继承本类,定义一个校验器(可使用单例模式)。
- *
- *
- *
- * 然后通过校验器的 {@link #validate} 方法,或
- * {@link ValidateUtil#validate(Object, Validator)} 对指定对象进行校验。
- *
- *
- *
- * ValidateUtil.validate(255, validator);
- *
- *
- *
- * validator.validate(666);
- *
- *
- *
- * @author ZhouXY
- * @see IValidateRequired
- * @see ValidateUtil
- * @see Validator
- */
-public abstract class BaseValidator {
-
- private final List> rules = new ArrayList<>();
-
- protected BaseValidator() {
- }
-
- protected final void withRule(Predicate rule, String errorMessage) {
- withRule(rule, () -> new InvalidInputException(errorMessage));
- }
-
- protected final void withRule(Predicate rule, Supplier exceptionCreator) {
- withRule(rule, value -> exceptionCreator.get());
- }
-
- protected final void withRule(Predicate rule, Function exceptionCreator) {
- this.rules.add(new RuleInfo<>(rule, exceptionCreator));
- }
-
- public void validate(T obj) {
- this.rules.forEach(ruleInfo -> ruleInfo.validate(obj));
- }
-
- protected static class RuleInfo {
- private final Predicate rule;
- private final Function exceptionCreator;
-
- private RuleInfo(Predicate rule, Function exceptionCreator) {
- this.rule = rule;
- this.exceptionCreator = exceptionCreator;
- }
-
- private void validate(T obj) {
- if (!rule.test(obj)) {
- throw exceptionCreator.apply(obj);
- }
- }
- }
-}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java
deleted file mode 100644
index 5a23431..0000000
--- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java
+++ /dev/null
@@ -1,53 +0,0 @@
-package xyz.zhouxy.plusone.validator;
-
-import xyz.zhouxy.plusone.exception.PlusoneException;
-
-/**
- * 4040200 - 无效的用户输入
- *
- * @author ZhouXY
- */
-public class InvalidInputException extends PlusoneException {
-
- private static final long serialVersionUID = 7956661913360059670L;
-
- public static final int ERROR_CODE = 4040200;
-
- private InvalidInputException(int code, String msg) {
- super(code, msg);
- }
-
- private InvalidInputException(int code, Throwable cause) {
- super(code, cause);
- }
-
- private InvalidInputException(int code, String msg, Throwable cause) {
- super(code, msg, cause);
- }
-
- public InvalidInputException(String msg) {
- this(ERROR_CODE, msg);
- }
-
- public InvalidInputException(Throwable cause) {
- this(ERROR_CODE, cause);
- }
-
- public InvalidInputException(String msg, Throwable cause) {
- this(ERROR_CODE, msg, cause);
- }
-
- /**
- * 不支持的 Principal 类型出现时抛出的异常
- */
- public static InvalidInputException unsupportedPrincipalTypeException() {
- return unsupportedPrincipalTypeException("不支持的 PrincipalType");
- }
-
- /**
- * 不支持的 Principal 类型出现时抛出的异常
- */
- public static InvalidInputException unsupportedPrincipalTypeException(String message) {
- return new InvalidInputException(4040201, message);
- }
-}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java
new file mode 100644
index 0000000..bc7f769
--- /dev/null
+++ b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java
@@ -0,0 +1,125 @@
+package xyz.zhouxy.plusone.validator;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+public abstract class MapValidator {
+
+ private final List>> consumers = new LinkedList<>();
+
+ private final Set keys;
+
+ protected MapValidator(K[] keys) {
+ this(Arrays.asList(keys));
+ }
+
+ protected MapValidator(Collection keys) {
+ this.keys = keys.stream().collect(Collectors.toSet());
+ }
+
+ public final Map validateAndCopy(Map obj) {
+ return validateAndCopyInternal(obj, this.keys);
+ }
+
+ public final Map validateAndCopy(Map obj, Collection keys) {
+ return validateAndCopyInternal(obj, keys);
+ }
+
+ @SafeVarargs
+ public final Map validateAndCopy(Map obj, K... keys) {
+ return validateAndCopyInternal(obj, Arrays.asList(keys));
+ }
+
+ private final Map validateAndCopyInternal(Map obj, Collection keys) {
+ validate(obj);
+ return obj.entrySet().stream()
+ .filter(kv -> keys.contains(kv.getKey()))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
+
+ public final void validate(Map obj) {
+ this.consumers.forEach(consumer -> consumer.accept(obj));
+ }
+
+ @SuppressWarnings("unused")
+ protected final PropertyValidator checkProperty(K key, Class clazz) {
+ return checkProperty(key);
+ }
+
+ protected final PropertyValidator checkProperty(K key) {
+ PropertyValidator validator = new PropertyValidator<>(key);
+ this.consumers.add(validator::validate);
+ return validator;
+ }
+
+ protected final void withRule(Predicate