Class ResolvedPromiseImpl<T>
- Type Parameters:
T
- The result type associated with the Promise.
- All Implemented Interfaces:
Promise<T>
This class is not used directly by clients. Clients should use
PromiseFactory.resolved(Object)
to create a resolved Promise
.
- Since:
- 1.1
-
Nested Class Summary
Nested classes/interfaces inherited from class org.osgi.util.promise.PromiseImpl
PromiseImpl.Result<P>
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionResolvedPromiseImpl
(T value, PromiseFactory factory) Initialize this resolved Promise. -
Method Summary
Modifier and TypeMethodDescription(package private) PromiseImpl.Result
<T> collect()
Return a holder of the result of this PromiseImpl.fallbackTo
(Promise<? extends T> fallback) Fall back to the value of the specified Promise if this Promise fails.Returns the failure of this Promise.getValue()
Returns the value of this Promise.boolean
isDone()
Returns whether this Promise has been resolved.Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure.Recover from a failure of this Promise with a recovery value.recoverWith
(Function<Promise<?>, Promise<? extends T>> recovery) Recover from a failure of this Promise with a recovery Promise.<R> Promise
<R> Chain a new Promise to this Promise with Success and Failure callbacks.timeout
(long millis) Time out the resolution of this Promise.toString()
Methods inherited from class org.osgi.util.promise.PromiseImpl
chain, collect, deferred, delay, failed, filter, flatMap, map, notifyCallbacks, onResolve, onSuccess, resolved, schedule, then, thenAccept, uncaughtException
-
Field Details
-
value
The value of this resolved Promise.
-
-
Constructor Details
-
ResolvedPromiseImpl
ResolvedPromiseImpl(T value, PromiseFactory factory) Initialize this resolved Promise.- Parameters:
value
- The value of this resolved Promise.factory
- The factory to use for callbacks and scheduled operations.
-
-
Method Details
-
isDone
public boolean isDone()Returns whether this Promise has been resolved.This Promise may be successfully resolved or resolved with a failure.
- Returns:
true
if this Promise was resolved either successfully or with a failure;false
if this Promise is unresolved.
-
getValue
Returns the value of this Promise.If this Promise is not
resolved
, this method must block and wait for this Promise to be resolved before completing.If this Promise was successfully resolved, this method returns with the value of this Promise. If this Promise was resolved with a failure, this method must throw an
InvocationTargetException
with thefailure exception
as the cause.- Returns:
- The value of this resolved Promise.
-
getFailure
Returns the failure of this Promise.If this Promise is not
resolved
, this method must block and wait for this Promise to be resolved before completing.If this Promise was resolved with a failure, this method returns with the failure of this Promise. If this Promise was successfully resolved, this method must return
null
.- Returns:
- The failure of this resolved Promise or
null
if this Promise was successfully resolved.
-
collect
PromiseImpl.Result<T> collect()Return a holder of the result of this PromiseImpl.- Specified by:
collect
in classPromiseImpl<T>
-
toString
-
onFailure
Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure. The callback will not be called if this Promise is resolved successfully.This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
Promise.isDone()
must returntrue
andPromise.getValue()
andPromise.getFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
-
then
Chain a new Promise to this Promise with Success and Failure callbacks.The specified
Success
callback is called when this Promise is successfully resolved and the specifiedFailure
callback is called when this Promise is resolved with a failure.This method returns a new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Success or Failure callback is executed. The result of the executed callback must be used to resolve the returned Promise. Multiple calls to this method can be used to create a chain of promises which are resolved in sequence.
If this Promise is successfully resolved, the Success callback is executed and the result Promise, if any, or thrown exception is used to resolve the returned Promise from this method. If this Promise is resolved with a failure, the Failure callback is executed and the returned Promise from this method is failed.
This method may be called at any time including before and after this Promise has been resolved.
Resolving this Promise happens-before any registered callback is called. That is, in a registered callback,
Promise.isDone()
must returntrue
andPromise.getValue()
andPromise.getFailure()
must not block.A callback may be called on a different thread than the thread which registered the callback. So the callback must be thread safe but can rely upon that the registration of the callback happens-before the registered callback is called.
- Specified by:
then
in interfacePromise<T>
- Overrides:
then
in classPromiseImpl<T>
- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
success
- The Success callback to be called when this Promise is successfully resolved. May benull
if no Success callback is required. In this case, the returned Promise must be resolved with the valuenull
when this Promise is successfully resolved.failure
- The Failure callback to be called when this Promise is resolved with a failure. May benull
if no Failure callback is required.- Returns:
- A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Success or Failure callback, if any, is executed.
-
recover
Recover from a failure of this Promise with a recovery value.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure, the specified Function is applied to this Promise to produce a recovery value.
- If the recovery value is not
null
, the returned Promise must be resolved with the recovery value. - If the recovery value is
null
, the returned Promise must be failed with the failure of this Promise. - If the specified Function throws an exception, the returned Promise must be failed with that exception.
To recover from a failure of this Promise with a recovery value of
null
, thePromise.recoverWith(Function)
method must be used. The specified Function forPromise.recoverWith(Function)
can returnPromises.resolved(null)
to supply the desirednull
value.This method may be called at any time including before and after this Promise has been resolved.
- Specified by:
recover
in interfacePromise<T>
- Overrides:
recover
in classPromiseImpl<T>
- Parameters:
recovery
- If this Promise resolves with a failure, the specified Function is called to produce a recovery value to be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that resolves with the value of this Promise or recovers from the failure of this Promise.
- If the recovery value is not
-
recoverWith
Recover from a failure of this Promise with a recovery Promise.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure, the specified Function is applied to this Promise to produce a recovery Promise.
- If the recovery Promise is not
null
, the returned Promise must be resolved with the recovery Promise. - If the recovery Promise is
null
, the returned Promise must be failed with the failure of this Promise. - If the specified Function throws an exception, the returned Promise must be failed with that exception.
This method may be called at any time including before and after this Promise has been resolved.
- Specified by:
recoverWith
in interfacePromise<T>
- Overrides:
recoverWith
in classPromiseImpl<T>
- Parameters:
recovery
- If this Promise resolves with a failure, the specified Function is called to produce a recovery Promise to be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that resolves with the value of this Promise or recovers from the failure of this Promise.
- If the recovery Promise is not
-
fallbackTo
Fall back to the value of the specified Promise if this Promise fails.If this Promise is successfully resolved, the returned Promise must be resolved with the value of this Promise.
If this Promise is resolved with a failure, the successful result of the specified Promise is used to resolve the returned Promise. If the specified Promise is resolved with a failure, the returned Promise must be failed with the failure of this Promise rather than the failure of the specified Promise.
This method may be called at any time including before and after this Promise has been resolved.
- Specified by:
fallbackTo
in interfacePromise<T>
- Overrides:
fallbackTo
in classPromiseImpl<T>
- Parameters:
fallback
- The Promise whose value must be used to resolve the returned Promise if this Promise resolves with a failure. Must not benull
.- Returns:
- A Promise that returns the value of this Promise or falls back to the value of the specified Promise.
-
timeout
Time out the resolution of this Promise.If this Promise is successfully resolved before the timeout, the returned Promise is resolved with the value of this Promise. If this Promise is resolved with a failure before the timeout, the returned Promise is resolved with the failure of this Promise. If the timeout is reached before this Promise is resolved, the returned Promise is failed with a
TimeoutException
.- Specified by:
timeout
in interfacePromise<T>
- Overrides:
timeout
in classPromiseImpl<T>
- Parameters:
millis
- The time to wait in milliseconds. Zero and negative time is treated as an immediate timeout.- Returns:
- A Promise that is resolved when either this Promise is resolved or the specified timeout is reached.
-