Francesco Vasco | 66d18c0 | 2017-08-04 18:10:09 +0200 | [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.channels8 |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.CommonPool |
Francesco Vasco | 66d18c0 | 2017-08-04 18:10:09 +0200 | [diff] [blame] | 20 | import kotlinx.coroutines.experimental.channels.ReceiveChannel |
| 21 | import kotlinx.coroutines.experimental.channels.consumeEach |
| 22 | import kotlinx.coroutines.experimental.channels.produce |
| 23 | import kotlinx.coroutines.experimental.runBlocking |
| 24 | import java.util.* |
| 25 | import java.util.function.BiConsumer |
| 26 | import java.util.function.Consumer |
| 27 | import java.util.stream.Collector |
| 28 | import java.util.stream.Stream |
| 29 | import java.util.stream.StreamSupport |
| 30 | import kotlin.coroutines.experimental.CoroutineContext |
| 31 | |
| 32 | /** |
| 33 | * Creates a [ProducerJob] to read all element of the [Stream]. |
| 34 | */ |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 35 | public fun <E> Stream<E>.asReceiveChannel(context: CoroutineContext = CommonPool): ReceiveChannel<E> = produce(context) { |
Francesco Vasco | 66d18c0 | 2017-08-04 18:10:09 +0200 | [diff] [blame] | 36 | for (element in this@asReceiveChannel) |
| 37 | send(element) |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Creates a [Stream] of elements in this [ReceiveChannel]. |
| 42 | */ |
| 43 | public fun <E : Any> ReceiveChannel<E>.asStream(): Stream<E> = StreamSupport.stream<E>(SpliteratorAdapter(this), false) |
| 44 | |
| 45 | /** |
| 46 | * Applies the [collector] to the [ReceiveChannel] |
| 47 | */ |
| 48 | public suspend fun <T, A : Any, R> ReceiveChannel<T>.collect(collector: Collector<T, A, R>): R { |
| 49 | val container: A = collector.supplier().get() |
| 50 | val accumulator: BiConsumer<A, T> = collector.accumulator() |
| 51 | consumeEach { accumulator.accept(container, it) } |
| 52 | return collector.finisher().apply(container) |
| 53 | } |
| 54 | |
| 55 | private class SpliteratorAdapter<E : Any>(val channel: ReceiveChannel<E>) : Spliterator<E> { |
| 56 | override fun estimateSize(): Long = Long.MAX_VALUE |
| 57 | |
| 58 | override fun forEachRemaining(action: Consumer<in E>) { |
| 59 | runBlocking { |
| 60 | for (element in channel) |
| 61 | action.accept(element) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | override fun tryAdvance(action: Consumer<in E>): Boolean = runBlocking { |
| 66 | val element = channel.receiveOrNull() |
| 67 | if (element != null) { |
| 68 | action.accept(element) |
| 69 | true |
| 70 | } else false |
| 71 | } |
| 72 | |
| 73 | override fun characteristics(): Int = characteristics |
| 74 | |
| 75 | override fun trySplit(): Spliterator<E>? = null |
| 76 | |
| 77 | private companion object { |
| 78 | @JvmStatic |
| 79 | private val characteristics = Spliterator.ORDERED or Spliterator.NONNULL |
| 80 | } |
| 81 | } |