-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extension function to convert a ListenableFuture to a CompletableFutu…
…re (#8) * Added an extension function to convert a ListenableFuture to a CompletableFuture * Using a bigger delay.
- Loading branch information
Showing
11 changed files
with
92 additions
and
25 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
Binary file not shown.
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#Sun Aug 05 14:30:33 CDT 2018 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-all.zip |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies { | ||
compile project(":kotlin-futures-guava") | ||
} |
19 changes: 19 additions & 0 deletions
19
...rc/main/kotlin/io/github/vjames19/futures/guava/ListenableFutureToCompletableFutureExt.kt
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,19 @@ | ||
package io.github.vjames19.futures.guava | ||
|
||
import com.google.common.util.concurrent.ListenableFuture | ||
import io.github.vjames19.futures.jdk8.DirectExecutor | ||
import io.github.vjames19.futures.jdk8.onComplete | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Executor | ||
|
||
fun <T> ListenableFuture<T>.toCompletableFuture(executor: Executor = DirectExecutor): CompletableFuture<T> { | ||
val future = CompletableFuture<T>() | ||
|
||
onComplete( | ||
executor = executor, | ||
onFailure = { future.completeExceptionally(it) }, | ||
onSuccess = { future.complete(it)} | ||
) | ||
|
||
return future | ||
} |
39 changes: 39 additions & 0 deletions
39
...c/test/kotlin/io/github/vjames19/futures/guava/ListenableFutureToCompletableFutureSpec.kt
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,39 @@ | ||
package io.github.vjames19.futures.guava | ||
|
||
import io.github.vjames19.futures.jdk8.ImmediateFuture | ||
import io.github.vjames19.futures.jdk8.toListenableFuture | ||
import org.amshove.kluent.shouldEqualTo | ||
import org.amshove.kluent.shouldThrowTheException | ||
import org.amshove.kluent.withCause | ||
import org.jetbrains.spek.api.Spek | ||
import org.jetbrains.spek.api.dsl.describe | ||
import org.jetbrains.spek.api.dsl.given | ||
import org.jetbrains.spek.api.dsl.it | ||
import java.util.concurrent.ExecutionException | ||
|
||
|
||
object ListenableFutureToCompletableFutureSpec : Spek({ | ||
|
||
describe("toCompletableFuture") { | ||
given("a successful future") { | ||
it("it should be able to return a successful CompletableFuture") { | ||
val listenableFuture = ImmediateFuture { 10 } | ||
val future = listenableFuture.toCompletableFuture() | ||
|
||
future.isDone shouldEqualTo true | ||
future.isCompletedExceptionally shouldEqualTo false | ||
|
||
future.get() shouldEqualTo 10 | ||
} | ||
} | ||
|
||
given("a failed future") { | ||
it("should return the exception returned by the ListenableFuture") { | ||
val listenableFuture = IllegalArgumentException().toListenableFuture<String>() | ||
val future = listenableFuture.toCompletableFuture() | ||
|
||
({ future.get() }) shouldThrowTheException ExecutionException::class withCause IllegalArgumentException::class | ||
} | ||
} | ||
} | ||
}) |
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
4 changes: 2 additions & 2 deletions
4
...in-futures-guava/src/test/kotlin/io/github/vjames19/futures/guava/ListenableFutureSpec.kt
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