Roman Elizarov | f2239e1 | 2018-01-10 16:25:25 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.internalAnnotations.* |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 20 | import kotlin.coroutines.experimental.* |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 21 | |
Roman Elizarov | f2239e1 | 2018-01-10 16:25:25 +0300 | [diff] [blame] | 22 | /** |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 23 | * Class for an internal state of a job that had completed exceptionally, including cancellation. |
| 24 | * |
| 25 | * **Note: This class cannot be used outside of internal coroutines framework**. |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 26 | * **Note: cannot be internal until we get rid of MutableDelegateContinuation in IO** |
Roman Elizarov | f2239e1 | 2018-01-10 16:25:25 +0300 | [diff] [blame] | 27 | * |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 28 | * @param cause the exceptional completion cause. It's either original exceptional cause |
| 29 | * or artificial JobCancellationException if no cause was provided |
Roman Elizarov | f2239e1 | 2018-01-10 16:25:25 +0300 | [diff] [blame] | 30 | * @suppress **This is unstable API and it is subject to change.** |
| 31 | */ |
Roman Elizarov | e89cd68 | 2018-04-25 13:03:40 +0300 | [diff] [blame] | 32 | open class CompletedExceptionally( |
| 33 | @JvmField public val cause: Throwable |
Roman Elizarov | f2239e1 | 2018-01-10 16:25:25 +0300 | [diff] [blame] | 34 | ) { |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 35 | override fun toString(): String = "$classSimpleName[$cause]" |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
| 39 | * A specific subclass of [CompletedExceptionally] for cancelled jobs. |
| 40 | * |
| 41 | * **Note: This class cannot be used outside of internal coroutines framework**. |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 42 | * TODO rename to CancelledJob? |
| 43 | * |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 44 | * @param job the job that was cancelled. |
Vsevolod Tolstopyatov | 4b9a559 | 2018-04-11 13:17:14 +0300 | [diff] [blame] | 45 | * @param cause the exceptional completion cause. If `cause` is null, then a [JobCancellationException] is created. |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 46 | * @suppress **This is unstable API and it is subject to change.** |
| 47 | */ |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 48 | internal class Cancelled( |
Roman Elizarov | 6d9f40f | 2018-04-28 14:44:02 +0300 | [diff] [blame] | 49 | job: Job, |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 50 | cause: Throwable? |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 51 | ) : CompletedExceptionally(cause ?: JobCancellationException("Job was cancelled normally", null, job)) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 52 | |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 53 | /** |
| 54 | * A specific subclass of [CompletedExceptionally] for cancelled [AbstractContinuation]. |
| 55 | * |
| 56 | * **Note: This class cannot be used outside of internal coroutines framework**. |
| 57 | * |
| 58 | * @param continuation the continuation that was cancelled. |
| 59 | * @param cause the exceptional completion cause. If `cause` is null, then a [JobCancellationException] |
| 60 | * if created on first get from [exception] property. |
| 61 | * @suppress **This is unstable API and it is subject to change.** |
| 62 | */ |
| 63 | public class CancelledContinuation( |
Roman Elizarov | 6d9f40f | 2018-04-28 14:44:02 +0300 | [diff] [blame] | 64 | continuation: Continuation<*>, |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 65 | cause: Throwable? |
Roman Elizarov | 6d9f40f | 2018-04-28 14:44:02 +0300 | [diff] [blame] | 66 | ) : CompletedExceptionally(cause ?: CancellationException("Continuation $continuation was cancelled normally")) |