diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..2087dd7
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+# EditorConfig is awesome: https://EditorConfig.org
+
+# top-most EditorConfig file
+root = true
+
+[*]
+indent_style = space
+indent_size = 4
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = false
+insert_final_newline = true
diff --git a/pom.xml b/pom.xml
index 12d02a5..d7d1aad 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
xyz.zhouxy.plusone
plusone-validator
- 0.1.3-SNAPSHOT
+ 0.1.4-SNAPSHOT
plusone-validator
http://zhouxy.xyz
@@ -28,6 +28,7 @@
org.apache.commons
commons-lang3
${commons-lang3.version}
+ test
org.junit.jupiter
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java
new file mode 100644
index 0000000..4913aa5
--- /dev/null
+++ b/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java
@@ -0,0 +1,171 @@
+package xyz.zhouxy.plusone.validator;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+
+public abstract class BasePropertyValidator< //
+ TObj, //
+ TProperty, //
+ TPropertyValidator extends BasePropertyValidator> {
+
+ private final Function getter;
+
+ private final List> consumers = new LinkedList<>();
+
+ protected BasePropertyValidator(Function getter) {
+ this.getter = getter;
+ }
+
+ public final TPropertyValidator withRule(Predicate super TProperty> rule) {
+ return withRule(rule, v -> new IllegalArgumentException());
+ }
+
+ public final TPropertyValidator withRule(
+ Predicate super TProperty> rule, String errMsg) {
+ return withRule(rule, convertExceptionCreator(errMsg));
+ }
+
+ public final TPropertyValidator withRule(
+ Predicate super TProperty> rule, Supplier e) {
+ return withRule(rule, convertExceptionCreator(e));
+ }
+
+ public final TPropertyValidator withRule(
+ Predicate super TProperty> rule, Function e) {
+ this.consumers.add(v -> {
+ if (!rule.test(v)) {
+ throw e.apply(v);
+ }
+ });
+ return thisObject();
+ }
+
+ public final void validate(T obj) {
+ for (Consumer consumer : consumers) {
+ consumer.accept(getter.apply(obj));
+ }
+ }
+
+ protected abstract TPropertyValidator thisObject();
+
+ // ====================
+ // ====== Object ======
+ // ====================
+
+ // ====== notNull =====
+
+ public TPropertyValidator notNull() {
+ return notNull("Value could not be null.");
+ }
+
+ public TPropertyValidator notNull(String errMsg) {
+ return notNull(convertExceptionCreator(errMsg));
+ }
+
+ public TPropertyValidator notNull(Supplier exceptionCreator) {
+ return notNull(convertExceptionCreator(exceptionCreator));
+ }
+
+ public TPropertyValidator notNull(Function exceptionCreator) {
+ withRule(Objects::nonNull, exceptionCreator);
+ return thisObject();
+ }
+
+ // ====== isNull =====
+
+ public TPropertyValidator isNull(String errMsg) {
+ return isNull(convertExceptionCreator(errMsg));
+ }
+
+ public TPropertyValidator isNull(Supplier exceptionCreator) {
+ return isNull(convertExceptionCreator(exceptionCreator));
+ }
+
+ public TPropertyValidator isNull(Function exceptionCreator) {
+ withRule(Objects::isNull, exceptionCreator);
+ return thisObject();
+ }
+
+ // ===== equals =====
+
+ public TPropertyValidator equalsThat(Object that) {
+ return equalsThat(that, value -> new IllegalArgumentException(String.format("(%s) 必须与 (%s) 相等", value, that)));
+ }
+
+ public TPropertyValidator equalsThat(Object that, String errMsg) {
+ return equalsThat(that, convertExceptionCreator(errMsg));
+ }
+
+ public TPropertyValidator equalsThat(
+ Object that, Supplier exceptionCreator) {
+ return equalsThat(that, convertExceptionCreator(exceptionCreator));
+ }
+
+ public TPropertyValidator equalsThat(
+ Object that, Function exceptionCreator) {
+ withRule(value -> Objects.equals(value, that), exceptionCreator);
+ return thisObject();
+ }
+
+ // ===== isTrue =====
+
+ public TPropertyValidator isTrue(Predicate condition) {
+ return isTrue(condition, "无效的用户输入");
+ }
+
+ public TPropertyValidator isTrue(Predicate condition, String errMsg) {
+ return isTrue(condition, convertExceptionCreator(errMsg));
+ }
+
+ public TPropertyValidator isTrue(
+ Predicate condition,
+ Supplier exceptionCreator) {
+ return isTrue(condition, convertExceptionCreator(exceptionCreator));
+ }
+
+ public TPropertyValidator isTrue(
+ Predicate condition,
+ Function exceptionCreator) {
+ withRule(condition, exceptionCreator);
+ return thisObject();
+ }
+
+ // ===== isTrue =====
+
+ public TPropertyValidator isTrue(Collection> conditions) {
+ return isTrue(conditions, "无效的用户输入");
+ }
+
+ public TPropertyValidator isTrue(Collection> conditions, String errMsg) {
+ return isTrue(conditions, convertExceptionCreator(errMsg));
+ }
+
+ public TPropertyValidator isTrue(
+ Collection> conditions,
+ Supplier exceptionCreator) {
+ return isTrue(conditions, convertExceptionCreator(exceptionCreator));
+ }
+
+ public TPropertyValidator isTrue(
+ Collection> conditions,
+ Function exceptionCreator) {
+ for (Predicate condition : conditions) {
+ withRule(condition, exceptionCreator);
+ }
+ return thisObject();
+ }
+
+ static Function convertExceptionCreator(String errMsg) {
+ return value -> new IllegalArgumentException(errMsg);
+ }
+
+ static Function convertExceptionCreator(Supplier exceptionSupplier) {
+ return value -> exceptionSupplier.get();
+ }
+}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
index 847a01d..655b9f4 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java
@@ -10,18 +10,17 @@ import java.util.function.Supplier;
public class BaseValidator {
private final List> rules = new ArrayList<>();
- private final List> propertyValidators = new ArrayList<>();
protected void withRule(final Predicate rule, final String errorMessage) {
- withRule(rule, () -> InvalidInputException.of(errorMessage));
+ withRule(rule, () -> new IllegalArgumentException(errorMessage));
}
protected void withRule(Predicate rule, Supplier exceptionBuilder) {
withRule(rule, value -> exceptionBuilder.get());
}
- protected void withRule(Predicate condition,
- Function exceptionBuilder) {
+ protected void withRule(
+ Predicate condition, Function exceptionBuilder) {
withRule(value -> {
if (!condition.test(value)) {
throw exceptionBuilder.apply(value);
@@ -34,47 +33,42 @@ public class BaseValidator {
}
protected final ObjectValidator ruleFor(Function getter) {
- ObjectValidator validValueHolder = new ObjectValidator<>(getter);
- propertyValidators.add(validValueHolder);
- return validValueHolder;
+ ObjectValidator validator = new ObjectValidator<>(getter);
+ this.rules.add(validator::validate);
+ return validator;
}
protected final IntValidator ruleForInt(Function getter) {
- IntValidator validValueHolder = new IntValidator<>(getter);
- propertyValidators.add(validValueHolder);
- return validValueHolder;
+ IntValidator validator = new IntValidator<>(getter);
+ this.rules.add(validator::validate);
+ return validator;
}
protected final DoubleValidator ruleForDouble(Function getter) {
- DoubleValidator validValueHolder = new DoubleValidator<>(getter);
- propertyValidators.add(validValueHolder);
- return validValueHolder;
+ DoubleValidator validator = new DoubleValidator<>(getter);
+ this.rules.add(validator::validate);
+ return validator;
}
protected final BoolValidator ruleForBool(Function getter) {
- BoolValidator validValueHolder = new BoolValidator<>(getter);
- propertyValidators.add(validValueHolder);
- return validValueHolder;
+ BoolValidator validator = new BoolValidator<>(getter);
+ this.rules.add(validator::validate);
+ return validator;
}
protected final StringValidator ruleForString(Function getter) {
- StringValidator validValueHolder = new StringValidator<>(getter);
- propertyValidators.add(validValueHolder);
- return validValueHolder;
+ StringValidator validator = new StringValidator<>(getter);
+ this.rules.add(validator::validate);
+ return validator;
}
protected final CollectionValidator ruleForCollection(Function> getter) {
- CollectionValidator validValueHolder = new CollectionValidator<>(getter);
- propertyValidators.add(validValueHolder);
- return validValueHolder;
+ CollectionValidator validator = new CollectionValidator<>(getter);
+ this.rules.add(validator::validate);
+ return validator;
}
public void validate(T obj) {
- for (Consumer rule : this.rules) {
- rule.accept(obj);
- }
- for (PropertyValidator valueValidator : this.propertyValidators) {
- valueValidator.validate(obj);
- }
+ this.rules.forEach(rule -> rule.accept(obj));
}
}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java
index fa408f6..726c164 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java
@@ -3,9 +3,7 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
-import org.apache.commons.lang3.BooleanUtils;
-
-public class BoolValidator extends PropertyValidator> {
+public class BoolValidator extends BasePropertyValidator> {
BoolValidator(Function getter) {
super(getter);
@@ -27,7 +25,7 @@ public class BoolValidator extends PropertyValidator BoolValidator isTrue(
Function exceptionCreator) {
- withRule(BooleanUtils::isTrue, exceptionCreator);
+ withRule(Boolean.TRUE::equals, exceptionCreator);
return this;
}
@@ -47,7 +45,7 @@ public class BoolValidator extends PropertyValidator BoolValidator isFalse(
Function exceptionCreator) {
- withRule(BooleanUtils::isFalse, exceptionCreator);
+ withRule(Boolean.FALSE::equals, exceptionCreator);
return this;
}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java
index cb434a1..afbd02e 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java
@@ -4,7 +4,10 @@ import java.util.Collection;
import java.util.function.Function;
import java.util.function.Supplier;
-public class CollectionValidator extends PropertyValidator, CollectionValidator> {
+import xyz.zhouxy.plusone.commons.collection.CollectionTools;
+
+public class CollectionValidator
+ extends BasePropertyValidator, CollectionValidator> {
CollectionValidator(Function> getter) {
super(getter);
@@ -22,7 +25,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator notEmpty(
Function, E> exceptionCreator) {
- withRule(value -> value != null && !value.isEmpty(), exceptionCreator);
+ withRule(CollectionTools::isNotEmpty, exceptionCreator);
return this;
}
@@ -38,7 +41,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator isEmpty(
Function, E> exceptionCreator) {
- withRule(value -> value == null || value.isEmpty(), exceptionCreator);
+ withRule(CollectionTools::isEmpty, exceptionCreator);
return this;
}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java
index 4174e79..f2bbf4e 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java
@@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
-public class DoubleValidator extends PropertyValidator> {
+public class DoubleValidator extends BasePropertyValidator> {
DoubleValidator(Function getter) {
super(getter);
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java
index 1cdf093..1250f95 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java
@@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
-public class IntValidator extends PropertyValidator> {
+public class IntValidator extends BasePropertyValidator> {
IntValidator(Function getter) {
super(getter);
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 1bc1c30..0000000
--- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package xyz.zhouxy.plusone.validator;
-
-import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException;
-
-/**
- * 4040000 - 用户请求参数错误
- *
- * @author ZhouXY
- */
-public class InvalidInputException extends BaseRuntimeException {
-
- private static final long serialVersionUID = 7956661913360059670L;
-
- public static final String ERROR_CODE = "4040000";
-
- protected InvalidInputException(String code, String msg) {
- super(code, msg);
- }
-
- protected InvalidInputException(String code, Throwable cause) {
- super(code, cause);
- }
-
- protected InvalidInputException(String code, String msg, Throwable cause) {
- super(code, msg, cause);
- }
-
- public static InvalidInputException of(String msg) {
- return new InvalidInputException(ERROR_CODE, msg);
- }
-
- public static InvalidInputException of(Throwable cause) {
- return new InvalidInputException(ERROR_CODE, cause);
- }
-
- public static InvalidInputException of(String msg, Throwable cause) {
- return new InvalidInputException(ERROR_CODE, msg, cause);
- }
-}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java
index 6f2de5f..3941bc0 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java
@@ -2,7 +2,7 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
-public class ObjectValidator extends PropertyValidator> {
+public class ObjectValidator extends BasePropertyValidator> {
ObjectValidator(Function getter) {
super(getter);
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java
deleted file mode 100644
index 2dafff3..0000000
--- a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java
+++ /dev/null
@@ -1,155 +0,0 @@
-package xyz.zhouxy.plusone.validator;
-
-import java.util.Collection;
-import java.util.Objects;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.function.Supplier;
-
-abstract class PropertyValidator {
- Function getter;
- Validator validator = new Validator<>();
-
- PropertyValidator(Function getter) {
- this.getter = getter;
- }
-
- void withRule(Predicate condition,
- Function exceptionCreator) {
- withRule(value -> {
- if (!condition.test(value)) {
- throw exceptionCreator.apply(value);
- }
- });
- }
-
- private void withRule(Consumer rule) {
- this.validator.addRule(rule);
- }
-
- // ====================
- // ====== Object ======
- // ====================
-
- // ====== notNull =====
-
- public THIS notNull() {
- return notNull("Value could not be null.");
- }
-
- public THIS notNull(String errMsg) {
- return notNull(convertExceptionCreator(errMsg));
- }
-
- public THIS notNull(Supplier exceptionCreator) {
- return notNull(convertExceptionCreator(exceptionCreator));
- }
-
- public THIS notNull(Function exceptionCreator) {
- withRule(Objects::nonNull, exceptionCreator);
- return thisObject();
- }
-
- // ====== isNull =====
-
- public THIS isNull(String errMsg) {
- return isNull(convertExceptionCreator(errMsg));
- }
-
- public THIS isNull(Supplier exceptionCreator) {
- return isNull(convertExceptionCreator(exceptionCreator));
- }
-
- public THIS isNull(Function exceptionCreator) {
- withRule(Objects::isNull, exceptionCreator);
- return thisObject();
- }
-
- // ===== equals =====
-
- public THIS equalsThat(Object that) {
- return equalsThat(that, value -> InvalidInputException.of(String.format("(%s) 必须与 (%s) 相等", value, that)));
- }
-
- public THIS equalsThat(Object that, String errMsg) {
- return equalsThat(that, convertExceptionCreator(errMsg));
- }
-
- public THIS equalsThat(
- Object that, Supplier exceptionCreator) {
- return equalsThat(that, convertExceptionCreator(exceptionCreator));
- }
-
- public THIS equalsThat(
- Object that, Function exceptionCreator) {
- withRule(value -> Objects.equals(value, that), exceptionCreator);
- return thisObject();
- }
-
- // ===== isTrue =====
-
- public THIS isTrue(Predicate condition) {
- return isTrue(condition, "无效的用户输入");
- }
-
- public THIS isTrue(Predicate condition, String errMsg) {
- return isTrue(condition, convertExceptionCreator(errMsg));
- }
-
- public THIS isTrue(
- Predicate condition,
- Supplier exceptionCreator) {
- return isTrue(condition, convertExceptionCreator(exceptionCreator));
- }
-
- public THIS isTrue(
- Predicate condition,
- Function exceptionCreator) {
- withRule(condition, exceptionCreator);
- return thisObject();
- }
-
- // ===== isTrue =====
-
- public THIS isTrue(Collection> conditions) {
- return isTrue(conditions, "无效的用户输入");
- }
-
- public THIS isTrue(Collection> conditions, String errMsg) {
- return isTrue(conditions, convertExceptionCreator(errMsg));
- }
-
- public THIS isTrue(
- Collection> conditions,
- Supplier exceptionCreator) {
- return isTrue(conditions, convertExceptionCreator(exceptionCreator));
- }
-
- public THIS isTrue(
- Collection> conditions,
- Function exceptionCreator) {
- for (Predicate condition : conditions) {
- withRule(condition, exceptionCreator);
- }
- return thisObject();
- }
-
- // ========================================================================
-
- void validate(DTO obj) {
- PROPERTY value = this.getter.apply(obj);
- this.validator.validate(value);
- }
-
- static Function convertExceptionCreator(String errMsg) {
- return value -> InvalidInputException.of(errMsg);
- }
-
- static Function convertExceptionCreator(
- Supplier exceptionSupplier) {
- return value -> exceptionSupplier.get();
- }
-
- protected abstract THIS thisObject();
-}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java
index 77824e3..c102318 100644
--- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java
+++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java
@@ -6,12 +6,12 @@ import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
-import org.apache.commons.lang3.StringUtils;
+import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
-import xyz.zhouxy.plusone.commons.util.RegexUtil;
+import xyz.zhouxy.plusone.commons.util.RegexTools;
-public class StringValidator extends PropertyValidator> {
+public class StringValidator extends BasePropertyValidator> {
StringValidator(Function getter) {
super(getter);
@@ -36,7 +36,7 @@ public class StringValidator extends PropertyValidator StringValidator matches(
Pattern regex,
Function exceptionCreator) {
- withRule(input -> RegexUtil.matches(input, regex), exceptionCreator);
+ withRule(input -> RegexTools.matches(input, regex), exceptionCreator);
return this;
}
@@ -55,7 +55,7 @@ public class StringValidator extends PropertyValidator StringValidator matchesOne(
Pattern[] regexs,
Function exceptionCreator) {
- withRule(input -> RegexUtil.matchesOne(input, regexs), exceptionCreator);
+ withRule(input -> RegexTools.matchesOne(input, regexs), exceptionCreator);
return this;
}
@@ -72,7 +72,7 @@ public class StringValidator extends PropertyValidator StringValidator matchesOne(
List regexs,
Function exceptionCreator) {
- withRule(input -> RegexUtil.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
+ withRule(input -> RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this;
}
@@ -91,7 +91,7 @@ public class StringValidator extends PropertyValidator StringValidator matchesAll(
Pattern[] regexs,
Function exceptionCreator) {
- withRule(input -> RegexUtil.matchesAll(input, regexs), exceptionCreator);
+ withRule(input -> RegexTools.matchesAll(input, regexs), exceptionCreator);
return this;
}
@@ -108,12 +108,24 @@ public class StringValidator extends PropertyValidator StringValidator matchesAll(
Collection regexs,
Function exceptionCreator) {
- withRule(input -> RegexUtil.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
+ withRule(input -> RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this;
}
// ===== notBlank =====
+ static boolean isNotBlank(final String cs) {
+ if (cs == null || cs.isEmpty()) {
+ return false;
+ }
+ for (int i = 0; i < cs.length(); i++) {
+ if (!Character.isWhitespace(cs.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
public StringValidator notBlank() {
return notBlank("This String argument must have text; it must not be null, empty, or blank");
}
@@ -128,7 +140,7 @@ public class StringValidator extends PropertyValidator StringValidator notBlank(
Function exceptionCreator) {
- withRule(StringUtils::isNotBlank, exceptionCreator);
+ withRule(StringValidator::isNotBlank, exceptionCreator);
return this;
}
@@ -162,23 +174,66 @@ public class StringValidator extends PropertyValidator StringValidator notEmpty(
Function exceptionCreator) {
- withRule(StringUtils::isNotEmpty, exceptionCreator);
+ withRule(s -> s != null && !s.isEmpty(), exceptionCreator);
return this;
}
- // ====== isEmpty =====
+ // ====== isNullOrEmpty =====
- public StringValidator isEmpty(String errMsg) {
- return isEmpty(convertExceptionCreator(errMsg));
+ public StringValidator isNullOrEmpty(String errMsg) {
+ return isNullOrEmpty(convertExceptionCreator(errMsg));
}
- public StringValidator isEmpty(Supplier exceptionCreator) {
- return isEmpty(convertExceptionCreator(exceptionCreator));
+ public StringValidator isNullOrEmpty(Supplier exceptionCreator) {
+ return isNullOrEmpty(convertExceptionCreator(exceptionCreator));
}
- public StringValidator isEmpty(
+ public StringValidator isNullOrEmpty(
Function exceptionCreator) {
- withRule(StringUtils::isEmpty, exceptionCreator);
+ withRule(s -> s == null || s.isEmpty(), exceptionCreator);
+ return this;
+ }
+
+ // ====== length =====
+
+ public StringValidator length(int length, String errMsg) {
+ return length(length, convertExceptionCreator(errMsg));
+ }
+
+ public StringValidator length(int length,
+ Supplier exceptionCreator) {
+ return length(length, convertExceptionCreator(exceptionCreator));
+ }
+
+ public StringValidator length(int length,
+ Function exceptionCreator) {
+ Preconditions.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
+ withRule(s -> s != null && s.length() == length, exceptionCreator);
+ return this;
+ }
+
+ static boolean length(String str, int min, int max) {
+ if (str == null) {
+ return false;
+ }
+ final int len = str.length();
+ return len >= min && len < max;
+ }
+
+ public StringValidator length(int min, int max, String errMsg) {
+ return length(min, max, convertExceptionCreator(errMsg));
+ }
+
+ public StringValidator length(int min, int max,
+ Supplier exceptionCreator) {
+ return length(min, max, convertExceptionCreator(exceptionCreator));
+ }
+
+ public StringValidator length(int min, int max,
+ Function exceptionCreator) {
+ Preconditions.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.");
+ withRule(s -> length(s, min, max), exceptionCreator);
return this;
}
diff --git a/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java
new file mode 100644
index 0000000..fd42bb0
--- /dev/null
+++ b/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java
@@ -0,0 +1,22 @@
+package xyz.zhouxy.plusone.validator.map;
+
+import java.util.Map;
+
+import xyz.zhouxy.plusone.validator.BasePropertyValidator;
+
+public class EntryValidator
+ extends BasePropertyValidator