Class ReactorFlux

java.lang.Object
dmx.fun.reactor.ReactorFlux

@NullMarked public final class ReactorFlux extends Object
Idiomatic conversions between Project Reactor's Flux and dmx-fun's Option, Result, Try, and NonEmptyList.

Where ReactorFun handles a single value (Mono), this facade handles streams (Flux):

  • sequence / collectResult / collectValidated fold a stream of outcomes into a single Mono<Result<List<…>, …>> — the reactive equivalent of Result.sequence(Iterable) — choosing fail-fast or error-accumulating semantics.
  • flattenOption drops absent elements from a Flux<Option<T>>.
  • toFlux emits a dmx-fun container or collection as a Flux.

Backpressure and cancellation are preserved by the underlying Reactor operators.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> reactor.core.publisher.Mono<Result<List<T>, Throwable>>
    collectResult(reactor.core.publisher.Flux<T> source)
    Collects every element of a Flux<T> into Ok(list) (empty stream → Ok([])), turning a Reactor error signal into Err(cause).
    static <T,E> reactor.core.publisher.Mono<Result<List<T>,E>>
    collectResult(reactor.core.publisher.Flux<T> source, Function<? super Throwable, E> errorMapper)
    Collects every element of a Flux<T> into Ok(list), mapping a Reactor error signal through errorMapper to the typed error channel.
    static <T,E> reactor.core.publisher.Mono<Validated<NonEmptyList<E>, List<T>>>
    collectValidated(reactor.core.publisher.Flux<Result<T,E>> results)
    Folds a Flux<Result<T, E>> into a Mono<Validated<NonEmptyList<E>, List<T>>>, accumulating: every Err is gathered into the invalid channel (no short-circuit), and an all-Ok stream (including empty) is Valid.
    static <T> reactor.core.publisher.Flux<T>
    flattenOption(reactor.core.publisher.Flux<Option<T>> source)
    Drops the absent elements of a Flux<Option<T>>, keeping the values of the Some elements in source order.
    static <T,E> reactor.core.publisher.Mono<Result<List<T>,E>>
    sequence(reactor.core.publisher.Flux<Result<T,E>> results)
    Folds a Flux<Result<T, E>> into a single Mono<Result<List<T>, E>>, fail-fast: the first Err short-circuits and cancels the upstream, and an empty stream yields Ok([]).
    static <T> reactor.core.publisher.Flux<T>
    Emits each element of a NonEmptyList as a Flux (1..N elements).
    static <T> reactor.core.publisher.Flux<T>
    toFlux(Option<T> option)
    Emits an Option as a Flux: Some emits one element, None completes empty.
    static <T, E extends Throwable>
    reactor.core.publisher.Flux<T>
    toFlux(Result<T,E> result)
    Emits a Result whose error channel is already a Throwable as a Flux: Ok emits one element, Err errors with the error.
    static <T,E> reactor.core.publisher.Flux<T>
    toFlux(Result<T,E> result, Function<? super E, ? extends Throwable> errorMapper)
    Emits a Result as a Flux: Ok emits one element, Err errors with the error mapped to a Throwable by errorMapper.
    static <T> reactor.core.publisher.Flux<T>
    toFlux(Try<T> aTry)
    Emits a Try as a Flux: Success emits one element, Failure errors with the cause.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • sequence

      public static <T,E> reactor.core.publisher.Mono<Result<List<T>,E>> sequence(reactor.core.publisher.Flux<Result<T,E>> results)
      Folds a Flux<Result<T, E>> into a single Mono<Result<List<T>, E>>, fail-fast: the first Err short-circuits and cancels the upstream, and an empty stream yields Ok([]). A Reactor error signal from the source is not absorbed — it propagates as a Reactor error.
      Type Parameters:
      T - the value type
      E - the error type
      Parameters:
      results - the stream of results
      Returns:
      a Mono emitting the sequenced Result
    • collectResult

      public static <T> reactor.core.publisher.Mono<Result<List<T>, Throwable>> collectResult(reactor.core.publisher.Flux<T> source)
      Collects every element of a Flux<T> into Ok(list) (empty stream → Ok([])), turning a Reactor error signal into Err(cause).
      Type Parameters:
      T - the value type
      Parameters:
      source - the source stream
      Returns:
      a Mono emitting the collected Result
    • collectResult

      public static <T,E> reactor.core.publisher.Mono<Result<List<T>,E>> collectResult(reactor.core.publisher.Flux<T> source, Function<? super Throwable, E> errorMapper)
      Collects every element of a Flux<T> into Ok(list), mapping a Reactor error signal through errorMapper to the typed error channel.
      Type Parameters:
      T - the value type
      E - the typed error type
      Parameters:
      source - the source stream
      errorMapper - maps a Throwable error signal to the error type
      Returns:
      a Mono emitting the collected Result
    • collectValidated

      public static <T,E> reactor.core.publisher.Mono<Validated<NonEmptyList<E>, List<T>>> collectValidated(reactor.core.publisher.Flux<Result<T,E>> results)
      Folds a Flux<Result<T, E>> into a Mono<Validated<NonEmptyList<E>, List<T>>>, accumulating: every Err is gathered into the invalid channel (no short-circuit), and an all-Ok stream (including empty) is Valid.
      Type Parameters:
      T - the value type
      E - the error type
      Parameters:
      results - the stream of results
      Returns:
      a Mono emitting the accumulated Validated
    • flattenOption

      public static <T> reactor.core.publisher.Flux<T> flattenOption(reactor.core.publisher.Flux<Option<T>> source)
      Drops the absent elements of a Flux<Option<T>>, keeping the values of the Some elements in source order.
      Type Parameters:
      T - the value type
      Parameters:
      source - the stream of options
      Returns:
      a Flux of the present values
    • toFlux

      public static <T> reactor.core.publisher.Flux<T> toFlux(NonEmptyList<T> list)
      Emits each element of a NonEmptyList as a Flux (1..N elements).
      Type Parameters:
      T - the element type
      Parameters:
      list - the non-empty list
      Returns:
      a Flux over the list
    • toFlux

      public static <T> reactor.core.publisher.Flux<T> toFlux(Option<T> option)
      Emits an Option as a Flux: Some emits one element, None completes empty.
      Type Parameters:
      T - the value type
      Parameters:
      option - the option
      Returns:
      a Flux of zero or one element
    • toFlux

      public static <T> reactor.core.publisher.Flux<T> toFlux(Try<T> aTry)
      Emits a Try as a Flux: Success emits one element, Failure errors with the cause.
      Type Parameters:
      T - the value type
      Parameters:
      aTry - the try
      Returns:
      a Flux of one element or an error
    • toFlux

      public static <T, E extends Throwable> reactor.core.publisher.Flux<T> toFlux(Result<T,E> result)
      Emits a Result whose error channel is already a Throwable as a Flux: Ok emits one element, Err errors with the error.
      Type Parameters:
      T - the value type
      E - the error type, which must be a Throwable
      Parameters:
      result - the result
      Returns:
      a Flux of one element or an error
    • toFlux

      public static <T,E> reactor.core.publisher.Flux<T> toFlux(Result<T,E> result, Function<? super E, ? extends Throwable> errorMapper)
      Emits a Result as a Flux: Ok emits one element, Err errors with the error mapped to a Throwable by errorMapper.
      Type Parameters:
      T - the value type
      E - the error type
      Parameters:
      result - the result
      errorMapper - maps the typed error to a Throwable error signal
      Returns:
      a Flux of one element or an error