From 10893388c7755e7d43d7ff35753fedadc2849588 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 17 Aug 2022 18:11:34 +0200 Subject: [PATCH 1/3] Update AbstractValidator --- .../steamwar/command/AbstractValidator.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/de/steamwar/command/AbstractValidator.java b/src/de/steamwar/command/AbstractValidator.java index 60efd19..4614681 100644 --- a/src/de/steamwar/command/AbstractValidator.java +++ b/src/de/steamwar/command/AbstractValidator.java @@ -19,6 +19,12 @@ package de.steamwar.command; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; + +import java.security.Provider; +import java.util.function.*; + @FunctionalInterface public interface AbstractValidator { @@ -32,8 +38,52 @@ public interface AbstractValidator { */ boolean validate(K sender, T value, MessageSender messageSender); + default Validator validate(C value, MessageSender messageSender) { + return new Validator<>(value, messageSender); + } + + @RequiredArgsConstructor + class Validator { + private final C value; + private final MessageSender messageSender; + + private boolean valid = true; + + public Validator map(Function mapper) { + return new Validator<>(mapper.apply(value), messageSender).and(ignore -> valid); + } + + public Validator and(Predicate predicate) { + valid &= predicate.test(value); + return this; + } + + public Validator or(Predicate predicate) { + valid |= predicate.test(value); + return this; + } + + public Validator errorMessage(String s, Object... args) { + if (!valid) messageSender.send(s, args); + return this; + } + + public boolean result() { + return valid; + } + } + @FunctionalInterface interface MessageSender { void send(String s, Object... args); + + default boolean send(boolean condition, String s, Object... args) { + if (condition) send(s, args); + return condition; + } + + default boolean send(BooleanSupplier condition, String s, Object... args) { + return send(condition.getAsBoolean(), s, args); + } } } -- 2.39.2 From a4ac3fa8f011c2d683b84099d5aa7efd3da86c54 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 17 Aug 2022 18:25:08 +0200 Subject: [PATCH 2/3] Remove unused imports --- src/de/steamwar/command/AbstractValidator.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/command/AbstractValidator.java b/src/de/steamwar/command/AbstractValidator.java index 4614681..b0340e3 100644 --- a/src/de/steamwar/command/AbstractValidator.java +++ b/src/de/steamwar/command/AbstractValidator.java @@ -19,11 +19,11 @@ package de.steamwar.command; -import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; -import java.security.Provider; -import java.util.function.*; +import java.util.function.BooleanSupplier; +import java.util.function.Function; +import java.util.function.Predicate; @FunctionalInterface public interface AbstractValidator { -- 2.39.2 From 21cb3b266a67f8f14f6d68e260a793de2f2f9141 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 17 Aug 2022 18:35:59 +0200 Subject: [PATCH 3/3] Add AbstractValidator.Validator.set --- src/de/steamwar/command/AbstractValidator.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/command/AbstractValidator.java b/src/de/steamwar/command/AbstractValidator.java index b0340e3..aab4876 100644 --- a/src/de/steamwar/command/AbstractValidator.java +++ b/src/de/steamwar/command/AbstractValidator.java @@ -50,7 +50,12 @@ public interface AbstractValidator { private boolean valid = true; public Validator map(Function mapper) { - return new Validator<>(mapper.apply(value), messageSender).and(ignore -> valid); + return new Validator<>(mapper.apply(value), messageSender).set(valid); + } + + public Validator set(boolean value) { + this.valid = value; + return this; } public Validator and(Predicate predicate) { -- 2.39.2