Class ReactorFun
java.lang.Object
dmx.fun.reactor.ReactorFun
Idiomatic conversions between Project Reactor's
Mono and dmx-fun's
Option, Result, and Try.
The conversions come in two flavours:
toMono*variants stay reactive: they absorb the empty and error signals into the value channel so the result is aMono<Result<...>>/Mono<Try<...>>that never errors for a modeled failure. Prefer these inside reactive pipelines.toTry/toResult/toOptionare blocking extractors: they subscribe and wait for the terminal signal, returning the dmx-fun value directly. They callMono.block()and therefore must not be used on a non-blocking thread (event loop). They exist for bridging into non-reactive code and tests.toMonogoes the other way, turning anOption,Result, orTryback into aMono.
Empty-Mono policy: a Mono that completes without a value is treated as
a missing value — Option.none() for the Option conversions, and a failure
carrying NoSuchElementException for the Try and Result conversions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> reactor.core.publisher.Mono<V> static <V, E extends Throwable>
reactor.core.publisher.Mono<V> static <V,E> reactor.core.publisher.Mono <V> static <V> reactor.core.publisher.Mono<V> Turns aTryinto aMono:SuccessbecomesMono.just(Object)andFailurebecomesMono.error(Throwable)carrying the cause.static <V> reactor.core.publisher.Mono<Option<V>> toMonoOption(reactor.core.publisher.Mono<V> mono) Converts aMono<V>into aMono<Option<V>>: a value becomesOption.some(Object)and an empty completion becomesOption.none().toMonoResult(reactor.core.publisher.Mono<V> mono) Converts aMono<V>into aMono<Result<V, Throwable>>that never errors: a value becomesResult.Ok, an error signal becomesResult.Errcarrying the cause, and an empty completion becomes anErrcarryingNoSuchElementException.static <V,E> reactor.core.publisher.Mono <Result<V, E>> toMonoResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper) Converts aMono<V>into aMono<Result<V, E>>, mapping any error (and an empty completion, viaNoSuchElementException) througherrorMapperto the typed error channel.static <V,E> reactor.core.publisher.Mono <Result<V, E>> toMonoResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper, Supplier<E> onEmpty) Converts aMono<V>into aMono<Result<V, E>>with full control over both channels: errors are mapped througherrorMapper, and an empty completion produces the error fromonEmptyinstead of the defaultNoSuchElementException.static <V> reactor.core.publisher.Mono<Try<V>> toMonoTry(reactor.core.publisher.Mono<V> mono) Converts aMono<V>into aMono<Try<V>>that never errors: a value becomesTry.Success, an error signal becomesTry.Failurecarrying the cause, and an empty completion becomes aFailurecarryingNoSuchElementException.static <V> Option<V> toOption(reactor.core.publisher.Mono<V> mono) Blocking variant oftoMonoOption(Mono): subscribes and waits, returning theOptiondirectly.toResult(reactor.core.publisher.Mono<V> mono) Blocking variant oftoMonoResult(Mono): subscribes and waits, returning theResultdirectly.static <V,E> Result <V, E> Blocking variant oftoMonoResult(Mono, Function): subscribes and waits, returning the typedResultdirectly.static <V,E> Result <V, E> toResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper, Supplier<E> onEmpty) Blocking variant oftoMonoResult(Mono, Function, Supplier): subscribes and waits, returning the typedResultdirectly.static <V> Try<V> toTry(reactor.core.publisher.Mono<V> mono) Blocking variant oftoMonoTry(Mono): subscribes and waits, returning theTrydirectly.
-
Method Details
-
toMonoTry
public static <V> reactor.core.publisher.Mono<Try<V>> toMonoTry(reactor.core.publisher.Mono<V> mono) Converts aMono<V>into aMono<Try<V>>that never errors: a value becomesTry.Success, an error signal becomesTry.Failurecarrying the cause, and an empty completion becomes aFailurecarryingNoSuchElementException.- Type Parameters:
V- the value type- Parameters:
mono- the source publisher- Returns:
- a
Monothat always emits aTry
-
toMonoResult
public static <V> reactor.core.publisher.Mono<Result<V, Throwable>> toMonoResult(reactor.core.publisher.Mono<V> mono) Converts aMono<V>into aMono<Result<V, Throwable>>that never errors: a value becomesResult.Ok, an error signal becomesResult.Errcarrying the cause, and an empty completion becomes anErrcarryingNoSuchElementException. -
toMonoResult
public static <V,E> reactor.core.publisher.Mono<Result<V,E>> toMonoResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper) Converts aMono<V>into aMono<Result<V, E>>, mapping any error (and an empty completion, viaNoSuchElementException) througherrorMapperto the typed error channel.- Type Parameters:
V- the value typeE- the typed error type- Parameters:
mono- the source publishererrorMapper- maps aThrowable(the cause, or aNoSuchElementExceptionon empty) to the error type- Returns:
- a
Monothat always emits aResult
-
toMonoResult
public static <V,E> reactor.core.publisher.Mono<Result<V,E>> toMonoResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper, Supplier<E> onEmpty) Converts aMono<V>into aMono<Result<V, E>>with full control over both channels: errors are mapped througherrorMapper, and an empty completion produces the error fromonEmptyinstead of the defaultNoSuchElementException. -
toMonoOption
public static <V> reactor.core.publisher.Mono<Option<V>> toMonoOption(reactor.core.publisher.Mono<V> mono) Converts aMono<V>into aMono<Option<V>>: a value becomesOption.some(Object)and an empty completion becomesOption.none(). An error signal is not absorbed — it propagates as a Reactor error, sinceOptionhas no error channel.- Type Parameters:
V- the value type- Parameters:
mono- the source publisher- Returns:
- a
Monothat emits anOption, or errors if the source does
-
toTry
Blocking variant oftoMonoTry(Mono): subscribes and waits, returning theTrydirectly. Blocks the calling thread — do not call on a non-blocking (event-loop) thread.- Type Parameters:
V- the value type- Parameters:
mono- the source publisher- Returns:
- the resulting
Try
-
toResult
Blocking variant oftoMonoResult(Mono): subscribes and waits, returning theResultdirectly. Blocks the calling thread. -
toResult
public static <V,E> Result<V,E> toResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper) Blocking variant oftoMonoResult(Mono, Function): subscribes and waits, returning the typedResultdirectly. Blocks the calling thread. -
toResult
-
toOption
Blocking variant oftoMonoOption(Mono): subscribes and waits, returning theOptiondirectly. Blocks the calling thread, and a Reactor error signal is rethrown byMono.block().- Type Parameters:
V- the value type- Parameters:
mono- the source publisher- Returns:
- the resulting
Option
-
toMono
-
toMono
Turns aTryinto aMono:SuccessbecomesMono.just(Object)andFailurebecomesMono.error(Throwable)carrying the cause.- Type Parameters:
V- the value type- Parameters:
aTry- the try to convert- Returns:
- a
Monothat emits the value or errors with the cause
-
toMono
Turns aResultwhose error channel is already aThrowableinto aMono:OkbecomesMono.just(Object)andErrbecomesMono.error(Throwable)carrying the error directly. Use thetoMono(Result, Function)overload when the error is a domain type that must be mapped to aThrowable.- Type Parameters:
V- the value typeE- the error type, which must be aThrowable- Parameters:
result- the result to convert- Returns:
- a
Monothat emits the value or errors with the error
-
toMono
public static <V,E> reactor.core.publisher.Mono<V> toMono(Result<V, E> result, Function<? super E, ? extends Throwable> errorMapper) Turns aResultinto aMono:OkbecomesMono.just(Object)andErrbecomesMono.error(Throwable)with the error mapped to aThrowablebyerrorMapper.- Type Parameters:
V- the value typeE- the typed error type- Parameters:
result- the result to converterrorMapper- maps the typed error to aThrowableerror signal- Returns:
- a
Monothat emits the value or errors with the mapped throwable
-