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

reactor.core.publisher.MonoExtensions.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.concurrent.Callable
import java.util.concurrent.CompletableFuture
import kotlin.reflect.KClass

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

/**
 * Extension for transforming an object to a [Mono].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  T.toMono(): Mono = Mono.just(this)

/**
 * Extension for transforming an [CompletableFuture] to a [Mono].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  CompletableFuture.toMono(): Mono = Mono.fromFuture(this)

/**
 * Extension for transforming an [Callable] to a [Mono].
 *
 * @author Sebastien Deleuze
 * @since 3.1
 */
fun  Callable.toMono(): Mono = Mono.fromCallable(this::call)

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

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

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

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

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy