T - the type of the asynchronous computation resultpublic abstract class Promise<T> extends Object implements Future<T>
Promise is a Future that provides methods to set its result. They are useful to interact with callback-based APIs like the ones that are typically provided by network libraries. A promise can be created and returned synchronously to the caller, but its completion is deferred until the value is set, typically by a callback.
| Modifier | Constructor and Description |
|---|---|
protected |
Promise() |
| Modifier and Type | Method and Description |
|---|---|
static <T> Promise<T> |
apply()
Creates a promise without an interrupt handler.
|
static <T> Promise<T> |
apply(InterruptHandler handler)
Creates a promise that triggers the provided handler in case it receives an interrupt.
|
protected static <T> Promise<T> |
apply(InterruptHandler h1,
InterruptHandler h2)
Creates an interrupt handler that calls to other handlers.
|
static <T> Promise<T> |
apply(List<? extends InterruptHandler> handlers)
Creates a promise that triggers the provided handlers in case it receives an interrupt.
|
void |
become(Future<T> result)
Becomes another future.
|
boolean |
becomeIfEmpty(Future<T> result)
Becomes another future only if this promise is undefined.
|
<U,R> Future<R> |
biFlatMap(Future<U> other,
java.util.function.BiFunction<? super T,? super U,? extends Future<R>> f)
Waits for this and other (running in parallel), maps the result with f, and flattens the result.
|
<U,R> Future<R> |
biMap(Future<U> other,
java.util.function.BiFunction<? super T,? super U,? extends R> f)
Waits for this and other (running in parallel) and then maps the result with f.
|
protected <R> Future<R> |
continuation(io.trane.future.Continuation<T,R> c) |
static <T> Promise<T> |
create(java.util.function.Function<Promise<T>,InterruptHandler> handlerBuilder)
Creates a new promise using a handler builder that it’s based on the promise under creation.
|
Future<T> |
delayed(java.time.Duration delay,
ScheduledExecutorService scheduler)
Delays the result of this future.
|
Future<T> |
ensure(Runnable f)
Runs r when this future completes.
|
<R> Future<R> |
flatMap(java.util.function.Function<? super T,? extends Future<R>> f)
Maps the result of this future to another future and flattens the result.
|
T |
get(java.time.Duration timeout)
Blocks the current thread until this future is satisfied and gets its result.
|
protected InterruptHandler |
getInterruptHandler() |
protected Optional<?>[] |
getSavedContext() |
Future<T> |
interruptible()
Creates a future that will be completed with the interrupt exception if an interrupt is received.
|
boolean |
isDefined()
Checks if this future is completed.
|
void |
join(java.time.Duration timeout)
Blocks the current thread until this future is satisfied.
|
<R> Future<R> |
map(java.util.function.Function<? super T,? extends R> f)
Maps the result of this future to another value.
|
Future<T> |
onFailure(java.util.function.Consumer<Throwable> c)
Executes the Consumer if this future completes with an exception.
|
Future<T> |
onSuccess(java.util.function.Consumer<? super T> c)
Executes the Consumer if this future completes successfully.
|
void |
proxyTo(Promise<T> p)
Proxies the result of this future, successful or not, to a Promise.
|
void |
raise(Throwable ex)
Raises an interrupt.
|
Future<T> |
rescue(java.util.function.Function<Throwable,? extends Future<T>> f)
If this future completes with an exception, applies the provided rescue function and flattens the result.
|
Future<T> |
respond(Responder<? super T> r)
Executes the Responder once this future completes.
|
protected io.trane.future.WaitQueue<T> |
safeFlush(Future<T> result) |
void |
setException(Throwable ex)
Completes this promise with a failure ex.
|
void |
setValue(T value)
Completes this promise with value.
|
String |
toString() |
protected String |
toStringPrefix() |
<R> Future<R> |
transform(Transformer<? super T,? extends R> t)
Maps the result of this future using the provided Transformer.
|
<R> Future<R> |
transformWith(Transformer<? super T,? extends Future<R>> t)
Maps the result of this future using a Transformer that returns another future and flattens the result.
|
Future<Void> |
voided()
Creates a future that is satisfied with void when this future completes.
|
Future<T> |
within(java.time.Duration timeout,
ScheduledExecutorService scheduler,
Throwable exception)
Creates a future that fails with a exception if this future isn’t completed within the timeout.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitapply, collect, delay, emptyList, emptyOptional, exception, firstCompletedOf, flatApply, flatten, join, never, selectIndex, unsafeCast, value, whileDo, withinapplypublic static final <T> Promise<T> apply(List<? extends InterruptHandler> handlers)
Creates a promise that triggers the provided handlers in case it receives an interrupt.
handlers - the list of handlers to be triggered.public static final <T> Promise<T> apply(InterruptHandler handler)
Creates a promise that triggers the provided handler in case it receives an interrupt.
handler - the handler to be triggered.public static final <T> Promise<T> apply()
Creates a promise without an interrupt handler. Interrupt signals are ignored by the created promise since there’s no handler.
public static final <T> Promise<T> create(java.util.function.Function<Promise<T>,InterruptHandler> handlerBuilder)
Creates a new promise using a handler builder that it’s based on the promise under creation. This method allows the user to define handlers that use the it’s own promise.
handlerBuilder - a builder that receives the new promise and returns the interrupt handler of the new promise.protected static final <T> Promise<T> apply(InterruptHandler h1, InterruptHandler h2)
InterruptHandlerCreates an interrupt handler that calls to other handlers.
apply in interface InterruptHandlerh1 - first handler to be called.h2 - second handler to be called.protected InterruptHandler getInterruptHandler()
protected Optional<?>[] getSavedContext()
public final void become(Future<T> result)
Becomes another future. This and result become the same: both are completed with the same result and both receive the same interrupt signals.
result - the future to become.public final boolean becomeIfEmpty(Future<T> result)
Becomes another future only if this promise is undefined. This and result become the same: both are completed with the same result and both receive the same interrupt signals.
result - the future to become.public final void setValue(T value)
Completes this promise with value.
value - the result.public final void setException(Throwable ex)
Completes this promise with a failure ex.
ex - the failure.public final void raise(Throwable ex)
Raises an interrupt.
raise in interface InterruptHandlerex - the interrupt exception.public Future<T> interruptible()
FutureCreates a future that will be completed with the interrupt exception if an interrupt is received.
interruptible in interface Future<T>public final boolean isDefined()
FutureChecks if this future is completed.
public final T get(java.time.Duration timeout) throws CheckedFutureException
FutureBlocks the current thread until this future is satisfied and gets its result. This method normally only useful for tests, avoid it for production code.
get in interface Future<T>timeout - for how long the thread should wait for the resultCheckedFutureException - wrapper exception used when the result is a checked exception. If the exception is unchecked, it’s thrown without this wrapper.public final void join(java.time.Duration timeout)
throws CheckedFutureException
FutureBlocks the current thread until this future is satisfied. This method normally only useful for tests, avoid it for production code.
This method is similar to get but it doesn’t return the future value nor throws if the future completes with an exception.
join in interface Future<T>timeout - for how long the thread should wait for the resultCheckedFutureException - wrapper exception used when the result is a checked exception. If the exception is unchecked, it’s thrown without this wrapper.public final <R> Future<R> map(java.util.function.Function<? super T,? extends R> f)
FutureMaps the result of this future to another value.
public final <R> Future<R> flatMap(java.util.function.Function<? super T,? extends Future<R>> f)
FutureMaps the result of this future to another future and flattens the result.
public <R> Future<R> transform(Transformer<? super T,? extends R> t)
FutureMaps the result of this future using the provided Transformer. If this future completes successfully, transformer.onValue is called. If this future completes with an exception, transformer.onException is called.
public <R> Future<R> transformWith(Transformer<? super T,? extends Future<R>> t)
FutureMaps the result of this future using a Transformer that returns another future and flattens the result. If this future completes successfully, transformer.onValue is called. If this future completes with an exception, transformer.onException is called.
transformWith in interface Future<T>t - a Transformer that applies the transformation.public <U,R> Future<R> biMap(Future<U> other, java.util.function.BiFunction<? super T,? super U,? extends R> f)
FutureWaits for this and other (running in parallel) and then maps the result with f.
public <U,R> Future<R> biFlatMap(Future<U> other, java.util.function.BiFunction<? super T,? super U,? extends Future<R>> f)
FutureWaits for this and other (running in parallel), maps the result with f, and flattens the result.
public final Future<T> ensure(Runnable f)
FutureRuns r when this future completes.
public final Future<T> onSuccess(java.util.function.Consumer<? super T> c)
FutureExecutes the Consumer if this future completes successfully.
public final Future<T> onFailure(java.util.function.Consumer<Throwable> c)
FutureExecutes the Consumer if this future completes with an exception.
public final Future<T> respond(Responder<? super T> r)
FutureExecutes the Responder once this future completes. If this future completes successfully, responder.onValue is called. If this future completes with an exception, responder.onException is called.
public final Future<T> rescue(java.util.function.Function<Throwable,? extends Future<T>> f)
FutureIf this future completes with an exception, applies the provided rescue function and flattens the result.
Note that it’s possible to return a Future.exception from f if the exception can’t be recovered.
public final Future<Void> voided()
FutureCreates a future that is satisfied with void when this future completes.
public final Future<T> delayed(java.time.Duration delay, ScheduledExecutorService scheduler)
FutureDelays the result of this future. It method doesn’t take in consideration how long this future takes to be completed. It assumes the state of this future after delay, being it completed or not.
public final void proxyTo(Promise<T> p)
FutureProxies the result of this future, successful or not, to a Promise.
public final Future<T> within(java.time.Duration timeout, ScheduledExecutorService scheduler, Throwable exception)
FutureCreates a future that fails with a exception if this future isn’t completed within the timeout.
within in interface Future<T>timeout - how long to wait for the resultscheduler - used to schedule an internal task after the timeout.exception - the exception to be thrown when the future times out.protected String toStringPrefix()
Copyright © 2017. All Rights Reserved.