Package ratpack.exec.stream
Interface TransformablePublisher<T>
-
- Type Parameters:
T
- the type of item emitted by this publisher
- All Superinterfaces:
Publisher<T>
public interface TransformablePublisher<T> extends Publisher<T>
A wrapper over aPublisher
that makes it more convenient to chain transformations of different kinds.Note that this type implements the publisher interface, so behaves just like the publisher that it is wrapping with respect to the
Publisher.subscribe(Subscriber)
method.
-
-
Method Summary
All Methods Instance Methods Default Methods Modifier and Type Method Description default TransformablePublisher<T>
batch(int batchSize, Action<? super T> disposer)
default TransformablePublisher<T>
bindExec()
default TransformablePublisher<T>
bindExec(Action<? super T> disposer)
default TransformablePublisher<T>
buffer()
default TransformablePublisher<T>
filter(Predicate<? super T> filter)
default <O> TransformablePublisher<O>
flatMap(Function<? super T,? extends Promise<? extends O>> function)
default TransformablePublisher<T>
fork()
Consumes the given publisher eagerly in a forked execution, buffering results until ready to be consumed by this execution.default TransformablePublisher<T>
fork(Action<? super ExecSpec> execConfig, Action<? super T> disposer)
Consumes the given publisher eagerly in a forked execution, buffering results until ready to be consumed by this execution.default TransformablePublisher<T>
gate(Action<? super Runnable> valveReceiver)
default <O> TransformablePublisher<O>
map(Function<? super T,? extends O> function)
default TransformablePublisher<T>
multicast()
default <R> Promise<R>
reduce(R seed, BiFunction<? super R,? super T,? extends R> reducer)
Reduces the stream to a single value, by applying the given function successively.default <O> TransformablePublisher<O>
streamMap(StreamMapper<? super T,O> mapper)
default TransformablePublisher<T>
take(long count)
default TransformablePublisher<T>
takeWhile(Predicate<T> condition)
default Promise<List<T>>
toList()
Consumes the given publisher's items to a list.default Promise<T>
toPromise()
default <O> TransformablePublisher<O>
transform(Function<? super TransformablePublisher<? extends T>,? extends Publisher<O>> transformer)
Convenience method to allow a non Ratpack publisher transform method to be hooked in.default TransformablePublisher<T>
wiretap(Action<? super StreamEvent<T>> listener)
-
-
-
Method Detail
-
map
default <O> TransformablePublisher<O> map(Function<? super T,? extends O> function)
- Type Parameters:
O
- the type of transformed item- Parameters:
function
- the transformation- Returns:
- the transformed publisher
-
flatMap
default <O> TransformablePublisher<O> flatMap(Function<? super T,? extends Promise<? extends O>> function)
- Type Parameters:
O
- the type of transformed item- Parameters:
function
- the transformation- Returns:
- the transformed publisher
-
buffer
default TransformablePublisher<T> buffer()
- Returns:
- a buffering publisher
-
gate
default TransformablePublisher<T> gate(Action<? super Runnable> valveReceiver)
- Parameters:
valveReceiver
- an action that receives a runnable “valve” that when run allows request to start flowing upstream- Returns:
- a publisher that is logically equivalent to the given publisher as far as subscribers are concerned
-
wiretap
default TransformablePublisher<T> wiretap(Action<? super StreamEvent<T>> listener)
- Parameters:
listener
- the listener for emitted items- Returns:
- a publisher that is logically equivalent to the given publisher as far as subscribers are concerned
-
multicast
default TransformablePublisher<T> multicast()
- Returns:
- a publisher that respects back pressure for each of its subscribers
-
toList
default Promise<List<T>> toList()
Consumes the given publisher's items to a list.This method can be useful when testing, but should be uses with care in production code as it will exhaust memory if the stream is very large.
import org.reactivestreams.Publisher; import ratpack.exec.stream.Streams; import ratpack.test.exec.ExecHarness; import java.util.Arrays; import java.util.List; import static org.junit.jupiter.api.Assertions.*; public class Example { public static void main(String... args) throws Exception { List<String> expected = Arrays.asList("a", "b", "c"); List<String> result = ExecHarness.yieldSingle(execControl -> Streams.publish(expected).toList() ).getValue(); assertEquals(Arrays.asList("a", "b", "c"), result); } }
If the publisher emits an error, the promise will fail and the collected items will be discarded.
import org.reactivestreams.Publisher; import ratpack.exec.stream.Streams; import ratpack.test.exec.ExecHarness; import static org.junit.jupiter.api.Assertions.*; public class Example { public static void main(String... args) throws Exception { Throwable error = ExecHarness.yieldSingle(execControl -> Streams.yield(r -> { if (r.getRequestNum() < 1) { return "a"; } else { throw new RuntimeException("bang!"); } }).toList() ).getThrowable(); assertEquals("bang!", error.getMessage()); } }
- Returns:
- a promise for the stream's contents as a list
-
transform
default <O> TransformablePublisher<O> transform(Function<? super TransformablePublisher<? extends T>,? extends Publisher<O>> transformer)
Convenience method to allow a non Ratpack publisher transform method to be hooked in.This transformable publisher will be given to the function, that should return a new publisher. The returned publisher will then be wrapped in a transformable wrapper which will be returned by this method.
- Type Parameters:
O
- the type of transformed item- Parameters:
transformer
- a publisher transformer- Returns:
- a publisher that respects back pressure for each of its subscribers
-
streamMap
default <O> TransformablePublisher<O> streamMap(StreamMapper<? super T,O> mapper)
- Type Parameters:
O
- the type of transformed item- Parameters:
mapper
- the transformation- Returns:
- the transformed publisher
- Since:
- 1.4
-
filter
default TransformablePublisher<T> filter(Predicate<? super T> filter)
- Parameters:
filter
- the filter- Returns:
- the filtered publisher
-
bindExec
default TransformablePublisher<T> bindExec()
- Returns:
- a publisher bound to the current execution
- Since:
- 1.4
-
bindExec
default TransformablePublisher<T> bindExec(Action<? super T> disposer)
- Parameters:
disposer
- the disposer of unhandled items- Returns:
- a publisher bound to the current execution
- Since:
- 1.5
-
reduce
default <R> Promise<R> reduce(R seed, BiFunction<? super R,? super T,? extends R> reducer)
Reduces the stream to a single value, by applying the given function successively.- Type Parameters:
R
- the type of result- Parameters:
seed
- the initial valuereducer
- the reducing function- Returns:
- a promise for the reduced value
- Since:
- 1.4
-
fork
default TransformablePublisher<T> fork(Action<? super ExecSpec> execConfig, Action<? super T> disposer)
Consumes the given publisher eagerly in a forked execution, buffering results until ready to be consumed by this execution.- Parameters:
execConfig
- the configuration for the forked executiondisposer
- the disposer for any buffered items when the stream errors or is cancelled- Returns:
- an execution bound publisher that propagates the items of the given publisher
- Since:
- 1.5
- See Also:
Streams.fork(Publisher, Action, Action)
-
fork
default TransformablePublisher<T> fork()
Consumes the given publisher eagerly in a forked execution, buffering results until ready to be consumed by this execution.This method is identical to
fork(Action, Action)
, but usesAction.noop()
for both arguments.- Returns:
- an execution bound publisher that propagates the items of the given publisher
- Since:
- 1.5
- See Also:
Streams.fork(Publisher, Action, Action)
-
take
default TransformablePublisher<T> take(long count)
- Returns:
- a publisher that will emit a max of
n
elements - Since:
- 1.5
-
takeWhile
default TransformablePublisher<T> takeWhile(Predicate<T> condition)
- Returns:
- a publisher that will emit as long as the given predicate is true
- Since:
- 2.0
-
batch
default TransformablePublisher<T> batch(int batchSize, Action<? super T> disposer)
- Returns:
- a publisher that batches upstream requests
- Since:
- 1.5
-
-