-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
295 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
python-for-android/dists/kolibri/src/main/java/org/learningequality/FuturesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.learningequality; | ||
|
||
import android.util.Log; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executor; | ||
|
||
import java9.util.concurrent.CompletableFuture; | ||
|
||
public class FuturesUtil { | ||
public static final String TAG = "Kolibri.FuturesUtil"; | ||
|
||
public static <T> CompletableFuture<T> toCompletable(ListenableFuture<T> future, Executor executor) { | ||
CompletableFuture<T> completableFuture = new CompletableFuture<>(); | ||
future.addListener(() -> { | ||
try { | ||
completableFuture.complete(future.get(3, java.util.concurrent.TimeUnit.SECONDS)); | ||
Log.d(TAG, "Future completed"); | ||
} catch (InterruptedException | ExecutionException e) { | ||
Log.d(TAG, "Future encountered exception"); | ||
completableFuture.completeExceptionally(e); | ||
} catch (java.util.concurrent.TimeoutException e) { | ||
Log.d(TAG, "Future timed out"); | ||
completableFuture.completeExceptionally(e); | ||
} | ||
}, executor); | ||
completableFuture.whenCompleteAsync((result, error) -> { | ||
if (completableFuture.isCancelled()) { | ||
Log.d(TAG, "Propagating cancellation to future"); | ||
future.cancel(true); | ||
} | ||
}, executor); | ||
return completableFuture; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.