Class Util

java.lang.Object
net.pincette.rs.Util

public class Util extends Object
Some utilities.
Since:
1.0
Author:
Werner Donné
  • Method Details

    • asList

      public static <T> List<T> asList(Flow.Publisher<T> publisher)
      Returns the published values as a list. This function will block when needed.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the publisher.
      Returns:
      The generated list.
      Since:
      1.6
    • asList

      public static <T> List<T> asList(Flow.Publisher<T> publisher, int initialCapacity)
    • asListAsync

      public static <T> CompletionStage<List<T>> asListAsync(Flow.Publisher<T> publisher)
      Returns the published values as a list.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the publisher.
      Returns:
      The generated list.
      Since:
      1.6
    • asListAsync

      public static <T> CompletionStage<List<T>> asListAsync(Flow.Publisher<T> publisher, int initialCapacity)
    • asValue

      public static <T> T asValue(Flow.Publisher<T> publisher)
      Returns the first value. This function will block when needed.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the publisher.
      Returns:
      The value.
      Since:
      1.7.1
    • asValueAsync

      public static <T> CompletionStage<T> asValueAsync(Flow.Publisher<T> publisher)
      Completes with the first emitted value or null if there was no such value.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the publisher.
      Returns:
      The value.
      Since:
      1.7.1
    • backpressureCheck

      public static <T> Flow.Processor<T,T> backpressureCheck()
      This is a debugging aid that throws an exception when more messages are sent than requested.
      Type Parameters:
      T - the value type.
      Returns:
      The pass through processor.
      Since:
      3.0.2
    • cancel

      public static <T> void cancel(Flow.Publisher<T> publisher)
      Subscribes to the publisher and cancels it immediately.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the given publisher.
      Since:
      3.4
    • completablePublisher

      public static <T> Flow.Publisher<T> completablePublisher(Supplier<CompletionStage<Flow.Publisher<T>>> publisher)
    • deflate

      public static Flow.Processor<ByteBuffer,ByteBuffer> deflate()
      Creates a processor that deflates a ByteBuffer stream.
      Returns:
      The processor.
      Since:
      3.0
    • deflate

      public static Flow.Processor<ByteBuffer,ByteBuffer> deflate(Deflater deflater)
      Creates a processor that deflates a ByteBuffer stream.
      Returns:
      The processor.
      Since:
      3.0
    • devNull

      public static <T> Flow.Subscriber<T> devNull()
    • discard

      public static <T> void discard(Flow.Publisher<T> publisher)
      Consumes the publisher without doing anything.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the given publisher.
      Since:
      3.0
    • divisibleBy

      public static Flow.Processor<ByteBuffer,ByteBuffer> divisibleBy(int n)
      Returns a processor that produces byte buffers the size of which is divisible by a certain number, where the last one may be smaller than that number.
      Since:
      3.0
    • duplicateFilter

      public static <T, U> Flow.Processor<T,T> duplicateFilter(Function<T,U> criterion, Duration window)
      Filters the elements leaving out duplicates according to the criterion over the time window.
      Type Parameters:
      T - the value type.
      U - the criterion type.
      Parameters:
      criterion - the given criterion.
      window - the time window to look at.
      Returns:
      The filtered stream.
      Since:
      3.0
    • empty

      public static <T> Flow.Publisher<T> empty()
      Returns a publisher that emits no values.
      Type Parameters:
      T - the value type.
      Returns:
      The empty publisher.
      Since:
      1.0
    • empty

      public static void empty(Flow.Publisher<Void> publisher)
      Waits until the empty publisher completes.
      Parameters:
      publisher - the publisher.
      Since:
      1.7.1
    • emptyAsync

      public static CompletionStage<Void> emptyAsync(Flow.Publisher<Void> publisher)
      Completes when the empty publisher completes.
      Parameters:
      publisher - the publisher.
      Returns:
      The empty completion stage.
      Since:
      1.7.1
    • first

      public static <T> CompletionStage<Optional<T>> first(Flow.Publisher<T> publisher)
      Returns the first element publisher emits if there is one.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the given publisher.
      Returns:
      The optional first element.
      Since:
      1.4
    • generate

      public static <T> Flow.Publisher<T> generate(Supplier<T> supplier)
      Returns a publisher that always emits values from supplier when asked for messages. When the supplier returns null the publisher completes.
      Type Parameters:
      T - the value type.
      Parameters:
      supplier - the generator function.
      Returns:
      The publisher.
      Since:
      1.6
    • generate

      public static <T> Flow.Publisher<T> generate(Supplier<T> initial, UnaryOperator<T> next)
      Returns a publisher that always emits values from initial and next when asked for messages. When the supplier returns null the publisher completes.
      Type Parameters:
      T - the value type.
      Parameters:
      initial - the generator function for the first value.
      next - the generator function for all the subsequent values. The function receives the last value that was emitted.
      Returns:
      The publisher.
      Since:
      1.6
    • gzip

      public static Flow.Processor<ByteBuffer,ByteBuffer> gzip()
      Creates a processor that compresses a ByteBuffer stream in GZIP format.
      Returns:
      The processor.
      Since:
      3.0
    • gunzip

      public static Flow.Processor<ByteBuffer,ByteBuffer> gunzip()
      Creates a processor that uncompresses a ByteBuffer stream in GZIP format.
      Returns:
      The processor.
      Since:
      3.0
    • inflate

      public static Flow.Processor<ByteBuffer,ByteBuffer> inflate()
      Creates a processor that inflates a ByteBuffer stream.
      Returns:
      The processor.
      Since:
      3.0
    • inflate

      public static Flow.Processor<ByteBuffer,ByteBuffer> inflate(Inflater inflater)
      Creates a processor that inflates a ByteBuffer stream.
      Returns:
      The processor.
      Since:
      3.0
    • iterate

      public static <T> Iterable<T> iterate(Flow.Publisher<T> publisher)
      Returns a blocking Iterable with a request size of 100.
      Type Parameters:
      T - the element type.
      Parameters:
      publisher - the publisher from which the elements are buffered.
      Returns:
      The iterable.
      Since:
      1.2
    • iterate

      public static <T> Iterable<T> iterate(Flow.Publisher<T> publisher, long requestSize)
      Returns a blocking Iterable.
      Type Parameters:
      T - the element type.
      Parameters:
      publisher - the publisher from which the elements are buffered.
      requestSize - the size of the requests the subscriber will issue to the publisher.
      Returns:
      The iterable.
      Since:
      1.2
    • join

      public static <T> void join(Flow.Publisher<T> publisher)
      Blocks until the publisher is done.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the given publisher.
      Since:
      1.5
    • last

      public static <T> CompletionStage<Optional<T>> last(Flow.Publisher<T> publisher)
      Returns the last element publisher emits if there is one.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the given publisher.
      Returns:
      The optional first element.
      Since:
      1.4
    • lines

      public static Flow.Processor<ByteBuffer,String> lines()
      Returns a processor that receives buffers and interprets the contents as a UTF-8 encoded string. It emits the individual lines in the string without the line separators.
      Since:
      3.0
    • onComplete

      public static <T> Flow.Subscriber<T> onComplete(RunnableWithException runnable)
      Returns a subscriber that reacts to the onComplete event.
      Type Parameters:
      T - the value type.
      Parameters:
      runnable - the given function.
      Returns:
      The subscriber.
      Since:
      3.0
    • onCompleteProcessor

      public static <T> Flow.Processor<T,T> onCompleteProcessor(RunnableWithException runnable)
      Returns a processor that reacts to the onComplete event. It is transparent to all events.
      Type Parameters:
      T - the value type.
      Parameters:
      runnable - the given function.
      Returns:
      The processor.
      Since:
      3.2.2
    • onError

      public static <T> Flow.Subscriber<T> onError(ConsumerWithException<Throwable> consumer)
      Returns a subscriber that reacts to the onError event.
      Type Parameters:
      T - the value type.
      Parameters:
      consumer - the given function.
      Returns:
      The subscriber.
      Since:
      3.0
    • onErrorProcessor

      public static <T> Flow.Processor<T,T> onErrorProcessor(ConsumerWithException<Throwable> consumer)
      Returns a processor that reacts to the onError event. It is transparent to all events.
      Type Parameters:
      T - the value type.
      Parameters:
      consumer - the given function.
      Returns:
      The processor.
      Since:
      3.2.2
    • onNext

      public static <T> Flow.Subscriber<T> onNext(ConsumerWithException<T> consumer)
      Returns a subscriber that reacts to the onNext event.
      Type Parameters:
      T - the value type.
      Parameters:
      consumer - the given function.
      Returns:
      The subscriber.
      Since:
      3.0
    • onNextProcessor

      public static <T> Flow.Processor<T,T> onNextProcessor(ConsumerWithException<T> consumer)
      Returns a processor that reacts to the onNext event. It is transparent to all events.
      Type Parameters:
      T - the value type.
      Parameters:
      consumer - the given function.
      Returns:
      The processor.
      Since:
      3.2.2
    • pull

      public static <T, R> Flow.Subscriber<T> pull(Flow.Processor<T,R> processor)
      Pulls data from a processor without doing anything with it. You can use this to get a pipeline started that ends with some side effect.
      Type Parameters:
      T - the incoming value type.
      R - theo outgoing value type.
      Parameters:
      processor - the given processor.
      Returns:
      The given processor as a subscriber.
      Since:
      3.0
    • retryPublisher

      public static <T> Flow.Publisher<T> retryPublisher(Supplier<Flow.Publisher<T>> publisher, Duration retryInterval)
      Returns a publisher that will recreate the original publisher when an exception occurs.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the function to create the original publisher.
      retryInterval - the time before a new attempt is made.
      Returns:
      The retrying publisher.
      Since:
      1.6
    • retryPublisher

      public static <T> Flow.Publisher<T> retryPublisher(Supplier<Flow.Publisher<T>> publisher, Duration retryInterval, Consumer<Throwable> onException)
      Returns a publisher that will recreate the original publisher when an exception occurs.
      Type Parameters:
      T - the value type.
      Parameters:
      publisher - the function to create the original publisher.
      retryInterval - the time before a new attempt is made.
      onException - the handler of the exception. It may be null.
      Returns:
      The retrying publisher.
      Since:
      1.6
    • subscribe

      public static <T, R> Flow.Publisher<R> subscribe(Flow.Publisher<T> publisher, Flow.Processor<T,R> processor)
      Subscribes processor to publisher and returns the processor as a publisher.
      Type Parameters:
      T - the type of the publisher.
      R - the type of the returned publisher.
      Parameters:
      publisher - the given publisher.
      processor - the given processor.
      Returns:
      The processor as a publisher.
      Since:
      1.0
    • subscribe

      public static <T, R> Flow.Subscriber<T> subscribe(Flow.Processor<T,R> processor, Flow.Subscriber<R> subscriber)
      Subscribes subscriber to processor and returns processor as a subscriber.
      Type Parameters:
      T - the input type of the processor.
      R - the output type of the processor.
      Parameters:
      processor - the given processor.
      subscriber - the given subscriber.
      Returns:
      The processor as a subscriber.
      Since:
      1.6
    • tap

      public static <T> Flow.Processor<T,T> tap(Flow.Subscriber<T> subscriber)
      Creates a processor where a subscriber can tap the data that goes through it.
      Type Parameters:
      T - the value type.
      Parameters:
      subscriber - the given subscriber.
      Returns:
      The processor.
      Since:
      3.0
    • transform

      public static <T, R> R transform(Flow.Processor<T,R> processor, T value)
    • transformAsync

      public static <T, R> CompletionStage<R> transformAsync(Flow.Processor<T,R> processor, T value)