content: package androidx.concurrent.futures;
  
  import com.google.common.util.concurrent.ListenableFuture;
  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<T> {
          private boolean attemptedSetting;
          private ResolvableFuture<Void> cancellationFuture = ResolvableFuture.create();
          SafeFuture<T> future;
          Object tag;
  
          Completer() {
          }
  
          private void setCompletedNormally() {
              this.tag = null;
              this.future = null;
              this.cancellationFuture = null;
          }
  
          public void addCancellationListener(Runnable runnable, Executor executor) {
              ResolvableFuture<Void> resolvableFuture = this.cancellationFuture;
              if (resolvableFuture != null) {
                  resolvableFuture.addListener(runnable, executor);
              }
          }
  
          /* access modifiers changed from: protected */
          public void finalize() {
              ResolvableFuture<Void> resolvableFuture;
              SafeFuture<T> safeFuture = this.future;
              if (safeFuture != null && !safeFuture.isDone()) {
                  safeFuture.setException(new FutureGarbageCollectedException("The completer object was garbage collected - this future would otherwise never complete. The tag was: " + this.tag));
              }
              if (!this.attemptedSetting && (resolvableFuture = this.cancellationFuture) != null) {
                  resolvableFuture.set(null);
              }
          }
  
          /* access modifiers changed from: package-private */
          public void fireCancellationListeners() {
              this.tag = null;
              this.future = null;
              this.cancellationFuture.set(null);
          }
  
          public boolean set(T t) {
              boolean z = true;
              this.attemptedSetting = true;
              SafeFuture<T> safeFuture = this.future;
              if (safeFuture == null || !safeFuture.set(t)) {
                  z = false;
              }
              if (z) {
                  setCompletedNormally();
              }
              return z;
          }
  
          public boolean setCancelled() {
              boolean z = true;
              this.attemptedSetting = true;
              SafeFuture<T> safeFuture = this.future;
              if (safeFuture == null || !safeFuture.cancelWithoutNotifyingCompleter(true)) {
                  z = false;
              }
              if (z) {
                  setCompletedNormally();
              }
              return z;
          }
  
          public boolean setException(Throwable t) {
              boolean z = true;
              this.attemptedSetting = true;
              SafeFuture<T> safeFuture = this.future;
              if (safeFuture == null || !safeFuture.setException(t)) {
                  z = false;
              }
              if (z) {
                  setCompletedNormally();
              }
              return z;
          }
      }
  
      static final class FutureGarbageCollectedException extends Throwable {
          FutureGarbageCollectedException(String message) {
              super(message);
          }
  
          public synchronized Throwable fillInStackTrace() {
              return this;
          }
      }
  
      public interface Resolver<T> {
          Object attachCompleter(Completer<T> completer) throws Exception;
      }
  
      private static final class SafeFuture<T> implements ListenableFuture<T> {
          final WeakReference<Completer<T>> completerWeakReference;
          private final AbstractResolvableFuture<T> delegate = new AbstractResolvableFuture<T>() {
              /* access modifiers changed from: protected */
              public String pendingToString() {
                  Completer completer = (Completer) SafeFuture.this.completerWeakReference.get();
                  return completer == null ? "Completer object has been garbage collected, future will fail soon" : "tag=[" + completer.tag + "]";
              }
          };
  
          SafeFuture(Completer<T> completer) {
              this.completerWeakReference = new WeakReference<>(completer);
          }
  
          public void addListener(Runnable listener, Executor executor) {
              this.delegate.addListener(listener, executor);
          }
  
          public boolean cancel(boolean mayInterruptIfRunning) {
              Completer completer = (Completer) this.completerWeakReference.get();
              boolean cancel = this.delegate.cancel(mayInterruptIfRunning);
              if (cancel && completer != null) {
                  completer.fireCancellationListeners();
              }
              return cancel;
          }
  
          /* access modifiers changed from: package-private */
          public boolean cancelWithoutNotifyingCompleter(boolean shouldInterrupt) {
              return this.delegate.cancel(shouldInterrupt);
          }
  
          public T get() throws InterruptedException, ExecutionException {
              return this.delegate.get();
          }
  
          public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
              return this.delegate.get(timeout, unit);
          }
  
          public boolean isCancelled() {
              return this.delegate.isCancelled();
          }
  
          public boolean isDone() {
              return this.delegate.isDone();
          }
  
          /* access modifiers changed from: package-private */
          public boolean set(T t) {
              return this.delegate.set(t);
          }
  
          /* access modifiers changed from: package-private */
          public boolean setException(Throwable t) {
              return this.delegate.setException(t);
          }
  
          public String toString() {
              return this.delegate.toString();
          }
      }
  
      private CallbackToFutureAdapter() {
      }
  
      public static <T> ListenableFuture<T> getFuture(Resolver<T> resolver) {
          Completer completer = new Completer();
          SafeFuture<T> 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;
      }
  }
  