Skip to content

Commit 34330f5

Browse files
Francesco Vascoelizarov
authored andcommitted
A "CompletableDeferred" interface with a private implementation (#70)
1 parent 43e3af7 commit 34330f5

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CompletableDeferred.kt

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,50 @@ package kotlinx.coroutines.experimental
1919
import kotlinx.coroutines.experimental.selects.SelectInstance
2020

2121
/**
22-
* Concrete implementation of [Deferred] that can be completed via public functions
22+
* A [Deferred] that can be completed via public functions
2323
* [complete], [completeExceptionally], and [cancel].
2424
*
2525
* Completion functions return `false` when this deferred value is already complete.
2626
*/
27-
@Suppress("UNCHECKED_CAST")
28-
public class CompletableDeferred<T> : JobSupport(true), Deferred<T> {
29-
override fun getCompleted(): T = getCompletedInternal() as T
30-
suspend override fun await(): T = awaitInternal() as T
31-
override fun <R> registerSelectAwait(select: SelectInstance<R>, block: suspend (T) -> R) =
32-
registerSelectAwaitInternal(select, block as (suspend (Any?) -> R))
33-
27+
public interface CompletableDeferred<T> : Job, Deferred<T> {
3428
/**
3529
* Completes this deferred value with a given [value]. The result is `true` if this deferred was
3630
* completed as a result of this invocation and `false` otherwise (if it was already completed).
3731
*
3832
* Repeated invocations of this function have no effect and always produce `false`.
3933
*/
40-
public fun complete(value: T): Boolean {
34+
public fun complete(value: T): Boolean
35+
36+
/**
37+
* Completes this deferred value exceptionally with a given [exception]. The result is `true` if this deferred was
38+
* completed as a result of this invocation and `false` otherwise (if it was already completed).
39+
*
40+
* Repeated invocations of this function have no effect and always produce `false`.
41+
*/
42+
public fun completeExceptionally(exception: Throwable): Boolean
43+
}
44+
45+
/**
46+
* Create a [CompletableDeferred] not completed
47+
*/
48+
public fun <T> CompletableDeferred(): CompletableDeferred<T> = CompletableDeferredImpl()
49+
50+
/**
51+
* Create an already completed [CompletableDeferred] with a given [value]
52+
*/
53+
public fun <T> CompletableDeferred(value: T): CompletableDeferred<T> = CompletableDeferredImpl<T>().apply { complete(value) }
54+
55+
/**
56+
* Concrete implementation of [CompletableDeferred].
57+
*/
58+
@Suppress("UNCHECKED_CAST")
59+
private class CompletableDeferredImpl<T> : JobSupport(true), CompletableDeferred<T> {
60+
override fun getCompleted(): T = getCompletedInternal() as T
61+
suspend override fun await(): T = awaitInternal() as T
62+
override fun <R> registerSelectAwait(select: SelectInstance<R>, block: suspend (T) -> R) =
63+
registerSelectAwaitInternal(select, block as (suspend (Any?) -> R))
64+
65+
override fun complete(value: T): Boolean {
4166
lockFreeLoopOnState { state ->
4267
when (state) {
4368
is Incomplete -> {
@@ -50,13 +75,7 @@ public class CompletableDeferred<T> : JobSupport(true), Deferred<T> {
5075
}
5176
}
5277

53-
/**
54-
* Completes this deferred value exceptionally with a given [exception]. The result is `true` if this deferred was
55-
* completed as a result of this invocation and `false` otherwise (if it was already completed).
56-
*
57-
* Repeated invocations of this function have no effect and always produce `false`.
58-
*/
59-
public fun completeExceptionally(exception: Throwable): Boolean {
78+
override fun completeExceptionally(exception: Throwable): Boolean {
6079
lockFreeLoopOnState { state ->
6180
when (state) {
6281
is Incomplete -> {
@@ -68,4 +87,4 @@ public class CompletableDeferred<T> : JobSupport(true), Deferred<T> {
6887
}
6988
}
7089
}
71-
}
90+
}

0 commit comments

Comments
 (0)