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 or CompletableFuture"); } 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 or Response"); } return new ResponseCallAdapter(getParameterUpperBound(0, (ParameterizedType) parameterUpperBound)); } /* loaded from: classes.dex */ static final class BodyCallAdapter implements CallAdapter> { private final Type responseType; BodyCallAdapter(Type type) { this.responseType = type; } @Override // retrofit2.CallAdapter public final CompletableFuture adapt(Call 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 { private final CompletableFuture future; final BodyCallAdapter this$0; public BodyCallback(BodyCallAdapter bodyCallAdapter, CompletableFuture completableFuture) { this.this$0 = bodyCallAdapter; this.future = completableFuture; } @Override // retrofit2.Callback public void onResponse(Call call, Response response) { if (response.isSuccessful()) { this.future.complete(response.body()); } else { this.future.completeExceptionally(new HttpException(response)); } } @Override // retrofit2.Callback public void onFailure(Call 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 implements CallAdapter>> { private final Type responseType; ResponseCallAdapter(Type type) { this.responseType = type; } @Override // retrofit2.CallAdapter public final CompletableFuture> adapt(Call 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 { private final CompletableFuture> future; final ResponseCallAdapter this$0; public ResponseCallback(ResponseCallAdapter responseCallAdapter, CompletableFuture> completableFuture) { this.this$0 = responseCallAdapter; this.future = completableFuture; } @Override // retrofit2.Callback public void onResponse(Call call, Response response) { this.future.complete(response); } @Override // retrofit2.Callback public void onFailure(Call 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 extends CompletableFuture { 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); } } }