what-the-bank/sources/com/google/common/util/concurrent/TimeoutFuture.java

139 lines
5.7 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.util.concurrent;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FluentFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class TimeoutFuture<V> extends FluentFuture.TrustedFuture<V> {
private ListenableFuture<V> delegateRef;
private ScheduledFuture<?> timer;
/* JADX INFO: Access modifiers changed from: package-private */
public static <V> ListenableFuture<V> create(ListenableFuture<V> listenableFuture, long j, TimeUnit timeUnit, ScheduledExecutorService scheduledExecutorService) {
TimeoutFuture timeoutFuture = new TimeoutFuture(listenableFuture);
Fire fire = new Fire(timeoutFuture);
timeoutFuture.timer = scheduledExecutorService.schedule(fire, j, timeUnit);
listenableFuture.addListener(fire, MoreExecutors.directExecutor());
return timeoutFuture;
}
private TimeoutFuture(ListenableFuture<V> listenableFuture) {
this.delegateRef = (ListenableFuture) Preconditions.checkNotNull(listenableFuture);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class Fire<V> implements Runnable {
TimeoutFuture<V> timeoutFutureRef;
Fire(TimeoutFuture<V> timeoutFuture) {
this.timeoutFutureRef = timeoutFuture;
}
@Override // java.lang.Runnable
public final void run() {
ListenableFuture<? extends V> listenableFuture;
TimeoutFuture<V> timeoutFuture = this.timeoutFutureRef;
if (timeoutFuture == null || (listenableFuture = ((TimeoutFuture) timeoutFuture).delegateRef) == null) {
return;
}
this.timeoutFutureRef = null;
if (!listenableFuture.isDone()) {
try {
ScheduledFuture scheduledFuture = ((TimeoutFuture) timeoutFuture).timer;
((TimeoutFuture) timeoutFuture).timer = null;
String str = "Timed out";
if (scheduledFuture != null) {
try {
long abs = Math.abs(scheduledFuture.getDelay(TimeUnit.MILLISECONDS));
if (abs > 10) {
StringBuilder sb = new StringBuilder(75);
sb.append("Timed out (timeout delayed by ");
sb.append(abs);
sb.append(" ms after scheduled time)");
str = sb.toString();
}
} catch (Throwable th) {
timeoutFuture.setException(new TimeoutFutureException(str));
throw th;
}
}
String valueOf = String.valueOf(str);
String valueOf2 = String.valueOf(listenableFuture);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf).length() + 2 + String.valueOf(valueOf2).length());
sb2.append(valueOf);
sb2.append(": ");
sb2.append(valueOf2);
timeoutFuture.setException(new TimeoutFutureException(sb2.toString()));
return;
} finally {
listenableFuture.cancel(true);
}
}
timeoutFuture.setFuture(listenableFuture);
}
}
/* loaded from: classes2.dex */
static final class TimeoutFutureException extends TimeoutException {
private TimeoutFutureException(String str) {
super(str);
}
@Override // java.lang.Throwable
public final Throwable fillInStackTrace() {
synchronized (this) {
setStackTrace(new StackTraceElement[0]);
}
return this;
}
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.util.concurrent.AbstractFuture
public final String pendingToString() {
ListenableFuture<V> listenableFuture = this.delegateRef;
ScheduledFuture<?> scheduledFuture = this.timer;
if (listenableFuture == null) {
return null;
}
String valueOf = String.valueOf(listenableFuture);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 14);
sb.append("inputFuture=[");
sb.append(valueOf);
sb.append("]");
String obj = sb.toString();
if (scheduledFuture == null) {
return obj;
}
long delay = scheduledFuture.getDelay(TimeUnit.MILLISECONDS);
if (delay <= 0) {
return obj;
}
String valueOf2 = String.valueOf(obj);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 43);
sb2.append(valueOf2);
sb2.append(", remaining delay=[");
sb2.append(delay);
sb2.append(" ms]");
return sb2.toString();
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.util.concurrent.AbstractFuture
public final void afterDone() {
maybePropagateCancellationTo(this.delegateRef);
ScheduledFuture<?> scheduledFuture = this.timer;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
this.delegateRef = null;
this.timer = null;
}
}