Class Publishers


  • @Internal
    public class Publishers
    extends java.lang.Object
    Utilities for working with raw Publisher instances. Designed for internal use by Micronaut and not as a replacement for a reactive library such as RxJava, Reactor, Akka etc.
    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      Publishers()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T convertPublisher​(java.lang.Object object, java.lang.Class<T> publisherType)
      Attempts to convert the publisher to the given type.
      static <T> org.reactivestreams.Publisher<T> empty()
      A Publisher that completes without emitting any items.
      static <T> org.reactivestreams.Publisher<T> fromCompletableFuture​(java.util.concurrent.CompletableFuture<T> future)
      Build a Publisher from a CompletableFuture.
      static <T> org.reactivestreams.Publisher<T> fromCompletableFuture​(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> futureSupplier)
      Build a Publisher from a CompletableFuture.
      static java.util.List<java.lang.Class<?>> getKnownCompletableTypes()  
      static java.util.List<java.lang.Class<?>> getKnownReactiveTypes()  
      static java.util.List<java.lang.Class<?>> getKnownSingleTypes()  
      static boolean isCompletable​(java.lang.Class<?> type)
      Does the given reactive type emit a single result.
      static boolean isConvertibleToPublisher​(java.lang.Class<?> type)
      Is the given type a Publisher or convertible to a publisher.
      static boolean isConvertibleToPublisher​(java.lang.Object object)
      Is the given object a Publisher or convertible to a publisher.
      static boolean isSingle​(java.lang.Class<?> type)
      Does the given reactive type emit a single result.
      static <T> org.reactivestreams.Publisher<T> just​(java.lang.Throwable error)
      A Publisher that emits a fixed single value.
      static <T> org.reactivestreams.Publisher<T> just​(T value)
      A Publisher that emits a fixed single value.
      static <T,​R>
      org.reactivestreams.Publisher<R>
      map​(org.reactivestreams.Publisher<T> publisher, java.util.function.Function<T,​R> mapper)
      Map the result from a publisher using the given mapper.
      static <T,​R>
      org.reactivestreams.Publisher<R>
      mapOrSupplyEmpty​(org.reactivestreams.Publisher<T> publisher, Publishers.MapOrSupplyEmpty<T,​R> mapOrSupplyEmpty)
      Map the result from a publisher using the given mapper or supply empty value.
      static <T> org.reactivestreams.Publisher<T> onComplete​(org.reactivestreams.Publisher<T> publisher, java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Void>> future)
      Allow executing logic on completion of a Publisher.
      static void registerReactiveCompletable​(java.lang.Class<?> type)
      Registers an additional reactive completable type.
      static void registerReactiveSingle​(java.lang.Class<?> type)
      Registers an additional reactive single type.
      static void registerReactiveType​(java.lang.Class<?> type)
      Registers an additional reactive type.
      static <T> org.reactivestreams.Publisher<T> then​(org.reactivestreams.Publisher<T> publisher, java.util.function.Consumer<T> consumer)
      Map the result from a publisher using the given mapper.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Publishers

        public Publishers()
    • Method Detail

      • registerReactiveType

        public static void registerReactiveType​(java.lang.Class<?> type)
        Registers an additional reactive type. Should be called during application static initialization.
        Parameters:
        type - The type
        Since:
        2.0
      • registerReactiveSingle

        public static void registerReactiveSingle​(java.lang.Class<?> type)
        Registers an additional reactive single type. Should be called during application static initialization.
        Parameters:
        type - The type
        Since:
        2.0
      • registerReactiveCompletable

        public static void registerReactiveCompletable​(java.lang.Class<?> type)
        Registers an additional reactive completable type. Should be called during application static initialization.
        Parameters:
        type - The type
        Since:
        2.0
      • getKnownReactiveTypes

        public static java.util.List<java.lang.Class<?>> getKnownReactiveTypes()
        Returns:
        A list of known reactive types.
      • getKnownSingleTypes

        public static java.util.List<java.lang.Class<?>> getKnownSingleTypes()
        Returns:
        A list of known single types.
      • getKnownCompletableTypes

        public static java.util.List<java.lang.Class<?>> getKnownCompletableTypes()
        Returns:
        A list of known single types.
      • fromCompletableFuture

        public static <T> org.reactivestreams.Publisher<T> fromCompletableFuture​(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> futureSupplier)
        Build a Publisher from a CompletableFuture.
        Type Parameters:
        T - The type of the publisher
        Parameters:
        futureSupplier - The supplier of the CompletableFuture
        Returns:
        The Publisher
      • fromCompletableFuture

        public static <T> org.reactivestreams.Publisher<T> fromCompletableFuture​(java.util.concurrent.CompletableFuture<T> future)
        Build a Publisher from a CompletableFuture.
        Type Parameters:
        T - The type of the publisher
        Parameters:
        future - The CompletableFuture
        Returns:
        The Publisher
      • just

        public static <T> org.reactivestreams.Publisher<T> just​(T value)
        A Publisher that emits a fixed single value.
        Type Parameters:
        T - The value type
        Parameters:
        value - The value to emit
        Returns:
        The Publisher
      • just

        public static <T> org.reactivestreams.Publisher<T> just​(java.lang.Throwable error)
        A Publisher that emits a fixed single value.
        Type Parameters:
        T - The value type
        Parameters:
        error - The error to emit
        Returns:
        The Publisher
      • empty

        public static <T> org.reactivestreams.Publisher<T> empty()
        A Publisher that completes without emitting any items.
        Type Parameters:
        T - The value type
        Returns:
        The Publisher
        Since:
        2.0.0
      • map

        public static <T,​R> org.reactivestreams.Publisher<R> map​(org.reactivestreams.Publisher<T> publisher,
                                                                       java.util.function.Function<T,​R> mapper)
        Map the result from a publisher using the given mapper.
        Type Parameters:
        T - The generic type
        R - The result type
        Parameters:
        publisher - The publisher
        mapper - The mapper
        Returns:
        The mapped publisher
      • mapOrSupplyEmpty

        public static <T,​R> org.reactivestreams.Publisher<R> mapOrSupplyEmpty​(org.reactivestreams.Publisher<T> publisher,
                                                                                    Publishers.MapOrSupplyEmpty<T,​R> mapOrSupplyEmpty)
        Map the result from a publisher using the given mapper or supply empty value.
        Type Parameters:
        T - The generic type
        R - The result type
        Parameters:
        publisher - The publisher
        mapOrSupplyEmpty - The mapOrSupplyEmpty
        Returns:
        The mapped publisher
        Since:
        2.5.0
      • then

        public static <T> org.reactivestreams.Publisher<T> then​(org.reactivestreams.Publisher<T> publisher,
                                                                java.util.function.Consumer<T> consumer)
        Map the result from a publisher using the given mapper.
        Type Parameters:
        T - The generic type
        Parameters:
        publisher - The publisher
        consumer - The mapper
        Returns:
        The mapped publisher
      • onComplete

        public static <T> org.reactivestreams.Publisher<T> onComplete​(org.reactivestreams.Publisher<T> publisher,
                                                                      java.util.function.Supplier<java.util.concurrent.CompletableFuture<java.lang.Void>> future)
        Allow executing logic on completion of a Publisher.
        Type Parameters:
        T - The generic type
        Parameters:
        publisher - The publisher
        future - The runnable
        Returns:
        The mapped publisher
      • isConvertibleToPublisher

        public static boolean isConvertibleToPublisher​(java.lang.Class<?> type)
        Is the given type a Publisher or convertible to a publisher.
        Parameters:
        type - The type to check
        Returns:
        True if it is
      • isConvertibleToPublisher

        public static boolean isConvertibleToPublisher​(java.lang.Object object)
        Is the given object a Publisher or convertible to a publisher.
        Parameters:
        object - The object
        Returns:
        True if it is
      • convertPublisher

        public static <T> T convertPublisher​(java.lang.Object object,
                                             java.lang.Class<T> publisherType)
        Attempts to convert the publisher to the given type.
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to convert
        publisherType - The publisher type
        Returns:
        The Resulting in publisher
      • isSingle

        public static boolean isSingle​(java.lang.Class<?> type)
        Does the given reactive type emit a single result.
        Parameters:
        type - The type
        Returns:
        True it does
      • isCompletable

        public static boolean isCompletable​(java.lang.Class<?> type)
        Does the given reactive type emit a single result.
        Parameters:
        type - The type
        Returns:
        True it does