From e0b87c3ff228763de98068ca9a2b7f6af6e04138 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 27 Mar 2020 14:19:50 +0530 Subject: Convert AsyncWorker to Kotlin Signed-off-by: Harsh Shandilya --- .../com/wireguard/android/util/AsyncWorker.java | 66 ---------------------- .../java/com/wireguard/android/util/AsyncWorker.kt | 43 ++++++++++++++ 2 files changed, 43 insertions(+), 66 deletions(-) delete mode 100644 ui/src/main/java/com/wireguard/android/util/AsyncWorker.java create mode 100644 ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt (limited to 'ui/src/main/java/com/wireguard/android/util') diff --git a/ui/src/main/java/com/wireguard/android/util/AsyncWorker.java b/ui/src/main/java/com/wireguard/android/util/AsyncWorker.java deleted file mode 100644 index 461e2b64..00000000 --- a/ui/src/main/java/com/wireguard/android/util/AsyncWorker.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package com.wireguard.android.util; - -import android.os.Handler; - -import com.wireguard.util.NonNullForAll; - -import java.util.concurrent.Executor; - -import java9.util.concurrent.CompletableFuture; -import java9.util.concurrent.CompletionStage; - -/** - * Helper class for running asynchronous tasks and ensuring they are completed on the main thread. - */ - -@NonNullForAll -public class AsyncWorker { - private final Executor executor; - private final Handler handler; - - public AsyncWorker(final Executor executor, final Handler handler) { - this.executor = executor; - this.handler = handler; - } - - public CompletionStage runAsync(final AsyncRunnable runnable) { - final CompletableFuture future = new CompletableFuture<>(); - executor.execute(() -> { - try { - runnable.run(); - handler.post(() -> future.complete(null)); - } catch (final Throwable t) { - handler.post(() -> future.completeExceptionally(t)); - } - }); - return future; - } - - public CompletionStage supplyAsync(final AsyncSupplier supplier) { - final CompletableFuture future = new CompletableFuture<>(); - executor.execute(() -> { - try { - final T result = supplier.get(); - handler.post(() -> future.complete(result)); - } catch (final Throwable t) { - handler.post(() -> future.completeExceptionally(t)); - } - }); - return future; - } - - @FunctionalInterface - public interface AsyncRunnable { - void run() throws E; - } - - @FunctionalInterface - public interface AsyncSupplier { - T get() throws E; - } -} diff --git a/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt b/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt new file mode 100644 index 00000000..fd5eee2f --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt @@ -0,0 +1,43 @@ +/* + * Copyright © 2017-2020 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package com.wireguard.android.util + +import android.os.Handler +import java.util.concurrent.Executor +import java9.util.concurrent.CompletableFuture +import java9.util.concurrent.CompletionStage + +/** + * Helper class for running asynchronous tasks and ensuring they are completed on the main thread. + */ + +class AsyncWorker(private val executor: Executor, private val handler: Handler) { + + fun runAsync(run: () -> Unit): CompletionStage { + val future = CompletableFuture() + executor.execute { + try { + run() + handler.post { future.complete(null) } + } catch (t: Throwable) { + handler.post { future.completeExceptionally(t) } + } + } + return future + } + + fun supplyAsync(get: () -> T?): CompletionStage { + val future = CompletableFuture() + executor.execute { + try { + val result = get() + handler.post { future.complete(result) } + } catch (t: Throwable) { + handler.post { future.completeExceptionally(t) } + } + } + return future + } +} -- cgit v1.2.3