@ConsumerType public class PromiseFactory extends Object
Instances of this class can be used to create Deferred and Promise objects which use the executors used to construct this object for any callback or scheduled operation execution.
Modifier and Type | Class and Description |
---|---|
static class |
PromiseFactory.Option
Defines the options for a Promise factory.
|
Constructor and Description |
---|
PromiseFactory(Executor callbackExecutor)
Create a new PromiseFactory with the specified callback executor.
|
PromiseFactory(Executor callbackExecutor,
ScheduledExecutorService scheduledExecutor)
Create a new PromiseFactory with the specified callback executor and
specified scheduled executor.
|
PromiseFactory(Executor callbackExecutor,
ScheduledExecutorService scheduledExecutor,
PromiseFactory.Option... options)
Create a new PromiseFactory with the specified callback executor,
specified scheduled executor, and specified options.
|
Modifier and Type | Method and Description |
---|---|
<T,S extends T> |
all(Collection<Promise<S>> promises)
Returns a new Promise that is a latch on the resolution of the specified
Promises.
|
<T> Deferred<T> |
deferred()
Create a new Deferred with the callback executor and scheduled executor
of this PromiseFactory object.
|
Executor |
executor()
Returns the executor to use for callbacks.
|
<T> Promise<T> |
failed(Throwable failure)
Returns a new Promise that has been resolved with the specified failure.
|
static Executor |
inlineExecutor()
Returns an Executor implementation that executes tasks immediately on the
thread calling the
Executor.execute method. |
<T> Promise<T> |
resolved(T value)
Returns a new Promise that has been resolved with the specified value.
|
<T> Promise<T> |
resolvedWith(CompletionStage<? extends T> with)
Returns a new Promise that will be resolved with the result of the
specified CompletionStage.
|
<T> Promise<T> |
resolvedWith(Promise<? extends T> with)
Returns a new Promise that will be resolved with the specified Promise.
|
ScheduledExecutorService |
scheduledExecutor()
Returns the scheduled executor to use for scheduled operations.
|
<T> Promise<T> |
submit(Callable<? extends T> task)
Returns a new Promise that will hold the result of the specified task.
|
<T,S extends T> |
toPromise()
Returns a
Collector that accumulates the results of the input
Promises into a new all(Collection) Promise. |
public PromiseFactory(Executor callbackExecutor)
The default scheduled executor and default options will be used.
callbackExecutor
- The executor to use for callbacks. null
can be specified for the default callback executor.public PromiseFactory(Executor callbackExecutor, ScheduledExecutorService scheduledExecutor)
The default options will be used.
callbackExecutor
- The executor to use for callbacks. null
can be specified for the default callback executor.scheduledExecutor
- The scheduled executor for use for scheduled
operations. null
can be specified for the default
scheduled executor.public PromiseFactory(Executor callbackExecutor, ScheduledExecutorService scheduledExecutor, PromiseFactory.Option... options)
callbackExecutor
- The executor to use for callbacks. null
can be specified for the default callback executor.scheduledExecutor
- The scheduled executor for use for scheduled
operations. null
can be specified for the default
scheduled executor.options
- Options for PromiseFactory.public Executor executor()
null
was specified for the callback
executor when this PromiseFactory was created.public ScheduledExecutorService scheduledExecutor()
null
was specified
for the scheduled executor when this PromiseFactory was created.public <T> Deferred<T> deferred()
Use this method instead of Deferred()
to create a new
Deferred
whose associated Promise uses executors other than the
default executors.
T
- The value type associated with the returned Deferred.Deferred
with the callback and scheduled executors
of this PromiseFactory objectpublic <T> Promise<T> resolved(T value)
The returned Promise uses the callback executor and scheduled executor of this PromiseFactory object.
Use this method instead of Promises.resolved(Object)
to create a
Promise which uses executors other than the default executors.
T
- The value type associated with the returned Promise.value
- The value of the resolved Promise.public <T> Promise<T> failed(Throwable failure)
The returned Promise uses the callback executor and scheduled executor of this PromiseFactory object.
Use this method instead of Promises.failed(Throwable)
to create a
Promise which uses executors other than the default executors.
T
- The value type associated with the returned Promise.failure
- The failure of the resolved Promise. Must not be
null
.public <T> Promise<T> submit(Callable<? extends T> task)
The returned Promise uses the callback executor and scheduled executor of this PromiseFactory object.
The specified task will be executed on the callback
executor
.
T
- The value type associated with the returned Promise.task
- The task whose result will be available from the returned
Promise.public <T,S extends T> Promise<List<T>> all(Collection<Promise<S>> promises)
The returned Promise uses the callback executor and scheduled executor of this PromiseFactory object.
The returned Promise acts as a gate and must be resolved after all of the specified Promises are resolved.
T
- The value type of the List value associated with the returned
Promise.S
- The value type of the specified Promises.promises
- The Promises which must be resolved before the returned
Promise must be resolved. Must not be null
and all of
the elements in the collection must not be null
.FailedPromisesException
if any of the specified Promises
are resolved with a failure. The failure
FailedPromisesException
must contain all of the specified
Promises which resolved with a failure.public static Executor inlineExecutor()
Executor.execute
method.Executor.execute
method.public <T> Promise<T> resolvedWith(CompletionStage<? extends T> with)
The returned Promise uses the callback executor and scheduled executor of this PromiseFactory object.
If the specified CompletionStage is completed normally, the returned Promise is resolved with the value of the specified CompletionStage. If the specified CompletionStage is completed exceptionally, the returned Promise is resolved with the exception of the specified CompletionStage.
After the returned Promise is resolved with the specified
CompletionStage, all registered callbacks
are called and any chained
Promises are resolved. This may occur asynchronously to this
method.
Resolving the returned Promise happens-before any registered
callback is called. That is, in a registered callback,
Promise.isDone()
must return true
and
Promise.getValue()
and Promise.getFailure()
must not
block.
T
- The value type associated with the returned Promise.with
- A CompletionStage whose result will be used to resolve the
returned Promise. Must not be null
.public <T> Promise<T> resolvedWith(Promise<? extends T> with)
The returned Promise uses the callback executor and scheduled executor of this PromiseFactory object.
If the specified Promise is successfully resolved, the returned Promise is resolved with the value of the specified Promise. If the specified Promise is resolved with a failure, the returned Promise is resolved with the failure of the specified Promise.
After the returned Promise is resolved with the specified Promise, all
registered callbacks
are called and
any chained
Promises are resolved.
This may occur asynchronously to this method.
Resolving the returned Promise happens-before any registered
callback is called. That is, in a registered callback,
Promise.isDone()
must return true
and
Promise.getValue()
and Promise.getFailure()
must not
block.
T
- The value type associated with the returned Promise.with
- A Promise whose value or failure must be used to resolve the
returned Promise. Must not be null
.public <T,S extends T> Collector<Promise<S>,?,Promise<List<T>>> toPromise()
Collector
that accumulates the results of the input
Promises into a new all(Collection)
Promise.T
- The value type of the List value result of the collected
all(Collection)
Promise.S
- The value type of the input Promises.Collector
which accumulates the results of all the
input Promises into a new all(Collection)
Promise.Copyright © Contributors to the Eclipse Foundation Licensed under the Eclipse Foundation Specification License – v1.0