From 3f23d5383d89a813d045ceec10e809429feef3b4 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 1 Jun 2025 08:56:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=BB=98=E8=AE=A4=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20`SimpleImmutableEntry`=20=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E4=BA=8C=E5=85=83=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 `MapValidator` 类中,将 `SimpleEntry` 替换为 `SimpleImmutableEntry`,作为二元组的默认实现。 - 在 `PairPropertyValidatorTests` 类中,做同样的替换。 --- .../xyz/zhouxy/plusone/validator/MapValidator.java | 5 +++-- .../validator/PairPropertyValidatorTests.java | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java index dabe9a4..859ea8e 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java @@ -16,7 +16,7 @@ package xyz.zhouxy.plusone.validator; -import java.util.AbstractMap.SimpleEntry; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -205,7 +205,8 @@ public abstract class MapValidator extends BaseValidator> { protected final PairPropertyValidator, V1, V2> ruleForPair(K k1, K k2) { @SuppressWarnings("unchecked") - Function, Entry> getter = m -> new SimpleEntry((V1) m.get(k1), (V2) m.get(k2)); + Function, Entry> getter = m -> + new SimpleImmutableEntry<>((V1) m.get(k1), (V2) m.get(k2)); return ruleForPair(getter); } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java index 7b7ed03..29c61d1 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java @@ -21,7 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Objects; -import java.util.AbstractMap.SimpleEntry; +import java.util.AbstractMap.SimpleImmutableEntry; import org.junit.jupiter.api.Test; @@ -43,7 +43,7 @@ public class PairPropertyValidatorTests { void must_validInput() { IValidator validator = new BaseValidator() { { - ruleForPair((ExampleCommand command) -> new SimpleEntry(command.getStringProperty(), command.getIntProperty())) + ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .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()), () -> ExampleException.withMessage(MESSAGE)) @@ -60,7 +60,7 @@ public class PairPropertyValidatorTests { void must_default_invalidInput() { IValidator validator = new BaseValidator() { { - ruleForPair((ExampleCommand command) -> new SimpleEntry(command.getStringProperty(), command.getIntProperty())) + ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString())); } }; @@ -75,7 +75,7 @@ public class PairPropertyValidatorTests { void must_message_invalidInput() { IValidator validator = new BaseValidator() { { - ruleForPair((ExampleCommand command) -> new SimpleEntry(command.getStringProperty(), command.getIntProperty())) + ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE); } }; @@ -89,7 +89,7 @@ public class PairPropertyValidatorTests { void must_exceptionSupplier_invalidInput() { IValidator validator = new BaseValidator() { { - ruleForPair((ExampleCommand command) -> new SimpleEntry(command.getStringProperty(), command.getIntProperty())) + ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE)); } }; @@ -104,7 +104,7 @@ public class PairPropertyValidatorTests { void must_exceptionFunction_invalidInput() { IValidator validator = new BaseValidator() { { - ruleForPair((ExampleCommand command) -> new SimpleEntry(command.getStringProperty(), command.getIntProperty())) + ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString()), (str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue)); }