Add FluentFutures.submit() 09/106809/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Jul 2023 19:28:54 +0000 (21:28 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Jul 2023 19:28:54 +0000 (21:28 +0200)
This method mirrors Futures.submit(), but results in a FluentFuture.

Change-Id: Ie56ff26cb12a74b5021495cb33029c799d0cb721
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/concurrent/FluentFutures.java

index c94a74fea4cb915aa0110d613514e00a0e856918..3076f53a5d1fa7941a4e3d9d497557ec62c7ff88 100644 (file)
@@ -12,6 +12,8 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.annotations.Beta;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.Futures;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executor;
 import java.util.concurrent.Future;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -121,4 +123,17 @@ public final class FluentFutures {
     public static FluentFuture<Boolean> immediateFalseFluentFuture() {
         return FALSE_FUTURE;
     }
+
+    /**
+     * Submit a {@link Callable} to specified {@link Executor} and return a {@link FluentFuture} that completes with the
+     * result of the {@link Callable}.
+     *
+     * @param <T> Callable return type
+     * @param callable The Callable to call
+     * @param executor The Executor to use
+     * @throws NullPointerException if any argument is {@code null}
+     */
+    public static <T> FluentFuture<T> submit(final Callable<T> callable, final Executor executor) {
+        return FluentFuture.from(Futures.submit(callable, executor));
+    }
 }