refactor: 将方法调用与返回语句合并,简化了代码结构

This commit is contained in:
zhouxy108 2025-06-01 20:16:42 +08:00
parent 907d883be8
commit 0a83116e81
5 changed files with 24 additions and 47 deletions

View File

@ -74,8 +74,7 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isTrueValue(Function<Boolean, E> e) {
withRule(Boolean.TRUE::equals, e);
return this;
return withRule(Boolean.TRUE::equals, e);
}
// ====== isFalseValue ======
@ -118,8 +117,7 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
* @return 属性校验器
*/
public <E extends RuntimeException> BoolPropertyValidator<T> isFalseValue(Function<Boolean, E> e) {
withRule(Boolean.FALSE::equals, e);
return this;
return withRule(Boolean.FALSE::equals, e);
}
@Override

View File

@ -80,8 +80,7 @@ public class DoublePropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> gt(double min, Function<Double, E> e) {
withRule(value -> (value != null && value > min), e);
return this;
return withRule(value -> (value != null && value > min), e);
}
// ================================
@ -134,8 +133,7 @@ public class DoublePropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> ge(double min, Function<Double, E> e) {
withRule(value -> (value != null && value >= min), e);
return this;
return withRule(value -> (value != null && value >= min), e);
}
// ================================
@ -188,8 +186,7 @@ public class DoublePropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> lt(double max, Function<Double, E> e) {
withRule(value -> (value != null && value < max), e);
return this;
return withRule(value -> (value != null && value < max), e);
}
// ================================
@ -242,8 +239,7 @@ public class DoublePropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> DoublePropertyValidator<T> le(double max, Function<Double, E> e) {
withRule(value -> (value != null && value <= max), e);
return this;
return withRule(value -> (value != null && value <= max), e);
}
// ================================

View File

@ -80,8 +80,7 @@ public class IntPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> gt(int min, Function<Integer, E> e) {
withRule(value -> (value != null && value > min), e);
return this;
return withRule(value -> (value != null && value > min), e);
}
// ================================
@ -134,8 +133,7 @@ public class IntPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> ge(int min, Function<Integer, E> e) {
withRule(value -> (value != null && value >= min), e);
return this;
return withRule(value -> (value != null && value >= min), e);
}
// ================================
@ -188,8 +186,7 @@ public class IntPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> lt(int max, Function<Integer, E> e) {
withRule(value -> (value != null && value < max), e);
return this;
return withRule(value -> (value != null && value < max), e);
}
// ================================
@ -242,8 +239,7 @@ public class IntPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> IntPropertyValidator<T> le(int max, Function<Integer, E> e) {
withRule(value -> (value != null && value <= max), e);
return this;
return withRule(value -> (value != null && value <= max), e);
}
// ================================

View File

@ -80,8 +80,7 @@ public class LongPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> gt(long min, Function<Long, E> e) {
withRule(value -> (value != null && value > min), e);
return this;
return withRule(value -> (value != null && value > min), e);
}
// ================================
@ -134,8 +133,7 @@ public class LongPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> ge(long min, Function<Long, E> e) {
withRule(value -> (value != null && value >= min), e);
return this;
return withRule(value -> (value != null && value >= min), e);
}
// ================================
@ -188,8 +186,7 @@ public class LongPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> lt(long max, Function<Long, E> e) {
withRule(value -> (value != null && value < max), e);
return this;
return withRule(value -> (value != null && value < max), e);
}
// ================================
@ -242,8 +239,7 @@ public class LongPropertyValidator<T>
* @return 属性校验器
*/
public <E extends RuntimeException> LongPropertyValidator<T> le(long max, Function<Long, E> e) {
withRule(value -> (value != null && value <= max), e);
return this;
return withRule(value -> (value != null && value <= max), e);
}
// ================================

View File

@ -80,8 +80,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*/
public <E extends RuntimeException> StringPropertyValidator<T> matches(
Pattern regex, Function<String, E> e) {
withRule(input -> (input == null || RegexTools.matches(input, regex)), e);
return this;
return withRule(input -> (input == null || RegexTools.matches(input, regex)), e);
}
// ================================
@ -126,8 +125,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
Pattern[] regexs, Function<String, E> e) {
withRule(input -> input == null || RegexTools.matchesOne(input, regexs), e);
return this;
return withRule(input -> input == null || RegexTools.matchesOne(input, regexs), e);
}
/**
@ -164,8 +162,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesOne(
List<Pattern> regexs, Function<String, E> e) {
withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), e);
return this;
return withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), e);
}
// ================================
@ -210,8 +207,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Pattern[] regexs, Function<String, E> e) {
withRule(input -> input == null || RegexTools.matchesAll(input, regexs), e);
return this;
return withRule(input -> input == null || RegexTools.matchesAll(input, regexs), e);
}
/**
@ -248,8 +244,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*/
public <E extends RuntimeException> StringPropertyValidator<T> matchesAll(
Collection<Pattern> regexs, Function<String, E> e) {
withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), e);
return this;
return withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), e);
}
// ================================
@ -298,8 +293,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
* @return 属性校验器
*/
public <E extends RuntimeException> StringPropertyValidator<T> notBlank(Function<String, E> e) {
withRule(StringTools::isNotBlank, e);
return this;
return withRule(StringTools::isNotBlank, e);
}
// ================================
@ -399,8 +393,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
*/
public <E extends RuntimeException> StringPropertyValidator<T> notEmpty(
Function<String, E> e) {
withRule(s -> s != null && !s.isEmpty(), e);
return this;
return withRule(s -> s != null && !s.isEmpty(), e);
}
// ================================
@ -445,11 +438,10 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
public <E extends RuntimeException> StringPropertyValidator<T> length(int length, Function<String, E> e) {
AssertTools.checkArgument(length >= 0,
"The expected length must be greater than or equal to 0.");
withRule(s -> s == null || s.length() == length, e);
return this;
return withRule(s -> s == null || s.length() == length, e);
}
static boolean length(String str, int min, int max) {
static boolean checkLength(String str, int min, int max) {
if (str == null) {
return true;
}
@ -494,8 +486,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
Function<String, E> e) {
AssertTools.checkArgument(min >= 0, "min must be non-negative.");
AssertTools.checkArgument(min <= max, "min must be less than or equal to max.");
withRule(s -> length(s, min, max), e);
return this;
return withRule(s -> checkLength(s, min, max), e);
}
// ================================