Merge pull request #3099 from Multiverse/ben/mv5/AsyncAttempt

Add onFailure methods to AsyncAttempt class
This commit is contained in:
Ben Woo 2024-08-29 11:00:28 +08:00 committed by GitHub
commit fe79580e86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@ package org.mvplugins.multiverse.core.utils.result;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import org.mvplugins.multiverse.core.utils.message.MessageReplacement;
@ -67,6 +68,19 @@ public final class AsyncAttempt<T, F extends FailureReason> {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.onSuccess(runnable)));
}
public AsyncAttempt<T, F> onFailure(Runnable runnable) {
// TODO Not sure why we creating a new instance instead of using `this`
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.onFailure(runnable)));
}
public AsyncAttempt<T, F> onFailure(Consumer<Attempt.Failure<T, F>> consumer) {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.onFailure(consumer)));
}
public AsyncAttempt<T, F> onFailureReason(Consumer<F> consumer) {
return new AsyncAttempt<>(future.thenApply(attempt -> attempt.onFailureReason(consumer)));
}
public Attempt<T, F> toAttempt() {
return future.join();
}