Module mutiny.zero
Package mutiny.zero

Interface ZeroPublisher


public interface ZeroPublisher
Factory methods to simplify the creation of reactive streams compliant Flow.Publisher.

There are convenience methods for creating Flow.Publisher from in-memory data.

The general-purpose abstraction is to use a Tube and the create(TubeConfiguration, Consumer) factory method.

  • Method Details

    • fromItems

      @SafeVarargs static <T> Flow.Publisher<T> fromItems(T... items)
      Create a Flow.Publisher from existing items.
      Type Parameters:
      T - the items type
      Parameters:
      items - the existing items, cannot be a null array
      Returns:
      a new Flow.Publisher
    • fromIterable

      static <T> Flow.Publisher<T> fromIterable(Iterable<T> iterable)
      Create a Flow.Publisher from an iterable object.

      Note that this assumes an in-memory, non-blocking Iterator. Do not try to force an iterator as a way to bridge an API with Flow.Publisher as it does not behave like an in-memory data structure.

      Type Parameters:
      T - the items type
      Parameters:
      iterable - the iterable object, cannot be null
      Returns:
      a nes Flow.Publisher
    • fromStream

      static <T> Flow.Publisher<T> fromStream(Supplier<Stream<T>> supplier)
      Create a Flow.Publisher from a Stream.

      Note that this assumes an in-memory, non-blocking data structure, just like fromIterable(Iterable). Also note that a Stream can only be traversed once, hence the use of a supplier because multiple subscriptions would fail.

      Type Parameters:
      T - the items type
      Parameters:
      supplier - the stream supplier, cannot be null
      Returns:
      a new Flow.Publisher
    • fromGenerator

      static <S, T> Flow.Publisher<T> fromGenerator(Supplier<S> stateSupplier, Function<S,Iterator<T>> generator)
      Create a Flow.Publisher from a generator over some state.

      Note that this assumes an in-memory, non-blocking data structure, just like fromIterable(Iterable).

      Type Parameters:
      S - the initial state type
      T - the items type
      Parameters:
      stateSupplier - the initial state supplier, cannot be null but can supply null
      generator - a generator function over the initial state and an iterator, cannot be null, cannot yield null
      Returns:
      a new Flow.Publisher
    • fromCompletionStage

      static <T> Flow.Publisher<T> fromCompletionStage(Supplier<CompletionStage<T>> completionStageSupplier)
      Type Parameters:
      T - the item type
      Parameters:
      completionStageSupplier - the completion stage supplier, cannot be null, cannot yield null
      Returns:
      a new Flow.Publisher
    • toCompletionStage

      static <T> CompletionStage<Optional<T>> toCompletionStage(Flow.Publisher<T> publisher)
      Create a CompletionStage from a Flow.Publisher.

      The Flow.Publisher is requested exactly 1 element and the subscription is cancelled after it has been received.

      Type Parameters:
      T - the item type
      Parameters:
      publisher - the publisher, cannot be null
      Returns:
      a new CompletionStage
    • fromFailure

      static <T> Flow.Publisher<T> fromFailure(Throwable failure)
      Create a Flow.Publisher from a known failure.
      Type Parameters:
      T - the items type
      Parameters:
      failure - the failure, cannot be null
      Returns:
      a new Flow.Publisher
    • empty

      static <T> Flow.Publisher<T> empty()
      Create an empty Flow.Publisher that completes upon subscription without ever sending any item.
      Type Parameters:
      T - the items type
      Returns:
      a new Flow.Publisher
    • create

      static <T> Flow.Publisher<T> create(TubeConfiguration configuration, Consumer<Tube<T>> tubeConsumer)
      Create a new Flow.Publisher with the general-purpose Tube API.
      Type Parameters:
      T - the items type
      Parameters:
      configuration - the tube configuration
      tubeConsumer - the tube consumer, cannot be null
      Returns:
      a new Flow.Publisher