package com.google.common.util.concurrent; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.AbstractFuture; import com.google.common.util.concurrent.CollectionFuture; import com.google.common.util.concurrent.ImmediateFuture; import com.google.common.util.concurrent.internal.InternalFutureFailureAccess; import com.google.common.util.concurrent.internal.InternalFutures; import java.util.Collection; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; /* loaded from: classes2.dex */ public final class Futures extends GwtFuturesCatchingSpecialization { private Futures() { } public static ListenableFuture immediateFuture(V v) { if (v == null) { return (ListenableFuture) ImmediateFuture.NULL; } return new ImmediateFuture(v); } public static ListenableFuture immediateVoidFuture() { return ImmediateFuture.NULL; } public static ListenableFuture immediateFailedFuture(Throwable th) { Preconditions.checkNotNull(th); return new ImmediateFuture.ImmediateFailedFuture(th); } public static ListenableFuture immediateCancelledFuture() { return new ImmediateFuture.ImmediateCancelledFuture(); } public static ListenableFuture submit(Callable callable, Executor executor) { TrustedListenableFutureTask create = TrustedListenableFutureTask.create(callable); executor.execute(create); return create; } public static ListenableFuture submit(Runnable runnable, Executor executor) { TrustedListenableFutureTask create = TrustedListenableFutureTask.create(runnable, null); executor.execute(create); return create; } public static ListenableFuture submitAsync(AsyncCallable asyncCallable, Executor executor) { TrustedListenableFutureTask create = TrustedListenableFutureTask.create(asyncCallable); executor.execute(create); return create; } public static ListenableFuture scheduleAsync(AsyncCallable asyncCallable, long j, TimeUnit timeUnit, ScheduledExecutorService scheduledExecutorService) { TrustedListenableFutureTask create = TrustedListenableFutureTask.create(asyncCallable); create.addListener(new Runnable(scheduledExecutorService.schedule(create, j, timeUnit)) { // from class: com.google.common.util.concurrent.Futures.1 final Future val$scheduled; { this.val$scheduled = r1; } @Override // java.lang.Runnable public void run() { this.val$scheduled.cancel(false); } }, MoreExecutors.directExecutor()); return create; } public static ListenableFuture catching(ListenableFuture listenableFuture, Class cls, Function function, Executor executor) { return AbstractCatchingFuture.create(listenableFuture, cls, function, executor); } public static ListenableFuture catchingAsync(ListenableFuture listenableFuture, Class cls, AsyncFunction asyncFunction, Executor executor) { return AbstractCatchingFuture.create(listenableFuture, cls, asyncFunction, executor); } public static ListenableFuture withTimeout(ListenableFuture listenableFuture, long j, TimeUnit timeUnit, ScheduledExecutorService scheduledExecutorService) { return listenableFuture.isDone() ? listenableFuture : TimeoutFuture.create(listenableFuture, j, timeUnit, scheduledExecutorService); } public static ListenableFuture transformAsync(ListenableFuture listenableFuture, AsyncFunction asyncFunction, Executor executor) { return AbstractTransformFuture.create(listenableFuture, asyncFunction, executor); } public static ListenableFuture transform(ListenableFuture listenableFuture, Function function, Executor executor) { return AbstractTransformFuture.create(listenableFuture, function, executor); } public static Future lazyTransform(Future future, Function function) { Preconditions.checkNotNull(future); Preconditions.checkNotNull(function); return new Future(future, function) { // from class: com.google.common.util.concurrent.Futures.2 final Function val$function; final Future val$input; { this.val$input = future; this.val$function = function; } @Override // java.util.concurrent.Future public boolean cancel(boolean z) { return this.val$input.cancel(z); } @Override // java.util.concurrent.Future public boolean isCancelled() { return this.val$input.isCancelled(); } @Override // java.util.concurrent.Future public boolean isDone() { return this.val$input.isDone(); } @Override // java.util.concurrent.Future public O get() throws InterruptedException, ExecutionException { return applyTransformation(this.val$input.get()); } @Override // java.util.concurrent.Future public O get(long j, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { return applyTransformation(this.val$input.get(j, timeUnit)); } private O applyTransformation(I i) throws ExecutionException { try { return (O) this.val$function.apply(i); } catch (Throwable th) { throw new ExecutionException(th); } } }; } @SafeVarargs public static ListenableFuture> allAsList(ListenableFuture... listenableFutureArr) { return new CollectionFuture.ListFuture(ImmutableList.copyOf(listenableFutureArr), true); } public static ListenableFuture> allAsList(Iterable> iterable) { return new CollectionFuture.ListFuture(ImmutableList.copyOf(iterable), true); } @SafeVarargs public static FutureCombiner whenAllComplete(ListenableFuture... listenableFutureArr) { return new FutureCombiner<>(false, ImmutableList.copyOf(listenableFutureArr)); } public static FutureCombiner whenAllComplete(Iterable> iterable) { return new FutureCombiner<>(false, ImmutableList.copyOf(iterable)); } @SafeVarargs public static FutureCombiner whenAllSucceed(ListenableFuture... listenableFutureArr) { return new FutureCombiner<>(true, ImmutableList.copyOf(listenableFutureArr)); } public static FutureCombiner whenAllSucceed(Iterable> iterable) { return new FutureCombiner<>(true, ImmutableList.copyOf(iterable)); } /* loaded from: classes2.dex */ public static final class FutureCombiner { private final boolean allMustSucceed; private final ImmutableList> futures; private FutureCombiner(boolean z, ImmutableList> immutableList) { this.allMustSucceed = z; this.futures = immutableList; } public final ListenableFuture callAsync(AsyncCallable asyncCallable, Executor executor) { return new CombinedFuture(this.futures, this.allMustSucceed, executor, asyncCallable); } public final ListenableFuture call(Callable callable, Executor executor) { return new CombinedFuture(this.futures, this.allMustSucceed, executor, callable); } public final ListenableFuture run(Runnable runnable, Executor executor) { return call(new Callable(this, runnable) { // from class: com.google.common.util.concurrent.Futures.FutureCombiner.1 final Runnable val$combiner; { this.val$combiner = runnable; } @Override // java.util.concurrent.Callable public Void call() throws Exception { this.val$combiner.run(); return null; } }, executor); } } public static ListenableFuture nonCancellationPropagating(ListenableFuture listenableFuture) { if (listenableFuture.isDone()) { return listenableFuture; } NonCancellationPropagatingFuture nonCancellationPropagatingFuture = new NonCancellationPropagatingFuture(listenableFuture); listenableFuture.addListener(nonCancellationPropagatingFuture, MoreExecutors.directExecutor()); return nonCancellationPropagatingFuture; } /* loaded from: classes2.dex */ static final class NonCancellationPropagatingFuture extends AbstractFuture.TrustedFuture implements Runnable { private ListenableFuture delegate; NonCancellationPropagatingFuture(ListenableFuture listenableFuture) { this.delegate = listenableFuture; } @Override // java.lang.Runnable public final void run() { ListenableFuture listenableFuture = this.delegate; if (listenableFuture != null) { setFuture(listenableFuture); } } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.AbstractFuture public final String pendingToString() { ListenableFuture listenableFuture = this.delegate; if (listenableFuture == null) { return null; } String valueOf = String.valueOf(listenableFuture); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 11); sb.append("delegate=["); sb.append(valueOf); sb.append("]"); return sb.toString(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.AbstractFuture public final void afterDone() { this.delegate = null; } } @SafeVarargs public static ListenableFuture> successfulAsList(ListenableFuture... listenableFutureArr) { return new CollectionFuture.ListFuture(ImmutableList.copyOf(listenableFutureArr), false); } public static ListenableFuture> successfulAsList(Iterable> iterable) { return new CollectionFuture.ListFuture(ImmutableList.copyOf(iterable), false); } public static ImmutableList> inCompletionOrder(Iterable> iterable) { Collection copyOf; if (iterable instanceof Collection) { copyOf = (Collection) iterable; } else { copyOf = ImmutableList.copyOf(iterable); } ListenableFuture[] listenableFutureArr = (ListenableFuture[]) copyOf.toArray(new ListenableFuture[copyOf.size()]); InCompletionOrderState inCompletionOrderState = new InCompletionOrderState(listenableFutureArr); ImmutableList.Builder builder = ImmutableList.builder(); for (int i = 0; i < listenableFutureArr.length; i++) { builder.add((ImmutableList.Builder) new InCompletionOrderFuture(inCompletionOrderState)); } ImmutableList> build = builder.build(); for (int i2 = 0; i2 < listenableFutureArr.length; i2++) { listenableFutureArr[i2].addListener(new Runnable(inCompletionOrderState, build, i2) { // from class: com.google.common.util.concurrent.Futures.3 final ImmutableList val$delegates; final int val$localI; final InCompletionOrderState val$state; { this.val$state = inCompletionOrderState; this.val$delegates = build; this.val$localI = i2; } @Override // java.lang.Runnable public void run() { this.val$state.recordInputCompletion(this.val$delegates, this.val$localI); } }, MoreExecutors.directExecutor()); } return build; } /* loaded from: classes2.dex */ static final class InCompletionOrderFuture extends AbstractFuture { private InCompletionOrderState state; private InCompletionOrderFuture(InCompletionOrderState inCompletionOrderState) { this.state = inCompletionOrderState; } @Override // com.google.common.util.concurrent.AbstractFuture, java.util.concurrent.Future public final boolean cancel(boolean z) { InCompletionOrderState inCompletionOrderState = this.state; if (!super.cancel(z)) { return false; } inCompletionOrderState.recordOutputCancellation(z); return true; } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.AbstractFuture public final String pendingToString() { InCompletionOrderState inCompletionOrderState = this.state; if (inCompletionOrderState == null) { return null; } int length = ((InCompletionOrderState) inCompletionOrderState).inputFutures.length; int i = ((InCompletionOrderState) inCompletionOrderState).incompleteOutputCount.get(); StringBuilder sb = new StringBuilder(49); sb.append("inputCount=["); sb.append(length); sb.append("], remaining=["); sb.append(i); sb.append("]"); return sb.toString(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.util.concurrent.AbstractFuture public final void afterDone() { this.state = null; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class InCompletionOrderState { private volatile int delegateIndex; private final AtomicInteger incompleteOutputCount; private final ListenableFuture[] inputFutures; private boolean shouldInterrupt; private boolean wasCancelled; private InCompletionOrderState(ListenableFuture[] listenableFutureArr) { this.wasCancelled = false; this.shouldInterrupt = true; this.delegateIndex = 0; this.inputFutures = listenableFutureArr; this.incompleteOutputCount = new AtomicInteger(listenableFutureArr.length); } /* JADX INFO: Access modifiers changed from: private */ public void recordOutputCancellation(boolean z) { this.wasCancelled = true; if (!z) { this.shouldInterrupt = false; } recordCompletion(); } /* JADX INFO: Access modifiers changed from: private */ public void recordInputCompletion(ImmutableList> immutableList, int i) { ListenableFuture[] listenableFutureArr = this.inputFutures; ListenableFuture listenableFuture = listenableFutureArr[i]; listenableFutureArr[i] = null; for (int i2 = this.delegateIndex; i2 < immutableList.size(); i2++) { if (immutableList.get(i2).setFuture(listenableFuture)) { recordCompletion(); this.delegateIndex = i2 + 1; return; } } this.delegateIndex = immutableList.size(); } private void recordCompletion() { if (this.incompleteOutputCount.decrementAndGet() == 0 && this.wasCancelled) { for (ListenableFuture listenableFuture : this.inputFutures) { if (listenableFuture != null) { listenableFuture.cancel(this.shouldInterrupt); } } } } } public static void addCallback(ListenableFuture listenableFuture, FutureCallback futureCallback, Executor executor) { Preconditions.checkNotNull(futureCallback); listenableFuture.addListener(new CallbackListener(listenableFuture, futureCallback), executor); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class CallbackListener implements Runnable { final FutureCallback callback; final Future future; CallbackListener(Future future, FutureCallback futureCallback) { this.future = future; this.callback = futureCallback; } @Override // java.lang.Runnable public final void run() { Throwable tryInternalFastPathGetFailure; Future future = this.future; if ((future instanceof InternalFutureFailureAccess) && (tryInternalFastPathGetFailure = InternalFutures.tryInternalFastPathGetFailure((InternalFutureFailureAccess) future)) != null) { this.callback.onFailure(tryInternalFastPathGetFailure); return; } try { this.callback.onSuccess(Futures.getDone(this.future)); } catch (Error e) { e = e; this.callback.onFailure(e); } catch (RuntimeException e2) { e = e2; this.callback.onFailure(e); } catch (ExecutionException e3) { this.callback.onFailure(e3.getCause()); } } public final String toString() { return MoreObjects.toStringHelper(this).addValue(this.callback).toString(); } } public static V getDone(Future future) throws ExecutionException { Preconditions.checkState(future.isDone(), "Future was expected to be done: %s", future); return (V) Uninterruptibles.getUninterruptibly(future); } public static V getChecked(Future future, Class cls) throws Exception { return (V) FuturesGetChecked.getChecked(future, cls); } public static V getChecked(Future future, Class cls, long j, TimeUnit timeUnit) throws Exception { return (V) FuturesGetChecked.getChecked(future, cls, j, timeUnit); } public static V getUnchecked(Future future) { Preconditions.checkNotNull(future); try { return (V) Uninterruptibles.getUninterruptibly(future); } catch (ExecutionException e) { wrapAndThrowUnchecked(e.getCause()); throw new AssertionError(); } } private static void wrapAndThrowUnchecked(Throwable th) { if (th instanceof Error) { throw new ExecutionError((Error) th); } throw new UncheckedExecutionException(th); } }