package androidx.concurrent.futures; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import c.d.b.a.a; import java.lang.ref.WeakReference; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public final class CallbackToFutureAdapter { public static final class Completer { private boolean attemptedSetting; private ResolvableFuture cancellationFuture = ResolvableFuture.create(); public SafeFuture future; public Object tag; private void setCompletedNormally() { this.tag = null; this.future = null; this.cancellationFuture = null; } public void addCancellationListener(@NonNull Runnable runnable, @NonNull Executor executor) { ResolvableFuture resolvableFuture = this.cancellationFuture; if (resolvableFuture != null) { resolvableFuture.addListener(runnable, executor); } } public void finalize() { ResolvableFuture resolvableFuture; SafeFuture safeFuture = this.future; if (safeFuture != null && !safeFuture.isDone()) { StringBuilder P = a.P("The completer object was garbage collected - this future would otherwise never complete. The tag was: "); P.append(this.tag); safeFuture.setException(new FutureGarbageCollectedException(P.toString())); } if (!this.attemptedSetting && (resolvableFuture = this.cancellationFuture) != null) { resolvableFuture.set(null); } } public void fireCancellationListeners() { this.tag = null; this.future = null; this.cancellationFuture.set(null); } public boolean set(T t) { boolean z2 = true; this.attemptedSetting = true; SafeFuture safeFuture = this.future; if (safeFuture == null || !safeFuture.set(t)) { z2 = false; } if (z2) { setCompletedNormally(); } return z2; } public boolean setCancelled() { boolean z2 = true; this.attemptedSetting = true; SafeFuture safeFuture = this.future; if (safeFuture == null || !safeFuture.cancelWithoutNotifyingCompleter(true)) { z2 = false; } if (z2) { setCompletedNormally(); } return z2; } public boolean setException(@NonNull Throwable th) { boolean z2 = true; this.attemptedSetting = true; SafeFuture safeFuture = this.future; if (safeFuture == null || !safeFuture.setException(th)) { z2 = false; } if (z2) { setCompletedNormally(); } return z2; } } public static final class FutureGarbageCollectedException extends Throwable { public FutureGarbageCollectedException(String str) { super(str); } @Override // java.lang.Throwable public synchronized Throwable fillInStackTrace() { return this; } } public interface Resolver { @Nullable Object attachCompleter(@NonNull Completer completer) throws Exception; } public static final class SafeFuture implements c.i.b.d.a.a { public final WeakReference> completerWeakReference; private final AbstractResolvableFuture delegate = new AnonymousClass1(); /* renamed from: androidx.concurrent.futures.CallbackToFutureAdapter$SafeFuture$1 reason: invalid class name */ public class AnonymousClass1 extends AbstractResolvableFuture { public AnonymousClass1() { } @Override // androidx.concurrent.futures.AbstractResolvableFuture public String pendingToString() { Completer completer = SafeFuture.this.completerWeakReference.get(); if (completer == null) { return "Completer object has been garbage collected, future will fail soon"; } StringBuilder P = a.P("tag=["); P.append(completer.tag); P.append("]"); return P.toString(); } } public SafeFuture(Completer completer) { this.completerWeakReference = new WeakReference<>(completer); } @Override // c.i.b.d.a.a public void addListener(@NonNull Runnable runnable, @NonNull Executor executor) { this.delegate.addListener(runnable, executor); } @Override // java.util.concurrent.Future public boolean cancel(boolean z2) { Completer completer = this.completerWeakReference.get(); boolean cancel = this.delegate.cancel(z2); if (cancel && completer != null) { completer.fireCancellationListeners(); } return cancel; } public boolean cancelWithoutNotifyingCompleter(boolean z2) { return this.delegate.cancel(z2); } @Override // java.util.concurrent.Future public T get() throws InterruptedException, ExecutionException { return this.delegate.get(); } @Override // java.util.concurrent.Future public T get(long j, @NonNull TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { return this.delegate.get(j, timeUnit); } @Override // java.util.concurrent.Future public boolean isCancelled() { return this.delegate.isCancelled(); } @Override // java.util.concurrent.Future public boolean isDone() { return this.delegate.isDone(); } public boolean set(T t) { return this.delegate.set(t); } public boolean setException(Throwable th) { return this.delegate.setException(th); } @Override // java.lang.Object public String toString() { return this.delegate.toString(); } } private CallbackToFutureAdapter() { } @NonNull public static c.i.b.d.a.a getFuture(@NonNull Resolver resolver) { Completer completer = new Completer<>(); SafeFuture safeFuture = new SafeFuture<>(completer); completer.future = safeFuture; completer.tag = resolver.getClass(); try { Object attachCompleter = resolver.attachCompleter(completer); if (attachCompleter != null) { completer.tag = attachCompleter; } } catch (Exception e) { safeFuture.setException(e); } return safeFuture; } }