From b03f978da9d505438e55143a851fd4a512aec91c Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 7 Aug 2024 17:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/validator/BaseValidator.java | 8 +-- .../plusone/validator/MapValidator.java | 60 +++---------------- 2 files changed, 12 insertions(+), 56 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index 655b9f4..79f9085 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -9,18 +9,18 @@ import java.util.function.Predicate; import java.util.function.Supplier; public class BaseValidator { - private final List> rules = new ArrayList<>(); + private final List> rules = new ArrayList<>(); protected void withRule(final Predicate rule, final String errorMessage) { withRule(rule, () -> new IllegalArgumentException(errorMessage)); } - protected void withRule(Predicate rule, Supplier exceptionBuilder) { + protected void withRule(Predicate rule, Supplier exceptionBuilder) { withRule(rule, value -> exceptionBuilder.get()); } protected void withRule( - Predicate condition, Function exceptionBuilder) { + Predicate condition, Function exceptionBuilder) { withRule(value -> { if (!condition.test(value)) { throw exceptionBuilder.apply(value); @@ -28,7 +28,7 @@ public class BaseValidator { }); } - protected void withRule(Consumer rule) { + protected void withRule(Consumer rule) { this.rules.add(rule); } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java index 151133e..f6161e2 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java @@ -2,19 +2,12 @@ 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<>(); +public abstract class MapValidator extends BaseValidator> { private final Set keys; @@ -48,68 +41,31 @@ public abstract class MapValidator { .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } - public final void validate(Map obj) { - this.consumers.forEach(consumer -> consumer.accept(obj)); - } - // ========== ruleFor ========== protected final ObjectValidator, V> ruleFor(K key) { - ObjectValidator, V> validator = new ObjectValidator<>(m -> m.get(key)); - this.consumers.add(validator::validate); - return validator; + return ruleFor(m -> m.get(key)); } protected final IntValidator> ruleForInt(K key) { - IntValidator> validator = new IntValidator<>(m -> (Integer) m.get(key)); - this.consumers.add(validator::validate); - return validator; + return ruleForInt(m -> (Integer) m.get(key)); } protected final DoubleValidator> ruleForDouble(K key) { - DoubleValidator> validator = new DoubleValidator<>(m -> (Double) m.get(key)); - this.consumers.add(validator::validate); - return validator; + return ruleForDouble(m -> (Double) m.get(key)); } protected final BoolValidator> ruleForBool(K key) { - BoolValidator> validator = new BoolValidator<>(m -> (Boolean) m.get(key)); - this.consumers.add(validator::validate); - return validator; + return ruleForBool(m -> (Boolean) m.get(key)); } protected final StringValidator> ruleForString(K key) { - StringValidator> validator = new StringValidator<>(m -> (String) m.get(key)); - this.consumers.add(validator::validate); - return validator; + return ruleForString(m -> (String) m.get(key)); } protected final CollectionValidator, E> ruleForCollection(K key) { @SuppressWarnings("unchecked") - CollectionValidator, E> validator = new CollectionValidator<>(m -> (Collection) m.get(key)); - this.consumers.add(validator::validate); - return validator; - } - - // ========== withRule ========== - - protected final void withRule(Predicate> rule, String errMsg) { - withRule(rule, map -> new IllegalArgumentException(errMsg)); - } - - protected final void withRule(Predicate> rule, String errMsgFormat, Object... args) { - withRule(rule, map -> new IllegalArgumentException(String.format(errMsgFormat, args))); - } - - protected final void withRule(Predicate> rule, Supplier e) { - withRule(rule, map -> e.get()); - } - - protected final void withRule(Predicate> rule, Function, E> e) { - this.consumers.add(map -> { - if (!rule.test(map)) { - throw e.apply(map); - } - }); + Function, Collection> getter = m -> (Collection) m.get(key); + return ruleForCollection(getter); } }