From 302c22f6903b93085eafcca782320e04f6b68048 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Tue, 11 Apr 2023 22:17:26 +0800 Subject: [PATCH] chore: Seal result class, throw NoSuchElementException, fix consumer typing --- .../MultiverseCore/utils/result/Result.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 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 fab68fde..df79f10c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/result/Result.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/result/Result.java @@ -1,9 +1,9 @@ package com.onarandombox.MultiverseCore.utils.result; +import java.util.NoSuchElementException; import java.util.function.Consumer; -public interface Result { - +public sealed interface Result permits Result.Success, Result.Failure { static Result success(S successReason) { return new Success<>(successReason); } @@ -20,28 +20,28 @@ public interface Result { F failureReason(); - default Result success(Consumer consumer) { + default Result success(Consumer consumer) { if (this.isSuccess()) { consumer.accept(this.successReason()); } return this; } - default Result failure(Consumer consumer) { + default Result failure(Consumer consumer) { if (this.isFailure()) { consumer.accept(this.failureReason()); } return this; } - default Result successWithReason(S successReason, Consumer consumer) { + default Result successWithReason(S successReason, Consumer consumer) { if (this.isSuccess() && this.successReason() == successReason) { consumer.accept(this.successReason()); } return this; } - default Result failureWithReason(F failureReason, Consumer consumer) { + default Result failureWithReason(F failureReason, Consumer consumer) { if (this.isFailure() && this.failureReason() == failureReason) { consumer.accept(this.failureReason()); } @@ -61,7 +61,7 @@ public interface Result { @Override public F failureReason() { - return null; + throw new NoSuchElementException("No failure reason for success"); } } @@ -78,7 +78,7 @@ public interface Result { @Override public S successReason() { - return null; + throw new NoSuchElementException("No success reason for failure"); } } }