java.lang.Object
net.pincette.rs.Util
Some utilities.
- Since:
- 1.0
- Author:
- Werner Donné
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T>
asList
(Flow.Publisher<T> publisher) Returns the published values as a list.static <T> List<T>
asList
(Flow.Publisher<T> publisher, int initialCapacity) static <T> CompletionStage<List<T>>
asListAsync
(Flow.Publisher<T> publisher) Returns the published values as a list.static <T> CompletionStage<List<T>>
asListAsync
(Flow.Publisher<T> publisher, int initialCapacity) static <T> T
asValue
(Flow.Publisher<T> publisher) Returns the first value.static <T> CompletionStage<T>
asValueAsync
(Flow.Publisher<T> publisher) Completes with the first emitted value ornull
if there was no such value.static <T> Flow.Processor<T,
T> This is a debugging aid that throws an exception when more messages are sent than requested.static <T> void
cancel
(Flow.Publisher<T> publisher) Subscribes to the publisher and cancels it immediately.static <T> Flow.Publisher<T>
completablePublisher
(Supplier<CompletionStage<Flow.Publisher<T>>> publisher) static Flow.Processor<ByteBuffer,
ByteBuffer> deflate()
Creates a processor that deflates aByteBuffer
stream.static Flow.Processor<ByteBuffer,
ByteBuffer> Creates a processor that deflates aByteBuffer
stream.static <T> Flow.Subscriber<T>
devNull()
static <T> void
discard
(Flow.Publisher<T> publisher) Consumes the publisher without doing anything.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.static <T,
U> Flow.Processor<T, T> duplicateFilter
(Function<T, U> criterion, Duration window) Filters the elements leaving out duplicates according to thecriterion
over the timewindow
.static <T> Flow.Publisher<T>
empty()
Returns a publisher that emits no values.static void
empty
(Flow.Publisher<Void> publisher) Waits until the empty publisher completes.static CompletionStage<Void>
emptyAsync
(Flow.Publisher<Void> publisher) Completes when the empty publisher completes.static <T> CompletionStage<Optional<T>>
first
(Flow.Publisher<T> publisher) Returns the first elementpublisher
emits if there is one.static <T> Flow.Publisher<T>
Returns a publisher that always emits values fromsupplier
when asked for messages.static <T> Flow.Publisher<T>
generate
(Supplier<T> initial, UnaryOperator<T> next) Returns a publisher that always emits values frominitial
andnext
when asked for messages.static Flow.Processor<ByteBuffer,
ByteBuffer> gunzip()
Creates a processor that uncompresses aByteBuffer
stream in GZIP format.static Flow.Processor<ByteBuffer,
ByteBuffer> gzip()
Creates a processor that compresses aByteBuffer
stream in GZIP format.static Flow.Processor<ByteBuffer,
ByteBuffer> inflate()
Creates a processor that inflates aByteBuffer
stream.static Flow.Processor<ByteBuffer,
ByteBuffer> Creates a processor that inflates aByteBuffer
stream.static <T> Iterable<T>
iterate
(Flow.Publisher<T> publisher) Returns a blockingIterable
with a request size of 100.static <T> Iterable<T>
iterate
(Flow.Publisher<T> publisher, long requestSize) Returns a blockingIterable
.static <T> void
join
(Flow.Publisher<T> publisher) Blocks until the publisher is done.static <T> CompletionStage<Optional<T>>
last
(Flow.Publisher<T> publisher) Returns the last elementpublisher
emits if there is one.static Flow.Processor<ByteBuffer,
String> lines()
Returns a processor that receives buffers and interprets the contents as a UTF-8 encoded string.static <T> Flow.Subscriber<T>
onComplete
(RunnableWithException runnable) Returns a subscriber that reacts to theonComplete
event.static <T> Flow.Processor<T,
T> onCompleteProcessor
(RunnableWithException runnable) Returns a processor that reacts to theonComplete
event.static <T> Flow.Subscriber<T>
onError
(ConsumerWithException<Throwable> consumer) Returns a subscriber that reacts to theonError
event.static <T> Flow.Processor<T,
T> onErrorProcessor
(ConsumerWithException<Throwable> consumer) Returns a processor that reacts to theonError
event.static <T> Flow.Subscriber<T>
onNext
(ConsumerWithException<T> consumer) Returns a subscriber that reacts to theonNext
event.static <T> Flow.Processor<T,
T> onNextProcessor
(ConsumerWithException<T> consumer) Returns a processor that reacts to theonNext
event.static <T,
R> Flow.Subscriber<T> pull
(Flow.Processor<T, R> processor) Pulls data from a processor without doing anything with it.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.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.static <T,
R> Flow.Subscriber<T> subscribe
(Flow.Processor<T, R> processor, Flow.Subscriber<R> subscriber) Subscribessubscriber
toprocessor
and returnsprocessor
as a subscriber.static <T,
R> Flow.Publisher<R> subscribe
(Flow.Publisher<T> publisher, Flow.Processor<T, R> processor) Subscribesprocessor
topublisher
and returns the processor as a publisher.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.static <T,
R> R transform
(Flow.Processor<T, R> processor, T value) static <T,
R> CompletionStage<R> transformAsync
(Flow.Processor<T, R> processor, T value)
-
Method Details
-
asList
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
-
asListAsync
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
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
Completes with the first emitted value ornull
if there was no such value.- Type Parameters:
T
- the value type.- Parameters:
publisher
- the publisher.- Returns:
- The value.
- Since:
- 1.7.1
-
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
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
Creates a processor that deflates aByteBuffer
stream.- Returns:
- The processor.
- Since:
- 3.0
-
deflate
Creates a processor that deflates aByteBuffer
stream.- Returns:
- The processor.
- Since:
- 3.0
-
devNull
-
discard
Consumes the publisher without doing anything.- Type Parameters:
T
- the value type.- Parameters:
publisher
- the given publisher.- Since:
- 3.0
-
divisibleBy
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
Filters the elements leaving out duplicates according to thecriterion
over the timewindow
.- 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
Returns a publisher that emits no values.- Type Parameters:
T
- the value type.- Returns:
- The empty publisher.
- Since:
- 1.0
-
empty
Waits until the empty publisher completes.- Parameters:
publisher
- the publisher.- Since:
- 1.7.1
-
emptyAsync
Completes when the empty publisher completes.- Parameters:
publisher
- the publisher.- Returns:
- The empty completion stage.
- Since:
- 1.7.1
-
first
Returns the first elementpublisher
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
Returns a publisher that always emits values fromsupplier
when asked for messages. When the supplier returnsnull
the publisher completes.- Type Parameters:
T
- the value type.- Parameters:
supplier
- the generator function.- Returns:
- The publisher.
- Since:
- 1.6
-
generate
Returns a publisher that always emits values frominitial
andnext
when asked for messages. When the supplier returnsnull
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
Creates a processor that compresses aByteBuffer
stream in GZIP format.- Returns:
- The processor.
- Since:
- 3.0
-
gunzip
Creates a processor that uncompresses aByteBuffer
stream in GZIP format.- Returns:
- The processor.
- Since:
- 3.0
-
inflate
Creates a processor that inflates aByteBuffer
stream.- Returns:
- The processor.
- Since:
- 3.0
-
inflate
Creates a processor that inflates aByteBuffer
stream.- Returns:
- The processor.
- Since:
- 3.0
-
iterate
Returns a blockingIterable
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
Returns a blockingIterable
.- 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
Blocks until the publisher is done.- Type Parameters:
T
- the value type.- Parameters:
publisher
- the given publisher.- Since:
- 1.5
-
last
Returns the last elementpublisher
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
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
Returns a subscriber that reacts to theonComplete
event.- Type Parameters:
T
- the value type.- Parameters:
runnable
- the given function.- Returns:
- The subscriber.
- Since:
- 3.0
-
onCompleteProcessor
Returns a processor that reacts to theonComplete
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
Returns a subscriber that reacts to theonError
event.- Type Parameters:
T
- the value type.- Parameters:
consumer
- the given function.- Returns:
- The subscriber.
- Since:
- 3.0
-
onErrorProcessor
Returns a processor that reacts to theonError
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
Returns a subscriber that reacts to theonNext
event.- Type Parameters:
T
- the value type.- Parameters:
consumer
- the given function.- Returns:
- The subscriber.
- Since:
- 3.0
-
onNextProcessor
Returns a processor that reacts to theonNext
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
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 benull
.- 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) Subscribesprocessor
topublisher
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) Subscribessubscriber
toprocessor
and returnsprocessor
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
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
-
transformAsync
-