refactor!: 优化二元组校验的 API 设计

This commit is contained in:
2025-07-25 10:57:47 +08:00
parent 09d596b599
commit 2977c9a7fb
3 changed files with 25 additions and 22 deletions

View File

@@ -281,8 +281,12 @@ public abstract class BaseValidator<T> implements IValidator<T> {
* @param <V2> 第二个元素的类型 * @param <V2> 第二个元素的类型
* @param getter 根据对象构造一个二元组,通常是两个属性的值。 * @param getter 根据对象构造一个二元组,通常是两个属性的值。
* @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。 * @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。
*
* @deprecated 请使用 {@link #ruleFor(Function, Function)} 代替。
*/ */
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleForPair(Function<T, Entry<V1, V2>> getter) { @Deprecated
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleForPair( // NOSONAR
Function<T, Entry<V1, V2>> getter) {
PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>(getter); PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>(getter);
this.rules.add(validator::validate); this.rules.add(validator::validate);
return validator; return validator;
@@ -291,15 +295,14 @@ public abstract class BaseValidator<T> implements IValidator<T> {
/** /**
* 添加一个针对二元组的校验器 * 添加一个针对二元组的校验器
* *
* @param <V1> 第个元素的类型 * @param <V1> 第1个元素的类型
* @param <V2> 第个元素的类型 * @param <V2> 第2个元素的类型
* @param v1Getter 用于从目标对象获取第个元素的函数式接口。示例:{@code Person::getName1}。 * @param v1Getter 用于从目标对象获取第1个元素的函数式接口。示例:{@code Person::getName1}。
* @param v2Getter 用于从目标对象获取第个元素的函数式接口。示例:{@code Person::getName2}。 * @param v2Getter 用于从目标对象获取第2个元素的函数式接口。示例:{@code Person::getName2}。
* @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。 * @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。
*/ */
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleForPair( protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleFor(
Function<T, V1> v1Getter, Function<T, V1> v1Getter, Function<T, V2> v2Getter) {
Function<T, V2> v2Getter) {
PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>( PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>(
t -> new SimpleImmutableEntry<>(v1Getter.apply(t), v2Getter.apply(t))); t -> new SimpleImmutableEntry<>(v1Getter.apply(t), v2Getter.apply(t)));
this.rules.add(validator::validate); this.rules.add(validator::validate);

View File

@@ -16,11 +16,9 @@
package xyz.zhouxy.plusone.validator; package xyz.zhouxy.plusone.validator;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -196,18 +194,19 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
/** /**
* 添加一个属性校验器,对指定的两个 key 对应的 value 进行校验 * 添加一个属性校验器,对指定的两个 key 对应的 value 进行校验
* @param <V1> 第个属性的类型 * @param <V1> 第1个属性的类型
* @param <V2> 第个属性的类型 * @param <V2> 第2个属性的类型
* @param k1 第个 key * @param k1 第1个 key
* @param k2 第个 key * @param k2 第2个 key
* @return 属性校验器 * @return 属性校验器
*/ */
protected final <V1 extends V, V2 extends V> protected final <V1 extends V, V2 extends V>
PairPropertyValidator<Map<K, V>, V1, V2> ruleForPair(K k1, K k2) { PairPropertyValidator<Map<K, V>, V1, V2> ruleForPair(K k1, K k2) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Function<Map<K, V>, Entry<V1, V2>> getter = m -> final Function<Map<K, V>, V1> v1Getter = m -> (V1) m.get(k1);
new SimpleImmutableEntry<>((V1) m.get(k1), (V2) m.get(k2)); @SuppressWarnings("unchecked")
return ruleForPair(getter); final Function<Map<K, V>, V2> v2Getter = m -> (V2) m.get(k2);
return ruleFor(v1Getter, v2Getter);
} }
// ================================ // ================================

View File

@@ -31,6 +31,7 @@ import xyz.zhouxy.plusone.validator.BaseValidator;
import xyz.zhouxy.plusone.validator.IValidator; import xyz.zhouxy.plusone.validator.IValidator;
import xyz.zhouxy.plusone.validator.ValidationException; import xyz.zhouxy.plusone.validator.ValidationException;
@SuppressWarnings("deprecation")
public class PairPropertyValidatorTests { public class PairPropertyValidatorTests {
static final String MESSAGE = "Validation failed."; static final String MESSAGE = "Validation failed.";
@@ -57,7 +58,7 @@ public class PairPropertyValidatorTests {
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() { IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
{ {
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
.must((str, intValue) -> Objects.equals(str, intValue.toString())) .must((str, intValue) -> Objects.equals(str, intValue.toString()))
.must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE) .must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE)
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE)) .must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE))
@@ -83,7 +84,7 @@ public class PairPropertyValidatorTests {
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() { IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
{ {
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
.must((str, intValue) -> Objects.equals(str, intValue.toString())); .must((str, intValue) -> Objects.equals(str, intValue.toString()));
} }
}; };
@@ -106,7 +107,7 @@ public class PairPropertyValidatorTests {
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() { IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
{ {
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
.must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE); .must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE);
} }
}; };
@@ -129,7 +130,7 @@ public class PairPropertyValidatorTests {
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() { IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
{ {
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE)); .must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE));
} }
}; };
@@ -153,7 +154,7 @@ public class PairPropertyValidatorTests {
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() { IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
{ {
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
.must((str, intValue) -> Objects.equals(str, intValue.toString()), .must((str, intValue) -> Objects.equals(str, intValue.toString()),
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue)); (str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
} }