Class ReactorFun

java.lang.Object
dmx.fun.reactor.ReactorFun

@NullMarked public final class ReactorFun extends Object
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 a Mono<Result<...>> / Mono<Try<...>> that never errors for a modeled failure. Prefer these inside reactive pipelines.
  • toTry / toResult / toOption are blocking extractors: they subscribe and wait for the terminal signal, returning the dmx-fun value directly. They call Mono.block() and therefore must not be used on a non-blocking thread (event loop). They exist for bridging into non-reactive code and tests.
  • toMono goes the other way, turning an Option, Result, or Try back into a Mono.

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 Details

    • toMonoTry

      public static <V> reactor.core.publisher.Mono<Try<V>> toMonoTry(reactor.core.publisher.Mono<V> mono)
      Converts a Mono<V> into a Mono<Try<V>> that never errors: a value becomes Try.Success, an error signal becomes Try.Failure carrying the cause, and an empty completion becomes a Failure carrying NoSuchElementException.
      Type Parameters:
      V - the value type
      Parameters:
      mono - the source publisher
      Returns:
      a Mono that always emits a Try
    • toMonoResult

      public static <V> reactor.core.publisher.Mono<Result<V, Throwable>> toMonoResult(reactor.core.publisher.Mono<V> mono)
      Converts a Mono<V> into a Mono<Result<V, Throwable>> that never errors: a value becomes Result.Ok, an error signal becomes Result.Err carrying the cause, and an empty completion becomes an Err carrying NoSuchElementException.
      Type Parameters:
      V - the value type
      Parameters:
      mono - the source publisher
      Returns:
      a Mono that always emits a Result with the failure on the error channel as a Throwable
    • 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 a Mono<V> into a Mono<Result<V, E>>, mapping any error (and an empty completion, via NoSuchElementException) through errorMapper to the typed error channel.
      Type Parameters:
      V - the value type
      E - the typed error type
      Parameters:
      mono - the source publisher
      errorMapper - maps a Throwable (the cause, or a NoSuchElementException on empty) to the error type
      Returns:
      a Mono that always emits a Result
    • 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 a Mono<V> into a Mono<Result<V, E>> with full control over both channels: errors are mapped through errorMapper, and an empty completion produces the error from onEmpty instead of the default NoSuchElementException.
      Type Parameters:
      V - the value type
      E - the typed error type
      Parameters:
      mono - the source publisher
      errorMapper - maps a Throwable error signal to the error type
      onEmpty - supplies the error value used when the source completes empty
      Returns:
      a Mono that always emits a Result
    • toMonoOption

      public static <V> reactor.core.publisher.Mono<Option<V>> toMonoOption(reactor.core.publisher.Mono<V> mono)
      Converts a Mono<V> into a Mono<Option<V>>: a value becomes Option.some(Object) and an empty completion becomes Option.none(). An error signal is not absorbed — it propagates as a Reactor error, since Option has no error channel.
      Type Parameters:
      V - the value type
      Parameters:
      mono - the source publisher
      Returns:
      a Mono that emits an Option, or errors if the source does
    • toTry

      public static <V> Try<V> toTry(reactor.core.publisher.Mono<V> mono)
      Blocking variant of toMonoTry(Mono): subscribes and waits, returning the Try directly. 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

      public static <V> Result<V, Throwable> toResult(reactor.core.publisher.Mono<V> mono)
      Blocking variant of toMonoResult(Mono): subscribes and waits, returning the Result directly. Blocks the calling thread.
      Type Parameters:
      V - the value type
      Parameters:
      mono - the source publisher
      Returns:
      the resulting Result with a Throwable error channel
    • toResult

      public static <V,E> Result<V,E> toResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper)
      Blocking variant of toMonoResult(Mono, Function): subscribes and waits, returning the typed Result directly. Blocks the calling thread.
      Type Parameters:
      V - the value type
      E - the typed error type
      Parameters:
      mono - the source publisher
      errorMapper - maps a Throwable to the error type
      Returns:
      the resulting Result
    • toResult

      public static <V,E> Result<V,E> toResult(reactor.core.publisher.Mono<V> mono, Function<? super Throwable, E> errorMapper, Supplier<E> onEmpty)
      Blocking variant of toMonoResult(Mono, Function, Supplier): subscribes and waits, returning the typed Result directly. Blocks the calling thread.
      Type Parameters:
      V - the value type
      E - the typed error type
      Parameters:
      mono - the source publisher
      errorMapper - maps a Throwable error signal to the error type
      onEmpty - supplies the error value used when the source completes empty
      Returns:
      the resulting Result
    • toOption

      public static <V> Option<V> toOption(reactor.core.publisher.Mono<V> mono)
      Blocking variant of toMonoOption(Mono): subscribes and waits, returning the Option directly. Blocks the calling thread, and a Reactor error signal is rethrown by Mono.block().
      Type Parameters:
      V - the value type
      Parameters:
      mono - the source publisher
      Returns:
      the resulting Option
    • toMono

      public static <V> reactor.core.publisher.Mono<V> toMono(Option<V> option)
      Turns an Option into a Mono: Some becomes Mono.just(Object) and None becomes Mono.empty().
      Type Parameters:
      V - the value type
      Parameters:
      option - the option to convert
      Returns:
      a Mono that emits the value or completes empty
    • toMono

      public static <V> reactor.core.publisher.Mono<V> toMono(Try<V> aTry)
      Turns a Try into a Mono: Success becomes Mono.just(Object) and Failure becomes Mono.error(Throwable) carrying the cause.
      Type Parameters:
      V - the value type
      Parameters:
      aTry - the try to convert
      Returns:
      a Mono that emits the value or errors with the cause
    • toMono

      public static <V, E extends Throwable> reactor.core.publisher.Mono<V> toMono(Result<V,E> result)
      Turns a Result whose error channel is already a Throwable into a Mono: Ok becomes Mono.just(Object) and Err becomes Mono.error(Throwable) carrying the error directly. Use the toMono(Result, Function) overload when the error is a domain type that must be mapped to a Throwable.
      Type Parameters:
      V - the value type
      E - the error type, which must be a Throwable
      Parameters:
      result - the result to convert
      Returns:
      a Mono that 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 a Result into a Mono: Ok becomes Mono.just(Object) and Err becomes Mono.error(Throwable) with the error mapped to a Throwable by errorMapper.
      Type Parameters:
      V - the value type
      E - the typed error type
      Parameters:
      result - the result to convert
      errorMapper - maps the typed error to a Throwable error signal
      Returns:
      a Mono that emits the value or errors with the mapped throwable