Skip to content

Commit f2a710a

Browse files
committed
Channel pseudo-constructor is replaced with top-level fun
1 parent e2ceda8 commit f2a710a

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/ArrayChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import kotlin.concurrent.withLock
2525
* Channel with array buffer of a fixed [capacity].
2626
* Sender suspends only when buffer is fully and receiver suspends only when buffer is empty.
2727
*
28+
* This channel is created by `Channel(capacity)` factory function invocation.
29+
*
2830
* This implementation uses lock to protect the buffer, which is held only during very short buffer-update operations.
2931
* The lists of suspended senders or receivers are lock-free.
3032
*/

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/Channel.kt

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package kotlinx.coroutines.experimental.channels
1919
import kotlinx.coroutines.experimental.CancellationException
2020
import kotlinx.coroutines.experimental.CoroutineScope
2121
import kotlinx.coroutines.experimental.Job
22+
import kotlinx.coroutines.experimental.channels.Channel.Factory.CONFLATED
23+
import kotlinx.coroutines.experimental.channels.Channel.Factory.UNLIMITED
2224
import kotlinx.coroutines.experimental.selects.SelectBuilder
2325
import kotlinx.coroutines.experimental.selects.SelectInstance
2426
import kotlinx.coroutines.experimental.selects.select
@@ -243,45 +245,57 @@ public interface ChannelIterator<out E> {
243245
* Conceptually, a channel is similar to [BlockingQueue][java.util.concurrent.BlockingQueue],
244246
* but it has suspending operations instead of blocking ones and it can be closed.
245247
*
246-
* See [Channel()][Channel.invoke] factory function for the description of available channel implementations.
248+
* See `Channel(capacity)` factory function for the description of available channel implementations.
247249
*/
248250
public interface Channel<E> : SendChannel<E>, ReceiveChannel<E> {
249251
/**
250-
* Factory for channels.
252+
* Constants for channel factory function `Channel()`.
251253
*/
252254
public companion object Factory {
253255
/**
254-
* Requests channel with unlimited capacity buffer in [Channel()][invoke] factory function --
256+
* Requests channel with unlimited capacity buffer in `Channel(...)` factory function --
255257
* the [LinkedListChannel] gets created.
256258
*/
257259
public const val UNLIMITED = Int.MAX_VALUE
258260

259261
/**
260-
* Requests conflated channel in [Channel()][invoke] factory function --
262+
* Requests conflated channel in `Channel(...)` factory function --
261263
* the [ConflatedChannel] gets created.
262264
*/
263265
public const val CONFLATED = -1
264266

265267
/**
266268
* Creates a channel with the specified buffer capacity (or without a buffer by default).
267-
*
268-
* The resulting channel type depends on the specified [capacity] parameter:
269-
* * when `capacity` is 0 (default) -- creates [RendezvousChannel] without a buffer;
270-
* * when `capacity` is [UNLIMITED] -- creates [LinkedListChannel] with buffer of unlimited size;
271-
* * when `capacity` is [CONFLATED] -- creates [ConflatedChannel] that conflates back-to-back sends;
272-
* * when `capacity` is positive, but less than [UNLIMITED] -- creates [ArrayChannel] with a buffer of the specified `capacity`;
273-
* * otherwise -- throws [IllegalArgumentException].
269+
* @suppress **Deprecated**
274270
*/
275-
public operator fun <E> invoke(capacity: Int = 0): Channel<E> =
276-
when (capacity) {
277-
0 -> RendezvousChannel()
278-
UNLIMITED -> LinkedListChannel()
279-
CONFLATED -> ConflatedChannel()
280-
else -> ArrayChannel(capacity)
281-
}
271+
@Deprecated("Replaced with top-level function", level = DeprecationLevel.HIDDEN)
272+
public operator fun <E> invoke(capacity: Int = 0): Channel<E> = Channel(capacity)
282273
}
283274
}
284275

276+
/**
277+
* Creates a channel without a buffer -- [RendezvousChannel].
278+
*/
279+
public fun <E> Channel(): Channel<E> = RendezvousChannel<E>()
280+
281+
/**
282+
* Creates a channel with the specified buffer capacity (or without a buffer by default).
283+
*
284+
* The resulting channel type depends on the specified [capacity] parameter:
285+
* * when `capacity` is 0 -- creates [RendezvousChannel] without a buffer;
286+
* * when `capacity` is [Channel.UNLIMITED] -- creates [LinkedListChannel] with buffer of unlimited size;
287+
* * when `capacity` is [Channel.CONFLATED] -- creates [ConflatedChannel] that conflates back-to-back sends;
288+
* * when `capacity` is positive, but less than [UNLIMITED] -- creates [ArrayChannel] with a buffer of the specified `capacity`;
289+
* * otherwise -- throws [IllegalArgumentException].
290+
*/
291+
public fun <E> Channel(capacity: Int): Channel<E> =
292+
when (capacity) {
293+
0 -> RendezvousChannel()
294+
UNLIMITED -> LinkedListChannel()
295+
CONFLATED -> ConflatedChannel()
296+
else -> ArrayChannel(capacity)
297+
}
298+
285299
/**
286300
* Indicates attempt to [send][SendChannel.send] on [isClosedForSend][SendChannel.isClosedForSend] channel
287301
* that was closed _normally_. A _failed_ channel rethrows the original [close][SendChannel.close] cause

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/ConflatedChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import kotlinx.coroutines.experimental.selects.SelectInstance
2626
* while previously sent elements **are lost**.
2727
* Sender to this channel never suspends and [offer] always returns `true`.
2828
*
29+
* This channel is created by `Channel(Channel.CONFLATED)` factory function invocation.
30+
*
2931
* This implementation is fully lock-free.
3032
*/
3133
public open class ConflatedChannel<E> : AbstractChannel<E>() {

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/LinkedListChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import kotlinx.coroutines.experimental.selects.SelectInstance
2323
* Channel with linked-list buffer of a unlimited capacity (limited only by available memory).
2424
* Sender to this channel never suspends and [offer] always returns `true`.
2525
*
26+
* This channel is created by `Channel(Channel.UNLIMITED)` factory function invocation.
27+
*
2628
* This implementation is fully lock-free.
2729
*/
2830
public open class LinkedListChannel<E> : AbstractChannel<E>() {

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/RendezvousChannel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package kotlinx.coroutines.experimental.channels
2121
* to receiver only when [send] and [receive] invocations meet in time (rendezvous), so [send] suspends
2222
* until another coroutine invokes [receive] and [receive] suspends until another coroutine invokes [send].
2323
*
24+
* Use `Channel()` factory function to conveniently create an instance of rendezvous channel.
25+
*
2426
* This implementation is fully lock-free.
2527
*/
2628
public open class RendezvousChannel<E> : AbstractChannel<E>() {

0 commit comments

Comments
 (0)