mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-26 04:25:37 +01:00
Fix unintended changes to result
This commit is contained in:
parent
c8fed72137
commit
2fe6297b1d
@ -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<S extends SuccessReason, F extends FailureReason> permits Result.Success, Result.Failure {
|
||||
|
||||
static <F extends FailureReason, S extends SuccessReason> Result<S, F> success(
|
||||
S successReason, MessageReplacement... replacements) {
|
||||
S successReason, MessageReplacement...replacements) {
|
||||
return new Success<>(successReason, replacements);
|
||||
}
|
||||
|
||||
static <F extends FailureReason, S extends SuccessReason> Result<S, F> success(S successReason, Message message) {
|
||||
return new Success<>(successReason, message);
|
||||
}
|
||||
|
||||
static <F extends FailureReason, S extends SuccessReason> Result<S, F> failure(
|
||||
F failureReason, MessageReplacement... replacements) {
|
||||
F failureReason, MessageReplacement...replacements) {
|
||||
return new Failure<>(failureReason, replacements);
|
||||
}
|
||||
|
||||
static <F extends FailureReason, S extends SuccessReason> Result<S, F> failure(F failureReason, Message message) {
|
||||
return new Failure<>(failureReason, message);
|
||||
}
|
||||
boolean isSuccess();
|
||||
|
||||
boolean isFailure();
|
||||
@ -28,16 +37,16 @@ public sealed interface Result<S extends SuccessReason, F extends FailureReason>
|
||||
|
||||
@NotNull Message getReasonMessage();
|
||||
|
||||
default Result<S, F> onSuccess(Consumer<S> consumer) {
|
||||
if (this.isSuccess()) {
|
||||
consumer.accept(this.getSuccessReason());
|
||||
default Result<S, F> onSuccess(Consumer<Success<F, S>> consumer) {
|
||||
if (this instanceof Success) {
|
||||
consumer.accept((Success<F, S>) this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default Result<S, F> onFailure(Consumer<F> consumer) {
|
||||
if (this.isFailure()) {
|
||||
consumer.accept(this.getFailureReason());
|
||||
default Result<S, F> onFailure(Consumer<Failure<S, F>> consumer) {
|
||||
if (this instanceof Failure) {
|
||||
consumer.accept((Failure<S, F>) this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@ -56,13 +65,41 @@ public sealed interface Result<S extends SuccessReason, F extends FailureReason>
|
||||
return this;
|
||||
}
|
||||
|
||||
default Result<S, F> onSuccessThen(Function<Success<F, S>, Result<S, F>> function) {
|
||||
if (this instanceof Success) {
|
||||
return function.apply((Success<F, S>) this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default Result<S, F> onFailureThen(Function<Failure<S, F>, Result<S, F>> function) {
|
||||
if (this instanceof Failure) {
|
||||
return function.apply((Failure<S, F>) this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default <R> R fold(Function<Failure<S, F>, R> failureFunc, Function<Success<F, S>, R> successFunc) {
|
||||
if (this instanceof Failure) {
|
||||
return failureFunc.apply((Failure<S, F>) this);
|
||||
} else if (this instanceof Success) {
|
||||
return successFunc.apply((Success<F, S>) this);
|
||||
}
|
||||
throw new IllegalStateException("Unknown result type: " + this.getClass().getName());
|
||||
}
|
||||
|
||||
final class Success<F extends FailureReason, S extends SuccessReason> implements Result<S, F> {
|
||||
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<S extends SuccessReason, F extends FailureReason>
|
||||
|
||||
@Override
|
||||
public @NotNull Message getReasonMessage() {
|
||||
return Message.of(successReason, "Success!", replacements);
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -100,11 +137,16 @@ public sealed interface Result<S extends SuccessReason, F extends FailureReason>
|
||||
|
||||
final class Failure<S extends SuccessReason, F extends FailureReason> implements Result<S, F> {
|
||||
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<S extends SuccessReason, F extends FailureReason>
|
||||
|
||||
@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<S extends SuccessReason, F extends FailureReason>
|
||||
|
||||
@Override
|
||||
public @NotNull Message getReasonMessage() {
|
||||
return Message.of(failureReason, "Success!", replacements);
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user