Class Deferred<T>
- Type Parameters:
T
- The value type associated with the created Promise.
Instances of this class can be used to create a Promise
that can be
resolved in the future. The associated
Promise can be
successfully resolved with resolve(Object)
or resolved with a
failure with fail(Throwable)
. It can also be resolved with the
resolution of another promise using resolveWith(Promise)
.
The associated Promise can be provided to any one, but the Deferred object should be made available only to the party that will responsible for resolving the Promise.
- "Immutable"
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Fail the Promise associated with this Deferred.Returns the Promise associated with this Deferred.void
Successfully resolve the Promise associated with this Deferred.resolveWith
(CompletionStage<? extends T> with) Resolve the Promise associated with this Deferred with the specified CompletionStage.resolveWith
(Promise<? extends T> with) Resolve the Promise associated with this Deferred with the specified Promise.toString()
Returns a string representation of the associated Promise.
-
Constructor Details
-
Deferred
public Deferred()Create a new Deferred.The
associated promise
will use the default callback executor and default scheduled executor.- See Also:
-
-
Method Details
-
getPromise
Returns the Promise associated with this Deferred.All Promise objects created by the associated Promise will use the executors of the associated Promise.
- Returns:
- The Promise associated with this Deferred.
-
resolve
Successfully resolve the Promise associated with this Deferred.After the associated Promise is resolved with the specified value, all registered
callbacks
are called and anychained
Promises are resolved. This may occur asynchronously to this method.Resolving the associated 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.- Parameters:
value
- The value of the resolved Promise.- Throws:
IllegalStateException
- If the associated Promise was already resolved.
-
fail
Fail the Promise associated with this Deferred.After the associated Promise is resolved with the specified failure, all registered
callbacks
are called and anychained
Promises are resolved. This may occur asynchronously to this method.Resolving the associated 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.- Parameters:
failure
- The failure of the resolved Promise. Must not benull
.- Throws:
IllegalStateException
- If the associated Promise was already resolved.
-
resolveWith
Resolve the Promise associated with this Deferred with the specified Promise.If the specified Promise is successfully resolved, the associated Promise is resolved with the value of the specified Promise. If the specified Promise is resolved with a failure, the associated Promise is resolved with the failure of the specified Promise.
After the associated Promise is resolved with the specified Promise, all registered
callbacks
are called and anychained
Promises are resolved. This may occur asynchronously to this method.Resolving the associated 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.- Parameters:
with
- A Promise whose value or failure must be used to resolve the associated Promise. Must not benull
.- Returns:
- A Promise that is resolved only when the associated Promise is
resolved by the specified Promise. The returned Promise must be
successfully resolved with the value
null
, if the associated Promise was resolved by the specified Promise. The returned Promise must be resolved with a failure ofIllegalStateException
, if the associated Promise was already resolved when the specified Promise was resolved.
-
toString
Returns a string representation of the associated Promise. -
resolveWith
Resolve the Promise associated with this Deferred with the specified CompletionStage.If the specified CompletionStage is completed normally, the associated Promise is resolved with the value of the specified CompletionStage. If the specified CompletionStage is completed exceptionally, the associated Promise is resolved with the failure of the specified CompletionStage.
After the associated Promise is resolved with the specified CompletionStage, all registered
callbacks
are called and anychained
Promises are resolved. This may occur asynchronously to this method.Resolving the associated 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.- Parameters:
with
- A CompletionStage whose result must be used to resolve the associated Promise. Must not benull
.- Returns:
- A Promise that is resolved only when the associated Promise is
resolved by the specified CompletionStage. The returned Promise
must be successfully resolved with the value
null
, if the associated Promise was resolved by the specified CompletionStage. The returned Promise must be resolved with a failure ofIllegalStateException
, if the associated Promise was already resolved when the specified CompletionStage was completed. - Since:
- 1.2
-