diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/result/Attempt.java b/src/main/java/org/mvplugins/multiverse/core/utils/result/Attempt.java index 3e5783cab..553a0c6b6 100644 --- a/src/main/java/org/mvplugins/multiverse/core/utils/result/Attempt.java +++ b/src/main/java/org/mvplugins/multiverse/core/utils/result/Attempt.java @@ -107,18 +107,14 @@ static Attempt failure(F failureReason, Messa * * @return Whether this attempt is a success. */ - default boolean isSuccess() { - return this instanceof Success; - } + boolean isSuccess(); /** * Returns whether this attempt is a failure. * * @return Whether this attempt is a failure. */ - default boolean isFailure() { - return this instanceof Failure; - } + boolean isFailure(); /** * Converts this {@link Attempt} instance to an equivalent {@link Try} representation. Defaults to a @@ -143,19 +139,25 @@ default boolean isFailure() { @ApiStatus.AvailableSince("5.1") Try toTry(Function, Throwable> throwableFunction); + /** + * Runs the given runnable after this attempt regardless of success or failure. + * + * @param runnable The runnable. + * @return This attempt. + */ default Attempt thenRun(Runnable runnable) { runnable.run(); return this; } - default Attempt thenAccept(Consumer> consumer) { - if (this instanceof Success) { - consumer.accept(Either.left(get())); - } else { - consumer.accept(Either.right(getFailureReason())); - } - return this; - } + /** + * Accepts either the value or the failure reason depending on the result type. + * This will run regardless of success or failure. + * + * @param consumer The consumer with either the value or the failure reason. + * @return This attempt. + */ + Attempt thenAccept(Consumer> consumer); /** * Peeks at the value if this is a success attempt. @@ -163,12 +165,7 @@ default Attempt thenAccept(Consumer> consumer) { * @param consumer The consumer with the value. * @return This attempt. */ - default Attempt peek(Consumer consumer) { - if (this instanceof Success) { - consumer.accept(get()); - } - return this; - } + Attempt peek(Consumer consumer); /** * Maps the value to another value if this is a success attempt. @@ -177,13 +174,7 @@ default Attempt peek(Consumer consumer) { * @param The type of the new value. * @return The new attempt. */ - default Attempt map(Function mapper) { - if (this instanceof Success) { - return new Success<>(mapper.apply(get())); - } else { - return new Failure<>((Failure) this); - } - } + Attempt map(Function mapper); /** * Maps the value to another attempt if this is a success attempt. @@ -192,13 +183,7 @@ default Attempt map(Function mapper) { * @param The type of the new value. * @return The new attempt. */ - default Attempt map(Supplier mapper) { - if (this instanceof Success) { - return new Success<>(mapper.get()); - } else { - return new Failure<>((Failure) this); - } - } + Attempt map(Supplier mapper); /** * Maps the value to another attempt with the same fail reason if this is a success attempt. @@ -207,13 +192,7 @@ default Attempt map(Supplier mapper) { * @param The type of the new value. * @return The new attempt. */ - default Attempt mapAttempt(Function> mapper) { - if (this instanceof Success) { - return mapper.apply(get()); - } else { - return new Failure<>((Failure) this); - } - } + Attempt mapAttempt(Function> mapper); /** * Maps the value to another attempt with the same fail reason if this is a success attempt. @@ -222,13 +201,7 @@ default Attempt mapAttempt(Function> mapper) * @param The type of the new value. * @return The new attempt. */ - default Attempt mapAttempt(Supplier> mapper) { - if (this instanceof Success) { - return mapper.get(); - } else { - return new Failure<>((Failure) this); - } - } + Attempt mapAttempt(Supplier> mapper); /** * Maps to another attempt with a different fail reason. @@ -237,13 +210,7 @@ default Attempt mapAttempt(Supplier> mapper) { * @param The type of the new fail reason. * @return The new attempt. */ - default Attempt transform(UF failureReason) { - if (this instanceof Success) { - return new Success<>(get()); - } else { - return new Failure<>(failureReason, getFailureMessage(), (Failure) this); - } - } + Attempt transform(UF failureReason); /** * Maps attempt result to another value. @@ -256,13 +223,7 @@ default Attempt transform(UF failureReason) { * @since 5.1 */ @ApiStatus.AvailableSince("5.1") - default U transform(Function successMapper, Function failureMapper) { - if (this instanceof Success) { - return successMapper.apply(get()); - } else { - return failureMapper.apply(getFailureReason()); - } - } + U transform(Function successMapper, Function failureMapper); /** * Calls either the failure or success function depending on the result type. @@ -272,13 +233,7 @@ default U transform(Function successMapper, Function failureMapp * @param The type of the new value. * @return The result of the function. */ - default N fold(Function, N> failureMapper, Function successMapper) { - if (this instanceof Success) { - return successMapper.apply(get()); - } else { - return failureMapper.apply((Failure) this); - } - } + N fold(Function, N> failureMapper, Function successMapper); /** * Calls the given runnable if this is a success attempt. @@ -286,12 +241,7 @@ default N fold(Function, N> failureMapper, Function succ * @param runnable The runnable. * @return This attempt. */ - default Attempt onSuccess(Runnable runnable) { - if (this instanceof Success) { - runnable.run(); - } - return this; - } + Attempt onSuccess(Runnable runnable); /** * Calls the given consumer if this is a success attempt. @@ -299,12 +249,7 @@ default Attempt onSuccess(Runnable runnable) { * @param consumer The consumer with the value. * @return This attempt. */ - default Attempt onSuccess(Consumer consumer) { - if (this instanceof Success) { - consumer.accept(get()); - } - return this; - } + Attempt onSuccess(Consumer consumer); /** * Calls the given consumer if this is a failure attempt. @@ -312,12 +257,7 @@ default Attempt onSuccess(Consumer consumer) { * @param runnable The runnable. * @return This attempt. */ - default Attempt onFailure(Runnable runnable) { - if (this instanceof Failure) { - runnable.run(); - } - return this; - } + Attempt onFailure(Runnable runnable); /** * Calls the given consumer if this is a failure attempt. @@ -325,12 +265,7 @@ default Attempt onFailure(Runnable runnable) { * @param consumer The consumer with the failure instance. * @return This attempt. */ - default Attempt onFailure(Consumer> consumer) { - if (this instanceof Failure) { - consumer.accept((Failure) this); - } - return this; - } + Attempt onFailure(Consumer> consumer); /** * Calls the given runnable if this is a failure attempt. @@ -338,12 +273,7 @@ default Attempt onFailure(Consumer> consumer) { * @param consumer The consumer with the failure reason. * @return This attempt. */ - default Attempt onFailureReason(Consumer consumer) { - if (this instanceof Failure) { - consumer.accept(getFailureReason()); - } - return this; - } + Attempt onFailureReason(Consumer consumer); /** * Represents a successful attempt with a value. @@ -378,6 +308,26 @@ public T getOrThrow(Function, X> exceptionSu return value; } + @Override + public F getFailureReason() { + throw new UnsupportedOperationException("No failure reason as attempt is a success"); + } + + @Override + public Message getFailureMessage() { + throw new UnsupportedOperationException("No failure message as attempt is a success"); + } + + @Override + public boolean isSuccess() { + return true; + } + + @Override + public boolean isFailure() { + return false; + } + @Override public Try toTry() { return Try.success(value); @@ -389,13 +339,83 @@ public Try toTry(Function, Throwable> throwableFunction) { } @Override - public F getFailureReason() { - throw new UnsupportedOperationException("No failure reason as attempt is a success"); + public Attempt thenAccept(Consumer> consumer) { + consumer.accept(Either.left(value)); + return this; } @Override - public Message getFailureMessage() { - throw new UnsupportedOperationException("No failure message as attempt is a success"); + public Attempt peek(Consumer consumer) { + consumer.accept(value); + return this; + } + + @Override + public Attempt map(Function mapper) { + return new Success<>(mapper.apply(value)); + } + + @Override + public Attempt map(Supplier mapper) { + return new Success<>(mapper.get()); + } + + @Override + public Attempt mapAttempt(Function> mapper) { + return mapper.apply(value); + } + + @Override + public Attempt mapAttempt(Supplier> mapper) { + return mapper.get(); + } + + @Override + public Attempt transform(UF failureReason) { + return changeFailureType(); + } + + @Override + public U transform(Function successMapper, Function failureMapper) { + return successMapper.apply(value); + } + + @Override + public N fold(Function, N> failureMapper, Function successMapper) { + return successMapper.apply(value); + } + + @Override + public Attempt onSuccess(Runnable runnable) { + runnable.run(); + return this; + } + + @Override + public Attempt onSuccess(Consumer consumer) { + consumer.accept(value); + return this; + } + + @Override + public Attempt onFailure(Runnable runnable) { + return this; + } + + @Override + public Attempt onFailure(Consumer> consumer) { + return this; + } + + @Override + public Attempt onFailureReason(Consumer consumer) { + return this; + } + + private Attempt changeFailureType() { + @SuppressWarnings("unchecked") + Attempt mappedSuccess = (Attempt) this; + return mappedSuccess; } @Override @@ -451,6 +471,26 @@ public T getOrThrow(Function, X> exceptionSu throw exceptionSupplier.apply(this); } + @Override + public F getFailureReason() { + return failureReason; + } + + @Override + public Message getFailureMessage() { + return message; + } + + @Override + public boolean isSuccess() { + return false; + } + + @Override + public boolean isFailure() { + return true; + } + @Override public Try toTry() { return Try.failure(new MultiverseException(message)); @@ -462,13 +502,83 @@ public Try toTry(Function, Throwable> throwableFunction) { } @Override - public F getFailureReason() { - return failureReason; + public Attempt thenAccept(Consumer> consumer) { + consumer.accept(Either.right(failureReason)); + return this; } @Override - public Message getFailureMessage() { - return message; + public Attempt peek(Consumer consumer) { + return this; + } + + @Override + public Attempt map(Function mapper) { + return changeValueType(); + } + + @Override + public Attempt map(Supplier mapper) { + return changeValueType(); + } + + @Override + public Attempt mapAttempt(Function> mapper) { + return changeValueType(); + } + + @Override + public Attempt mapAttempt(Supplier> mapper) { + return changeValueType(); + } + + @Override + public Attempt transform(UF failureReason) { + return new Failure<>(failureReason, getFailureMessage(), this); + } + + @Override + public U transform(Function successMapper, Function failureMapper) { + return failureMapper.apply(failureReason); + } + + @Override + public N fold(Function, N> failureMapper, Function successMapper) { + return failureMapper.apply(this); + } + + @Override + public Attempt onSuccess(Runnable runnable) { + return this; + } + + @Override + public Attempt onSuccess(Consumer consumer) { + return this; + } + + @Override + public Attempt onFailure(Runnable runnable) { + runnable.run(); + return this; + } + + @Override + public Attempt onFailure(Consumer> consumer) { + consumer.accept(this); + return this; + } + + @Override + public Attempt onFailureReason(Consumer consumer) { + consumer.accept(failureReason); + return this; + } + + private Attempt changeValueType() { + @SuppressWarnings("unchecked") + Attempt mappedFailure = (Attempt) this; + return mappedFailure; } @Override