Skip to content

Commit 98a39d0

Browse files
Francesco Vascoelizarov
Francesco Vasco
authored andcommitted
java.time adapters
1 parent 3a78aa2 commit 98a39d0

File tree

1 file changed

+44
-0
lines changed
  • integration/kotlinx-coroutines-jdk8/src/main/kotlin/kotlinx/coroutines/experimental/time

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
package kotlinx.coroutines.experimental.time
17+
18+
import kotlinx.coroutines.experimental.selects.SelectBuilder
19+
import java.time.Duration
20+
import java.util.concurrent.TimeUnit
21+
22+
/**
23+
* "java.time" adapter method for [kotlinx.coroutines.experimental.delay]
24+
*/
25+
public suspend fun delay(duration: Duration) =
26+
kotlinx.coroutines.experimental.delay(duration.toNanos(), TimeUnit.NANOSECONDS)
27+
28+
/**
29+
* "java.time" adapter method for [SelectBuilder.onTimeout]
30+
*/
31+
public suspend fun <R> SelectBuilder<R>.onTimeout(duration: Duration, block: suspend () -> R) =
32+
onTimeout(duration.toNanos(), TimeUnit.NANOSECONDS, block)
33+
34+
/**
35+
* "java.time" adapter method for [kotlinx.coroutines.experimental.withTimeout]
36+
*/
37+
public suspend fun <T> withTimeout(duration: Duration, block: suspend () -> T): T =
38+
kotlinx.coroutines.experimental.withTimeout(duration.toNanos(), TimeUnit.NANOSECONDS, block)
39+
40+
/**
41+
* "java.time" adapter method for [kotlinx.coroutines.experimental.withTimeoutOrNull]
42+
*/
43+
public suspend fun <T> withTimeoutOrNull(duration: Duration, block: suspend () -> T): T? =
44+
kotlinx.coroutines.experimental.withTimeoutOrNull(duration.toNanos(), TimeUnit.NANOSECONDS, block)

0 commit comments

Comments
 (0)