-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
1,536 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...inx-coroutines-jdk8/src/main/kotlin/kotlinx/coroutines/experimental/channels8/Channels.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,82 @@ | ||
/* | ||
* Copyright 2016-2017 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package kotlinx.coroutines.experimental.channels8 | ||
|
||
import kotlinx.coroutines.experimental.CommonPool | ||
import kotlinx.coroutines.experimental.channels.ProducerJob | ||
import kotlinx.coroutines.experimental.channels.ReceiveChannel | ||
import kotlinx.coroutines.experimental.channels.consumeEach | ||
import kotlinx.coroutines.experimental.channels.produce | ||
import kotlinx.coroutines.experimental.runBlocking | ||
import java.util.* | ||
import java.util.function.BiConsumer | ||
import java.util.function.Consumer | ||
import java.util.stream.Collector | ||
import java.util.stream.Stream | ||
import java.util.stream.StreamSupport | ||
import kotlin.coroutines.experimental.CoroutineContext | ||
|
||
/** | ||
* Creates a [ProducerJob] to read all element of the [Stream]. | ||
*/ | ||
public fun <E> Stream<E>.asReceiveChannel(context: CoroutineContext = CommonPool): ProducerJob<E> = produce(context) { | ||
for (element in this@asReceiveChannel) | ||
send(element) | ||
} | ||
|
||
/** | ||
* Creates a [Stream] of elements in this [ReceiveChannel]. | ||
*/ | ||
public fun <E : Any> ReceiveChannel<E>.asStream(): Stream<E> = StreamSupport.stream<E>(SpliteratorAdapter(this), false) | ||
|
||
/** | ||
* Applies the [collector] to the [ReceiveChannel] | ||
*/ | ||
public suspend fun <T, A : Any, R> ReceiveChannel<T>.collect(collector: Collector<T, A, R>): R { | ||
val container: A = collector.supplier().get() | ||
val accumulator: BiConsumer<A, T> = collector.accumulator() | ||
consumeEach { accumulator.accept(container, it) } | ||
return collector.finisher().apply(container) | ||
} | ||
|
||
private class SpliteratorAdapter<E : Any>(val channel: ReceiveChannel<E>) : Spliterator<E> { | ||
override fun estimateSize(): Long = Long.MAX_VALUE | ||
|
||
override fun forEachRemaining(action: Consumer<in E>) { | ||
runBlocking { | ||
for (element in channel) | ||
action.accept(element) | ||
} | ||
} | ||
|
||
override fun tryAdvance(action: Consumer<in E>): Boolean = runBlocking { | ||
val element = channel.receiveOrNull() | ||
if (element != null) { | ||
action.accept(element) | ||
true | ||
} else false | ||
} | ||
|
||
override fun characteristics(): Int = characteristics | ||
|
||
override fun trySplit(): Spliterator<E>? = null | ||
|
||
private companion object { | ||
@JvmStatic | ||
private val characteristics = Spliterator.ORDERED or Spliterator.NONNULL | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...coroutines-jdk8/src/test/kotlin/kotlinx/coroutines/experimental/channels8/ChannelsTest.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,28 @@ | ||
package kotlinx.coroutines.experimental.channels8 | ||
|
||
import kotlinx.coroutines.experimental.TestBase | ||
import kotlinx.coroutines.experimental.channels.asReceiveChannel | ||
import kotlinx.coroutines.experimental.channels.toList | ||
import kotlinx.coroutines.experimental.runBlocking | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import java.util.stream.Collectors | ||
|
||
class ChannelsTest : TestBase() { | ||
private val testList = listOf(1, 2, 3) | ||
|
||
@Test | ||
fun testCollect() = runBlocking { | ||
assertEquals(testList, testList.asReceiveChannel().collect(Collectors.toList())) | ||
} | ||
|
||
@Test | ||
fun testStreamAsReceiveChannel() = runBlocking { | ||
assertEquals(testList, testList.stream().asReceiveChannel().toList()) | ||
} | ||
|
||
@Test | ||
fun testReceiveChannelAsStream() { | ||
assertEquals(testList, testList.asReceiveChannel().asStream().collect(Collectors.toList())) | ||
} | ||
} |
Oops, something went wrong.