/* * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.libraries.mobiledatadownload.tracing; import com.google.common.base.Function; import com.google.common.util.concurrent.AsyncCallable; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures.FutureCombiner; import com.google.common.util.concurrent.ListenableFuture; import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.checkerframework.checker.nullness.qual.Nullable; /** Wrapper around {@link Futures}. */ public final class PropagatedFutures { private PropagatedFutures() {} public static ListenableFuture transformAsync( ListenableFuture input, AsyncFunction function, Executor executor) { return Futures.transformAsync(input, function, executor); } public static ListenableFuture transform( ListenableFuture input, Function function, Executor executor) { return Futures.transform(input, function, executor); } public static void addCallback( ListenableFuture future, FutureCallback callback, Executor executor) { Futures.addCallback(future, callback, executor); } public static ListenableFuture catching( ListenableFuture input, Class exceptionType, Function fallback, Executor executor) { return Futures.catching(input, exceptionType, fallback, executor); } public static ListenableFuture catchingAsync( ListenableFuture input, Class exceptionType, AsyncFunction fallback, Executor executor) { return Futures.catchingAsync(input, exceptionType, fallback, executor); } public static ListenableFuture submit( Callable callable, Executor executor) { return Futures.submit(callable, executor); } public static ListenableFuture<@Nullable Void> submit(Runnable runnable, Executor executor) { return Futures.submit(runnable, executor); } public static ListenableFuture submitAsync( AsyncCallable callable, Executor executor) { return Futures.submitAsync(callable, executor); } public static ListenableFuture scheduleAsync( AsyncCallable callable, long delay, TimeUnit timeUnit, ScheduledExecutorService executor) { return Futures.scheduleAsync(callable, delay, timeUnit, executor); } @SafeVarargs public static PropagatedFutureCombiner whenAllComplete( ListenableFuture... futures) { return new PropagatedFutureCombiner<>(Futures.whenAllComplete(futures)); } public static PropagatedFutureCombiner whenAllComplete( Iterable> futures) { return new PropagatedFutureCombiner<>(Futures.whenAllComplete(futures)); } @SafeVarargs public static PropagatedFutureCombiner whenAllSucceed( ListenableFuture... futures) { return new PropagatedFutureCombiner<>(Futures.whenAllSucceed(futures)); } public static PropagatedFutureCombiner whenAllSucceed( Iterable> futures) { return new PropagatedFutureCombiner<>(Futures.whenAllSucceed(futures)); } /** Wrapper around {@link FutureCombiner}. */ public static final class PropagatedFutureCombiner { private final FutureCombiner futureCombiner; private PropagatedFutureCombiner(FutureCombiner futureCombiner) { this.futureCombiner = futureCombiner; } public ListenableFuture callAsync( AsyncCallable combiner, Executor executor) { return futureCombiner.callAsync(combiner, executor); } public ListenableFuture call( Callable combiner, Executor executor) { return futureCombiner.call(combiner, executor); } public ListenableFuture run(final Runnable combiner, Executor executor) { return futureCombiner.run(combiner, executor); } } }