Class DeferredPromiseImpl<T>
- java.lang.Object
-
- org.osgi.util.promise.PromiseImpl<T>
-
- org.osgi.util.promise.DeferredPromiseImpl<T>
-
- Type Parameters:
T
- The result type associated with the Promise.
- All Implemented Interfaces:
Promise<T>
final class DeferredPromiseImpl<T> extends PromiseImpl<T>
Deferred Promise implementation.This class is not used directly by clients. Clients should use
PromiseFactory.deferred()
to create aDeferred
which can be used to obtain a Promise whose resolution can be deferred.- Since:
- 1.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DeferredPromiseImpl.Chain
A callback used to resolve the chained Promise when the Promise is resolved.private class
DeferredPromiseImpl.ChainImpl
A callback used to resolve the chained Promise when the PromiseImpl is resolved.(package private) class
DeferredPromiseImpl.Delay
A callback used by thePromiseImpl.delay(long)
method to delay chaining a promise.private class
DeferredPromiseImpl.FallbackChain
A callback used to resolve the chained Promise when the fallback Promise is resolved.(package private) class
DeferredPromiseImpl.FallbackTo
A callback used by thePromiseImpl.fallbackTo(Promise)
method.(package private) class
DeferredPromiseImpl.Filter
A callback used by thePromiseImpl.filter(Predicate)
method.(package private) class
DeferredPromiseImpl.FlatMap<P>
A callback used by thePromiseImpl.flatMap(Function)
method.(package private) class
DeferredPromiseImpl.Map<P>
A callback used by thePromiseImpl.map(Function)
method.(package private) class
DeferredPromiseImpl.Recover
A callback used by thePromiseImpl.recover(Function)
method.(package private) class
DeferredPromiseImpl.RecoverWith
A callback used by thePromiseImpl.recoverWith(Function)
method.private class
DeferredPromiseImpl.ResolveWith<P>
A callback used to resolve a Promise with another Promise for theresolveWith(Promise)
method.(package private) class
DeferredPromiseImpl.Submit
A callback used by thePromiseFactory.submit(Callable)
method.(package private) class
DeferredPromiseImpl.Then<P>
A callback used to chain promises for thePromiseImpl.then(Success, Failure)
method.(package private) class
DeferredPromiseImpl.ThenAccept
A callback used by thePromiseImpl.thenAccept(Consumer)
method.(package private) class
DeferredPromiseImpl.Timeout
A 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
Fields Modifier and Type Field Description private java.lang.Throwable
fail
The failure of this Promise if resolved with a failure ornull
if successfully resolved.private java.util.concurrent.CountDownLatch
resolved
A CountDownLatch to manage the resolved state of this Promise.private T
value
The value of this Promise if successfully resolved.
-
Constructor Summary
Constructors Constructor Description DeferredPromiseImpl(PromiseFactory factory)
Initialize this Promise.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) PromiseImpl.Result<T>
collect()
Return a holder of the result of this PromiseImpl.java.lang.Throwable
getFailure()
Returns the failure of this Promise.T
getValue()
Returns the value of this Promise.boolean
isDone()
Returns whether this Promise has been resolved.(package private) PromiseImpl<T>
orDone()
Return a resolved PromiseImpl if this DeferredPromiseImpl is resolved.(package private) void
resolve(T v, java.lang.Throwable f)
Resolve this Promise.(package private) Promise<java.lang.Void>
resolveWith(Promise<? extends T> with)
Resolve this Promise with the specified Promise.java.lang.String
toString()
(package private) boolean
tryResolve(T v, java.lang.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 Detail
-
resolved
private final java.util.concurrent.CountDownLatch 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,
value
andfail
, 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
private T value
The value of this Promise if successfully resolved.- See Also:
PromiseImpl.<V>resolved(V)
-
fail
private java.lang.Throwable fail
The failure of this Promise if resolved with a failure ornull
if successfully resolved.- See Also:
PromiseImpl.<V>resolved(V)
-
-
Constructor Detail
-
DeferredPromiseImpl
DeferredPromiseImpl(PromiseFactory factory)
Initialize this Promise.- Parameters:
factory
- The factory to use for callbacks and scheduled operations.
-
-
Method Detail
-
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.
-
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
public T getValue() throws java.lang.reflect.InvocationTargetException, java.lang.InterruptedException
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.
- Throws:
java.lang.reflect.InvocationTargetException
- If this Promise was resolved with a failure. The cause of theInvocationTargetException
is the failure exception.java.lang.InterruptedException
- If the current thread was interrupted while waiting.
-
getFailure
public java.lang.Throwable getFailure() throws java.lang.InterruptedException
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. - Throws:
java.lang.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:
collect
in classPromiseImpl<T>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
tryResolve
boolean tryResolve(T v, java.lang.Throwable f)
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
void resolve(T v, java.lang.Throwable f)
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:
java.lang.IllegalStateException
- If this Promise was already resolved.
-
resolveWith
Promise<java.lang.Void> resolveWith(Promise<? extends T> with)
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.
-
-