2021-06-27 20:44:35 +00:00
|
|
|
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<T> {
|
|
|
|
private boolean attemptedSetting;
|
|
|
|
private ResolvableFuture<Void> cancellationFuture = ResolvableFuture.create();
|
|
|
|
public SafeFuture<T> 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<Void> resolvableFuture = this.cancellationFuture;
|
|
|
|
if (resolvableFuture != null) {
|
|
|
|
resolvableFuture.addListener(runnable, executor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void finalize() {
|
|
|
|
ResolvableFuture<Void> resolvableFuture;
|
|
|
|
SafeFuture<T> safeFuture = this.future;
|
|
|
|
if (safeFuture != null && !safeFuture.isDone()) {
|
2021-11-05 06:48:17 +00:00
|
|
|
StringBuilder R = a.R("The completer object was garbage collected - this future would otherwise never complete. The tag was: ");
|
|
|
|
R.append(this.tag);
|
|
|
|
safeFuture.setException(new FutureGarbageCollectedException(R.toString()));
|
2021-06-27 20:44:35 +00:00
|
|
|
}
|
|
|
|
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<T> 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<T> 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<T> 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<T> {
|
|
|
|
@Nullable
|
|
|
|
Object attachCompleter(@NonNull Completer<T> completer) throws Exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final class SafeFuture<T> implements c.i.b.d.a.a<T> {
|
|
|
|
public final WeakReference<Completer<T>> completerWeakReference;
|
|
|
|
private final AbstractResolvableFuture<T> delegate = new AnonymousClass1();
|
|
|
|
|
|
|
|
/* renamed from: androidx.concurrent.futures.CallbackToFutureAdapter$SafeFuture$1 reason: invalid class name */
|
|
|
|
public class AnonymousClass1 extends AbstractResolvableFuture<T> {
|
|
|
|
public AnonymousClass1() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.concurrent.futures.AbstractResolvableFuture
|
|
|
|
public String pendingToString() {
|
|
|
|
Completer<T> completer = SafeFuture.this.completerWeakReference.get();
|
|
|
|
if (completer == null) {
|
|
|
|
return "Completer object has been garbage collected, future will fail soon";
|
|
|
|
}
|
2021-11-05 06:48:17 +00:00
|
|
|
StringBuilder R = a.R("tag=[");
|
|
|
|
R.append(completer.tag);
|
|
|
|
R.append("]");
|
|
|
|
return R.toString();
|
2021-06-27 20:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SafeFuture(Completer<T> 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<T> 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 <T> c.i.b.d.a.a<T> getFuture(@NonNull Resolver<T> resolver) {
|
|
|
|
Completer<T> 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;
|
|
|
|
}
|
|
|
|
}
|