Class DeferredPromiseImpl<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.deferred() to create a Deferred which can be
used to obtain a Promise whose resolution can be deferred.
- Since:
- 1.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final classA callback used to resolve the chained Promise when the Promise is resolved.private final classA callback used to resolve the chained Promise when the PromiseImpl is resolved.(package private) final classA callback used by thePromiseImpl.delay(long)method to delay chaining a promise.private final classA callback used to resolve the chained Promise when the fallback Promise is resolved.(package private) final classA callback used by thePromiseImpl.fallbackTo(Promise)method.(package private) final classA callback used by thePromiseImpl.filter(Predicate)method.(package private) final classA callback used by thePromiseImpl.flatMap(Function)method.(package private) final classA callback used by thePromiseImpl.map(Function)method.(package private) final classA callback used by thePromiseImpl.recover(Function)method.(package private) final classA callback used by thePromiseImpl.recoverWith(Function)method.private final classA callback used to resolve a Promise with another Promise for theresolveWith(Promise)method.(package private) final classA callback used by thePromiseFactory.submit(Callable)method.(package private) final classA callback used to chain promises for thePromiseImpl.then(Success, Failure)method.(package private) final classA callback used by thePromiseImpl.thenAccept(Consumer)method.(package private) final classA callback used by thePromiseImpl.timeout(long)method to schedule the timeout and to resolve the chained Promise and cancel the timeout.Nested classes/interfaces inherited from class org.osgi.util.promise.PromiseImpl
PromiseImpl.Result<P> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ThrowableThe failure of this Promise if resolved with a failure ornullif successfully resolved.private final CountDownLatchA CountDownLatch to manage the resolved state of this Promise.private TThe value of this Promise if successfully resolved. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) PromiseImpl.Result<T> collect()Return a holder of the result of this PromiseImpl.Returns the failure of this Promise.getValue()Returns the value of this Promise.booleanisDone()Returns whether this Promise has been resolved.(package private) PromiseImpl<T> orDone()Return a resolved PromiseImpl if this DeferredPromiseImpl is resolved.(package private) voidResolve this Promise.resolveWith(Promise<? extends T> with) Resolve this Promise with the specified Promise.toString()(package private) booleantryResolve(T v, Throwable f) Try to resolve this Promise.Methods inherited from class org.osgi.util.promise.PromiseImpl
chain, collect, deferred, delay, failed, fallbackTo, filter, flatMap, map, notifyCallbacks, onFailure, onResolve, onSuccess, recover, recoverWith, resolved, schedule, then, then, thenAccept, timeout, uncaughtException
-
Field Details
-
resolved
A CountDownLatch to manage the resolved state of this Promise.This object is used as the synchronizing object to provide a critical section in
tryResolve(Object, Throwable)so that only a single thread can write the resolved state variables and open the latch.The resolved state variables,
valueandfail, must only be written when the latch is closed (getCount() != 0) and must only be read when the latch is open (getCount() == 0). The latch state must always be checked before writing or reading since the resolved state variables' memory consistency is guarded by the latch. -
value
The value of this Promise if successfully resolved.- See Also:
-
fail
The failure of this Promise if resolved with a failure ornullif successfully resolved.- See Also:
-
-
Constructor Details
-
DeferredPromiseImpl
DeferredPromiseImpl(PromiseFactory factory) Initialize this Promise.- Parameters:
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:
trueif this Promise was resolved either successfully or with a failure;falseif this Promise is unresolved.
-
orDone
PromiseImpl<T> orDone()Return a resolved PromiseImpl if this DeferredPromiseImpl is resolved.- Returns:
- A ResolvedPromiseImpl holding the value of this DeferredPromiseImpl or a FailedPromiseImpl holding the failure of this DeferredPromiseImpl or this DeferredPromiseImpl if this DeferredPromiseImpl is not resolved.
-
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
InvocationTargetExceptionwith thefailure exceptionas the cause.- Returns:
- The value of this resolved Promise.
- Throws:
InvocationTargetException- If this Promise was resolved with a failure. The cause of theInvocationTargetExceptionis the failure exception.InterruptedException- If the current thread was interrupted while waiting.
-
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
nullif this Promise was successfully resolved. - Throws:
InterruptedException- If the current thread was interrupted while waiting.
-
collect
PromiseImpl.Result<T> collect()Return a holder of the result of this PromiseImpl.- Specified by:
collectin classPromiseImpl<T>
-
toString
-
tryResolve
Try to resolve this Promise.If this Promise was already resolved, return false. Otherwise, resolve this Promise and return true.
- Parameters:
v- The value of this Promise.f- The failure of this Promise.- Returns:
- false if this Promise was already resolved; true if this method resolved this Promise.
-
resolve
Resolve this Promise.If this Promise was already resolved, throw IllegalStateException. Otherwise, resolve this Promise.
- Parameters:
v- The value of this Promise.f- The failure of this Promise.- Throws:
IllegalStateException- If this Promise was already resolved.
-
resolveWith
Resolve this Promise with the specified Promise.If the specified Promise is successfully resolved, this Promise is resolved with the value of the specified Promise. If the specified Promise is resolved with a failure, this Promise is resolved with the failure of the specified Promise.
- Parameters:
with- A Promise whose value or failure must be used to resolve this Promise. Must not benull.- Returns:
- A Promise that is resolved only when this Promise is resolved by
the specified Promise. The returned Promise must be successfully
resolved with the value
null, if this Promise was resolved by the specified Promise. The returned Promise must be resolved with a failure ofIllegalStateException, if this Promise was already resolved when the specified Promise was resolved.
-