package retrofit2; import java.lang.annotation.Annotation; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.concurrent.CompletableFuture; import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; import retrofit2.CallAdapter; /* JADX INFO: Access modifiers changed from: package-private */ @IgnoreJRERequirement /* loaded from: classes3.dex */ public final class CompletableFutureCallAdapterFactory extends CallAdapter.Factory { static final CallAdapter.Factory INSTANCE = new CompletableFutureCallAdapterFactory(); @IgnoreJRERequirement /* loaded from: classes3.dex */ public static final class BodyCallAdapter implements CallAdapter> { private final Type responseType; @IgnoreJRERequirement /* loaded from: classes3.dex */ public class BodyCallback implements Callback { private final CompletableFuture future; public BodyCallback(CompletableFuture completableFuture) { this.future = completableFuture; } @Override // retrofit2.Callback public void onFailure(Call call, Throwable th) { this.future.completeExceptionally(th); } @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)); } } } public BodyCallAdapter(Type type) { this.responseType = type; } @Override // retrofit2.CallAdapter public Type responseType() { return this.responseType; } @Override // retrofit2.CallAdapter public CompletableFuture adapt(Call call) { CallCancelCompletableFuture callCancelCompletableFuture = new CallCancelCompletableFuture(call); call.enqueue(new BodyCallback(callCancelCompletableFuture)); return callCancelCompletableFuture; } } @IgnoreJRERequirement /* loaded from: classes3.dex */ public static final class CallCancelCompletableFuture extends CompletableFuture { private final Call call; public CallCancelCompletableFuture(Call call) { this.call = call; } @Override // java.util.concurrent.CompletableFuture, java.util.concurrent.Future public boolean cancel(boolean z3) { if (z3) { this.call.cancel(); } return super.cancel(z3); } } @IgnoreJRERequirement /* loaded from: classes3.dex */ public static final class ResponseCallAdapter implements CallAdapter>> { private final Type responseType; @IgnoreJRERequirement /* loaded from: classes3.dex */ public class ResponseCallback implements Callback { private final CompletableFuture> future; public ResponseCallback(CompletableFuture> completableFuture) { this.future = completableFuture; } @Override // retrofit2.Callback public void onFailure(Call call, Throwable th) { this.future.completeExceptionally(th); } @Override // retrofit2.Callback public void onResponse(Call call, Response response) { this.future.complete(response); } } public ResponseCallAdapter(Type type) { this.responseType = type; } @Override // retrofit2.CallAdapter public Type responseType() { return this.responseType; } @Override // retrofit2.CallAdapter public CompletableFuture> adapt(Call call) { CallCancelCompletableFuture callCancelCompletableFuture = new CallCancelCompletableFuture(call); call.enqueue(new ResponseCallback(callCancelCompletableFuture)); return callCancelCompletableFuture; } } @Override // retrofit2.CallAdapter.Factory public CallAdapter get(Type type, Annotation[] annotationArr, Retrofit retrofit) { if (CallAdapter.Factory.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 = CallAdapter.Factory.getParameterUpperBound(0, (ParameterizedType) type); if (CallAdapter.Factory.getRawType(parameterUpperBound) != Response.class) { return new BodyCallAdapter(parameterUpperBound); } if (parameterUpperBound instanceof ParameterizedType) { return new ResponseCallAdapter(CallAdapter.Factory.getParameterUpperBound(0, (ParameterizedType) parameterUpperBound)); } throw new IllegalStateException("Response must be parameterized as Response or Response"); } }