@@ -19,25 +19,50 @@ package kotlinx.coroutines.experimental
19
19
import kotlinx.coroutines.experimental.selects.SelectInstance
20
20
21
21
/* *
22
- * Concrete implementation of [Deferred] that can be completed via public functions
22
+ * A [Deferred] that can be completed via public functions
23
23
* [complete], [completeExceptionally], and [cancel].
24
24
*
25
25
* Completion functions return `false` when this deferred value is already complete.
26
26
*/
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 > {
34
28
/* *
35
29
* Completes this deferred value with a given [value]. The result is `true` if this deferred was
36
30
* completed as a result of this invocation and `false` otherwise (if it was already completed).
37
31
*
38
32
* Repeated invocations of this function have no effect and always produce `false`.
39
33
*/
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 {
41
66
lockFreeLoopOnState { state ->
42
67
when (state) {
43
68
is Incomplete -> {
@@ -50,13 +75,7 @@ public class CompletableDeferred<T> : JobSupport(true), Deferred<T> {
50
75
}
51
76
}
52
77
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 {
60
79
lockFreeLoopOnState { state ->
61
80
when (state) {
62
81
is Incomplete -> {
@@ -68,4 +87,4 @@ public class CompletableDeferred<T> : JobSupport(true), Deferred<T> {
68
87
}
69
88
}
70
89
}
71
- }
90
+ }
0 commit comments