what-the-bank/sources/retrofit2/CompletableFutureCallAdapte...

143 lines
5.5 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package retrofit2;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.concurrent.CompletableFuture;
import retrofit2.CallAdapter;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class CompletableFutureCallAdapterFactory extends CallAdapter.Factory {
static final CallAdapter.Factory INSTANCE = new CompletableFutureCallAdapterFactory();
CompletableFutureCallAdapterFactory() {
}
@Override // retrofit2.CallAdapter.Factory
public final CallAdapter<?, ?> get(Type type, Annotation[] annotationArr, Retrofit retrofit) {
if (getRawType(type) != CompletableFuture.class) {
return null;
}
if (!(type instanceof ParameterizedType)) {
throw new IllegalStateException("CompletableFuture return type must be parameterized as CompletableFuture<Foo> or CompletableFuture<? extends Foo>");
}
Type parameterUpperBound = getParameterUpperBound(0, (ParameterizedType) type);
if (getRawType(parameterUpperBound) != Response.class) {
return new BodyCallAdapter(parameterUpperBound);
}
if (!(parameterUpperBound instanceof ParameterizedType)) {
throw new IllegalStateException("Response must be parameterized as Response<Foo> or Response<? extends Foo>");
}
return new ResponseCallAdapter(getParameterUpperBound(0, (ParameterizedType) parameterUpperBound));
}
/* loaded from: classes.dex */
static final class BodyCallAdapter<R> implements CallAdapter<R, CompletableFuture<R>> {
private final Type responseType;
BodyCallAdapter(Type type) {
this.responseType = type;
}
@Override // retrofit2.CallAdapter
public final CompletableFuture<R> adapt(Call<R> call) {
CallCancelCompletableFuture callCancelCompletableFuture = new CallCancelCompletableFuture(call);
call.enqueue(new BodyCallback(this, callCancelCompletableFuture));
return callCancelCompletableFuture;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class BodyCallback implements Callback<R> {
private final CompletableFuture<R> future;
final BodyCallAdapter this$0;
public BodyCallback(BodyCallAdapter bodyCallAdapter, CompletableFuture<R> completableFuture) {
this.this$0 = bodyCallAdapter;
this.future = completableFuture;
}
@Override // retrofit2.Callback
public void onResponse(Call<R> call, Response<R> response) {
if (response.isSuccessful()) {
this.future.complete(response.body());
} else {
this.future.completeExceptionally(new HttpException(response));
}
}
@Override // retrofit2.Callback
public void onFailure(Call<R> call, Throwable th) {
this.future.completeExceptionally(th);
}
}
@Override // retrofit2.CallAdapter
public final Type responseType() {
return this.responseType;
}
}
/* loaded from: classes.dex */
static final class ResponseCallAdapter<R> implements CallAdapter<R, CompletableFuture<Response<R>>> {
private final Type responseType;
ResponseCallAdapter(Type type) {
this.responseType = type;
}
@Override // retrofit2.CallAdapter
public final CompletableFuture<Response<R>> adapt(Call<R> call) {
CallCancelCompletableFuture callCancelCompletableFuture = new CallCancelCompletableFuture(call);
call.enqueue(new ResponseCallback(this, callCancelCompletableFuture));
return callCancelCompletableFuture;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class ResponseCallback implements Callback<R> {
private final CompletableFuture<Response<R>> future;
final ResponseCallAdapter this$0;
public ResponseCallback(ResponseCallAdapter responseCallAdapter, CompletableFuture<Response<R>> completableFuture) {
this.this$0 = responseCallAdapter;
this.future = completableFuture;
}
@Override // retrofit2.Callback
public void onResponse(Call<R> call, Response<R> response) {
this.future.complete(response);
}
@Override // retrofit2.Callback
public void onFailure(Call<R> call, Throwable th) {
this.future.completeExceptionally(th);
}
}
@Override // retrofit2.CallAdapter
public final Type responseType() {
return this.responseType;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public static final class CallCancelCompletableFuture<T> extends CompletableFuture<T> {
private final Call<?> call;
CallCancelCompletableFuture(Call<?> call) {
this.call = call;
}
@Override // java.util.concurrent.CompletableFuture, java.util.concurrent.Future
public final boolean cancel(boolean z) {
if (z) {
this.call.cancel();
}
return super.cancel(z);
}
}
}