All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reactor.core.publisher.FluxExtensions.kt Maven / Gradle / Ivy

/*
 * Copyright (c) 2011-2017 Pivotal Software Inc, All Rights Reserved.
 *
 * 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 reactor.core.publisher

import org.reactivestreams.Publisher
import java.util.stream.Stream
import kotlin.reflect.KClass


/**
 * Extension to convert any [Publisher] of [T] to a [Flux].
 *
 * Note this extension doesn't make much sense on a [Flux] but it won't be converted so it
 * doesn't hurt.
 *
 * @author Simon Baslé
 * @since 3.1.1
 */
fun  Publisher.toFlux(): Flux = Flux.from(this)

/**
 * Extension for transforming an [Iterator] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Iterator.toFlux(): Flux = toIterable().toFlux()

/**
 * Extension for transforming an [Iterable] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Iterable.toFlux(): Flux = Flux.fromIterable(this)

/**
 * Extension for transforming a [Sequence] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Sequence.toFlux(): Flux = Flux.fromIterable(object : Iterable {
    override fun iterator(): Iterator = [email protected]()
})

/**
 * Extension for transforming a [Stream] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Stream.toFlux(): Flux = Flux.fromStream(this)

/**
 * Extension for transforming a [BooleanArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun BooleanArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming a [ByteArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun ByteArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming a [ShortArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun ShortArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming a [IntArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun IntArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming a [LongArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun LongArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming a [FloatArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun FloatArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming a [DoubleArray] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun DoubleArray.toFlux(): Flux = this.toList().toFlux()

/**
 * Extension for transforming an [Array] to a [Flux].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Array.toFlux(): Flux = Flux.fromArray(this)

private fun  Iterator.toIterable() = object : Iterable {
    override fun iterator(): Iterator = this@toIterable
}

/**
 * Extension for transforming an exception to a [Flux] that completes with the specified error.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Throwable.toFlux(): Flux = Flux.error(this)

/**
 * Extension for [Flux.cast] providing a `cast()` variant.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
inline fun  Flux<*>.cast(): Flux = cast(T::class.java)


/**
 * Extension for [Flux.doOnError] providing a [KClass] based variant.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Flux.doOnError(exceptionType: KClass, onError: (E) -> Unit): Flux =
        doOnError(exceptionType.java, { onError(it) })

/**
 * Extension for [Flux.onErrorMap] providing a [KClass] based variant.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Flux.onErrorMap(exceptionType: KClass, mapper: (E) -> Throwable): Flux =
        onErrorMap(exceptionType.java, { mapper(it) })

/**
 * Extension for [Flux.ofType] providing a `ofType()` variant.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
inline fun  Flux<*>.ofType(): Flux = ofType(T::class.java)

/**
 * Extension for [Flux.onErrorResume] providing a [KClass] based variant.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Flux.onErrorResume(exceptionType: KClass, fallback: (E) -> Publisher): Flux =
        onErrorResume(exceptionType.java, { fallback(it) })

/**
 * Extension for [Flux.onErrorReturn] providing a [KClass] based variant.
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Flux.onErrorReturn(exceptionType: KClass, value: T): Flux =
        onErrorReturn(exceptionType.java, value)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy