-
- Type Parameters:
T- item type
- All Superinterfaces:
Flow.Publisher<T>,Subscribable<T>
public interface Single<T> extends Subscribable<T>
Single item publisher utility.
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Deprecated Methods Modifier and Type Method Description default Single<T>defaultIfEmpty(T defaultItem)Signals the default item if the upstream is empty.static <T> Single<T>defer(Supplier<? extends Single<? extends T>> supplier)Call the given supplier function for each individual downstream Subscriber to return a Flow.Publisher to subscribe to.static <T> Single<T>empty()Get aSingleinstance that completes immediately.static <T> Single<T>error(Throwable error)Create aSingleinstance that reports the given given exception to its subscriber(s).default <U> Multi<U>flatMap(Function<? super T,? extends Flow.Publisher<? extends U>> mapper)default <U> Multi<U>flatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper)Maps the single upstream value into anIterableand relays its items to the downstream.default <U> Single<U>flatMapSingle(Function<T,Single<U>> mapper)static <T> Single<T>from(Flow.Publisher<T> source)Create aSingleinstance that publishes the first and only item received from the given publisher.default Tget()Short-hand fortoFuture().toCompletableFuture().get().default Tget(long timeout, TimeUnit unit)Short-hand fortoFuture().toCompletableFuture().get().static <T> Single<T>just(T item)Create aSingleinstance that publishes the given item to its subscriber(s).default <U> Single<U>map(Mapper<T,U> mapper)default <U> Multi<U>mapMany(Mapper<T,Flow.Publisher<U>> mapper)static <T> Single<T>never()Get aSingleinstance that never completes.default Single<T>onComplete(Runnable onTerminate)Executes givenRunnablewhen onComplete signal is received.default Single<T>onError(Consumer<Throwable> onErrorConsumer)Executes givenRunnablewhen onError signal is received.default Single<T>onErrorResume(Function<? super Throwable,? extends T> onError)Functionproviding one item to be submitted as onNext in case of onError signal is received.default Single<T>onErrorResumeWith(Function<? super Throwable,? extends Single<? extends T>> onError)Resume stream from supplied publisher if onError signal is intercepted.default Single<T>onTerminate(Runnable onTerminate)Executes givenRunnablewhen any of signals onComplete, onCancel or onError is received.default Single<T>peek(Consumer<T> consumer)Invoke provided consumer for the item in stream.default Single<T>switchIfEmpty(Single<T> other)Switch to the other Single if the upstream is empty.default <U> Single<T>takeUntil(Flow.Publisher<U> other)Relay upstream items until the other source signals an item or completes.static Single<Long>timer(long time, TimeUnit unit, ScheduledExecutorService executor)Signal 0L and complete the sequence after the given time elapsed.default CompletionStage<Optional<T>>toOptionalStage()Exposes thisSingleinstance as aCompletionStagewithOptional<T>return type of the asynchronous operation.default CompletionStage<T>toStage()Exposes thisSingleinstance as aCompletionStage.-
Methods inherited from interface java.util.concurrent.Flow.Publisher
subscribe
-
Methods inherited from interface io.helidon.common.reactive.Subscribable
subscribe, subscribe, subscribe, subscribe
-
-
-
-
Method Detail
-
defer
static <T> Single<T> defer(Supplier<? extends Single<? extends T>> supplier)
Call the given supplier function for each individual downstream Subscriber to return a Flow.Publisher to subscribe to.- Type Parameters:
T- the element type of the sequence- Parameters:
supplier- the callback to return a Flow.Publisher for each Subscriber- Returns:
- Multi
- Throws:
NullPointerException- ifsupplierisnull
-
map
default <U> Single<U> map(Mapper<T,U> mapper)
- Type Parameters:
U- mapped item type- Parameters:
mapper- mapper- Returns:
- Single
- Throws:
NullPointerException- if mapper isnull
-
defaultIfEmpty
default Single<T> defaultIfEmpty(T defaultItem)
Signals the default item if the upstream is empty.- Parameters:
defaultItem- the item to signal if the upstream is empty- Returns:
- Single
- Throws:
NullPointerException- ifdefaultItemisnull
-
switchIfEmpty
default Single<T> switchIfEmpty(Single<T> other)
Switch to the other Single if the upstream is empty.- Parameters:
other- the Single to switch to if the upstream is empty.- Returns:
- Single
- Throws:
NullPointerException- ifotherisnull
-
mapMany
@Deprecated default <U> Multi<U> mapMany(Mapper<T,Flow.Publisher<U>> mapper)
Deprecated.- Type Parameters:
U- mapped items type- Parameters:
mapper- mapper- Returns:
- Publisher
- Throws:
NullPointerException- if mapper isnull
-
flatMap
default <U> Multi<U> flatMap(Function<? super T,? extends Flow.Publisher<? extends U>> mapper)
- Type Parameters:
U- mapped items type- Parameters:
mapper- mapper- Returns:
- Publisher
- Throws:
NullPointerException- if mapper isnull
-
flatMapSingle
default <U> Single<U> flatMapSingle(Function<T,Single<U>> mapper)
- Type Parameters:
U- mapped items type- Parameters:
mapper- mapper- Returns:
- Single
- Throws:
NullPointerException- if mapper isnull
-
flatMapIterable
default <U> Multi<U> flatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper)
Maps the single upstream value into anIterableand relays its items to the downstream.- Type Parameters:
U- the result type- Parameters:
mapper- the function that receives the single upstream value and should return an Iterable instance- Returns:
- Multi
- Throws:
NullPointerException- ifmapperisnull
-
toStage
default CompletionStage<T> toStage()
Exposes thisSingleinstance as aCompletionStage. Note that if thisSinglecompletes without a value, the resultingCompletionStagewill be completed exceptionally with anIllegalStateException- Returns:
- CompletionStage
-
toOptionalStage
default CompletionStage<Optional<T>> toOptionalStage()
Exposes thisSingleinstance as aCompletionStagewithOptional<T>return type of the asynchronous operation. Note that if thisSinglecompletes without a value, the resultingCompletionStagewill be completed exceptionally with anIllegalStateException- Returns:
- CompletionStage
-
get
default T get() throws InterruptedException, ExecutionException
Short-hand fortoFuture().toCompletableFuture().get().- Returns:
- T
- Throws:
InterruptedException- if the current thread was interrupted while waitingExecutionException- if the future completed exceptionally
-
get
default T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Short-hand fortoFuture().toCompletableFuture().get().- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- T
- Throws:
InterruptedException- if the current thread was interrupted while waitingExecutionException- if the future completed exceptionallyTimeoutException- if the wait timed out
-
from
static <T> Single<T> from(Flow.Publisher<T> source)
Create aSingleinstance that publishes the first and only item received from the given publisher. Note that if the publisher publishes more than one item, the resultingSinglewill hold an error. UseMulti.first()instead in order to get the first item of a publisher that may publish more than one item.- Type Parameters:
T- item type- Parameters:
source- source publisher- Returns:
- Single
- Throws:
NullPointerException- if source isnull
-
just
static <T> Single<T> just(T item)
Create aSingleinstance that publishes the given item to its subscriber(s).- Type Parameters:
T- item type- Parameters:
item- item to publish- Returns:
- Single
- Throws:
NullPointerException- if item isnull
-
error
static <T> Single<T> error(Throwable error)
Create aSingleinstance that reports the given given exception to its subscriber(s). The exception is reported by invokingFlow.Subscriber.onError(java.lang.Throwable)whenFlow.Publisher.subscribe(Subscriber)is called.- Type Parameters:
T- item type- Parameters:
error- exception to hold- Returns:
- Single
- Throws:
NullPointerException- if error isnull
-
empty
static <T> Single<T> empty()
Get aSingleinstance that completes immediately.- Type Parameters:
T- item type- Returns:
- Single
-
never
static <T> Single<T> never()
Get aSingleinstance that never completes.- Type Parameters:
T- item type- Returns:
- Single
-
timer
static Single<Long> timer(long time, TimeUnit unit, ScheduledExecutorService executor)
Signal 0L and complete the sequence after the given time elapsed.- Parameters:
time- the time to wait before signaling 0L and completionunit- the unit of timeexecutor- the executor to run the waiting on- Returns:
- Single
- Throws:
NullPointerException- ifunitorexecutorisnull
-
takeUntil
default <U> Single<T> takeUntil(Flow.Publisher<U> other)
Relay upstream items until the other source signals an item or completes.- Type Parameters:
U- the element type of the other sequence- Parameters:
other- the other sequence to signal the end of the main sequence- Returns:
- Single
- Throws:
NullPointerException- ifotherisnull
-
onTerminate
default Single<T> onTerminate(Runnable onTerminate)
Executes givenRunnablewhen any of signals onComplete, onCancel or onError is received.- Parameters:
onTerminate-Runnableto be executed.- Returns:
- Single
-
onComplete
default Single<T> onComplete(Runnable onTerminate)
Executes givenRunnablewhen onComplete signal is received.- Parameters:
onTerminate-Runnableto be executed.- Returns:
- Single
-
onError
default Single<T> onError(Consumer<Throwable> onErrorConsumer)
Executes givenRunnablewhen onError signal is received.- Parameters:
onErrorConsumer-Runnableto be executed.- Returns:
- Single
-
peek
default Single<T> peek(Consumer<T> consumer)
Invoke provided consumer for the item in stream.- Parameters:
consumer- consumer to be invoked- Returns:
- Single
-
onErrorResume
default Single<T> onErrorResume(Function<? super Throwable,? extends T> onError)
Functionproviding one item to be submitted as onNext in case of onError signal is received.- Parameters:
onError- Function receivingThrowableas argument and producing one item to resume stream with.- Returns:
- Single
-
-