it.auties.whatsapp.net.Response Maven / Gradle / Ivy
package it.auties.whatsapp.net;
import java.util.concurrent.CompletableFuture;
public sealed interface Response {
default boolean complete(V result) {
switch (this) {
case Callback callback -> callback.onResult(result, null);
case Future future -> future.complete(result);
}
return true;
}
@SuppressWarnings("UnusedReturnValue") // Would violate the return value of CompletableFuture
default boolean completeExceptionally(Throwable throwable) {
switch (this) {
case Callback callback -> callback.onResult(null, throwable);
case Future future -> future.completeExceptionally(throwable);
}
return true;
}
final class Future extends CompletableFuture implements Response {
}
@FunctionalInterface
non-sealed interface Callback extends Response {
void onResult(V result, Throwable error);
}
}