blob: 8cf1940381b2d7ad908395482c491ee621f9f9aa [file] [log] [blame]
Francesco Vasco66d18c02017-08-04 18:10:09 +02001/*
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
17package kotlinx.coroutines.experimental.channels8
18
19import kotlinx.coroutines.experimental.CommonPool
Francesco Vasco66d18c02017-08-04 18:10:09 +020020import kotlinx.coroutines.experimental.channels.ReceiveChannel
21import kotlinx.coroutines.experimental.channels.consumeEach
22import kotlinx.coroutines.experimental.channels.produce
23import kotlinx.coroutines.experimental.runBlocking
24import java.util.*
25import java.util.function.BiConsumer
26import java.util.function.Consumer
27import java.util.stream.Collector
28import java.util.stream.Stream
29import java.util.stream.StreamSupport
30import kotlin.coroutines.experimental.CoroutineContext
31
32/**
33 * Creates a [ProducerJob] to read all element of the [Stream].
34 */
Roman Elizarovb555d912017-08-17 21:01:33 +030035public fun <E> Stream<E>.asReceiveChannel(context: CoroutineContext = CommonPool): ReceiveChannel<E> = produce(context) {
Francesco Vasco66d18c02017-08-04 18:10:09 +020036 for (element in this@asReceiveChannel)
37 send(element)
38}
39
40/**
41 * Creates a [Stream] of elements in this [ReceiveChannel].
42 */
43public fun <E : Any> ReceiveChannel<E>.asStream(): Stream<E> = StreamSupport.stream<E>(SpliteratorAdapter(this), false)
44
45/**
46 * Applies the [collector] to the [ReceiveChannel]
47 */
48public 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
55private 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}