From 2fe6297b1da108c2c3735586e38833753c32c447 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 10 Sep 2023 21:45:30 +0800 Subject: [PATCH] Fix unintended changes to result --- .../MultiverseCore/utils/result/Result.java | 72 +++++++++++++++---- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/result/Result.java b/src/main/java/com/onarandombox/MultiverseCore/utils/result/Result.java index 03bcf789..6e029943 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/result/Result.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/result/Result.java @@ -6,18 +6,27 @@ import org.jetbrains.annotations.NotNull; import java.util.NoSuchElementException; import java.util.function.Consumer; +import java.util.function.Function; public sealed interface Result permits Result.Success, Result.Failure { + static Result success( - S successReason, MessageReplacement... replacements) { + S successReason, MessageReplacement...replacements) { return new Success<>(successReason, replacements); } + static Result success(S successReason, Message message) { + return new Success<>(successReason, message); + } + static Result failure( - F failureReason, MessageReplacement... replacements) { + F failureReason, MessageReplacement...replacements) { return new Failure<>(failureReason, replacements); } + static Result failure(F failureReason, Message message) { + return new Failure<>(failureReason, message); + } boolean isSuccess(); boolean isFailure(); @@ -28,16 +37,16 @@ public sealed interface Result @NotNull Message getReasonMessage(); - default Result onSuccess(Consumer consumer) { - if (this.isSuccess()) { - consumer.accept(this.getSuccessReason()); + default Result onSuccess(Consumer> consumer) { + if (this instanceof Success) { + consumer.accept((Success) this); } return this; } - default Result onFailure(Consumer consumer) { - if (this.isFailure()) { - consumer.accept(this.getFailureReason()); + default Result onFailure(Consumer> consumer) { + if (this instanceof Failure) { + consumer.accept((Failure) this); } return this; } @@ -56,13 +65,41 @@ public sealed interface Result return this; } + default Result onSuccessThen(Function, Result> function) { + if (this instanceof Success) { + return function.apply((Success) this); + } + return this; + } + + default Result onFailureThen(Function, Result> function) { + if (this instanceof Failure) { + return function.apply((Failure) this); + } + return this; + } + + default R fold(Function, R> failureFunc, Function, R> successFunc) { + if (this instanceof Failure) { + return failureFunc.apply((Failure) this); + } else if (this instanceof Success) { + return successFunc.apply((Success) this); + } + throw new IllegalStateException("Unknown result type: " + this.getClass().getName()); + } + final class Success implements Result { private final S successReason; - private final MessageReplacement[] replacements; + private final Message message; + + public Success(S successReason, Message message) { + this.successReason = successReason; + this.message = message; + } public Success(S successReason, MessageReplacement[] replacements) { this.successReason = successReason; - this.replacements = replacements; + this.message = Message.of(successReason, "Success!", replacements); } @Override @@ -87,7 +124,7 @@ public sealed interface Result @Override public @NotNull Message getReasonMessage() { - return Message.of(successReason, "Success!", replacements); + return message; } @Override @@ -100,11 +137,16 @@ public sealed interface Result final class Failure implements Result { private final F failureReason; - private final MessageReplacement[] replacements; + private final Message message; + + public Failure(F failureReason, Message message) { + this.failureReason = failureReason; + this.message = message; + } public Failure(F failureReason, MessageReplacement[] replacements) { this.failureReason = failureReason; - this.replacements = replacements; + this.message = Message.of(failureReason, "Success!", replacements); } @Override @@ -119,7 +161,7 @@ public sealed interface Result @Override public S getSuccessReason() { - throw new NoSuchElementException("No reason for success"); + throw new NoSuchElementException("No reason for failure"); } @Override @@ -129,7 +171,7 @@ public sealed interface Result @Override public @NotNull Message getReasonMessage() { - return Message.of(failureReason, "Success!", replacements); + return message; } @Override