Class PromiseImpl<T>
- java.lang.Object
-
- org.osgi.util.promise.PromiseImpl<T>
-
- Type Parameters:
T
- The result type associated with the Promise.
- All Implemented Interfaces:
Promise<T>
- Direct Known Subclasses:
DeferredPromiseImpl
,FailedPromiseImpl
,ResolvedPromiseImpl
abstract class PromiseImpl<T> extends java.lang.Object implements Promise<T>
Abstract Promise implementation.This class is not used directly by clients. Clients should use
PromiseFactory
to create aPromise
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
PromiseImpl.OnFailure
A callback used for theonFailure(Consumer)
method.private class
PromiseImpl.OnSuccess
A callback used for theonSuccess(Consumer)
method.(package private) static class
PromiseImpl.Result<P>
A holder of the result of a Promise.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentLinkedQueue<java.lang.Runnable>
callbacks
A ConcurrentLinkedQueue to hold the callbacks for this Promise, so no additional synchronization is required to write to or read from the queue.private PromiseFactory
factory
The factory to use for callbacks and scheduled operations.
-
Constructor Summary
Constructors Constructor Description PromiseImpl(PromiseFactory factory)
Initialize this Promise.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) static <V> void
chain(Promise<V> promise, java.lang.Runnable chain)
Run the specified chain when the specified promise is resolved.(package private) abstract PromiseImpl.Result<T>
collect()
Return a holder of the result of this PromiseImpl.(package private) static <R> PromiseImpl.Result<R>
collect(Promise<? extends R> promise)
Return a holder of the result of the specified Promise.(package private) <V> DeferredPromiseImpl<V>
deferred()
Return a newDeferredPromiseImpl
using thePromiseFactory
of this PromiseImpl.Promise<T>
delay(long millis)
Delay after the resolution of this Promise.(package private) <V> FailedPromiseImpl<V>
failed(java.lang.Throwable f)
Return a newFailedPromiseImpl
using thePromiseFactory
of this PromiseImpl.Promise<T>
fallbackTo(Promise<? extends T> fallback)
Fall back to the value of the specified Promise if this Promise fails.Promise<T>
filter(Predicate<? super T> predicate)
Filter the value of this Promise.<R> Promise<R>
flatMap(Function<? super T,Promise<? extends R>> mapper)
FlatMap the value of this Promise.<R> Promise<R>
map(Function<? super T,? extends R> mapper)
Map the value of this Promise.(package private) void
notifyCallbacks()
Call any registered callbacks if this Promise is resolved.Promise<T>
onFailure(Consumer<? super java.lang.Throwable> failure)
Register a callback to be called with the failure for this Promise when this Promise is resolved with a failure.Promise<T>
onResolve(java.lang.Runnable callback)
Register a callback to be called when this Promise is resolved.Promise<T>
onSuccess(Consumer<? super T> success)
Register a callback to be called with the result of this Promise when this Promise is resolved successfully.Promise<T>
recover(Function<Promise<?>,? extends T> recovery)
Recover from a failure of this Promise with a recovery value.Promise<T>
recoverWith(Function<Promise<?>,Promise<? extends T>> recovery)
Recover from a failure of this Promise with a recovery Promise.(package private) <V> ResolvedPromiseImpl<V>
resolved(V v)
Return a newResolvedPromiseImpl
using thePromiseFactory
of this PromiseImpl.(package private) java.util.concurrent.ScheduledFuture<?>
schedule(java.lang.Runnable operation, long delay, java.util.concurrent.TimeUnit unit)
Schedule a operation on the scheduled executor.<R> Promise<R>
then(Success<? super T,? extends R> success)
Chain a new Promise to this Promise with a Success callback.<R> Promise<R>
then(Success<? super T,? extends R> success, Failure failure)
Chain a new Promise to this Promise with Success and Failure callbacks.Promise<T>
thenAccept(Consumer<? super T> consumer)
Chain a new Promise to this Promise with a Consumer callback that receives the value of this Promise when it is successfully resolved.Promise<T>
timeout(long millis)
Time out the resolution of this Promise.(package private) static void
uncaughtException(java.lang.Throwable t)
Handle an uncaught exception from a Runnable.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.osgi.util.promise.Promise
getFailure, getValue, isDone
-
-
-
-
Field Detail
-
factory
private final PromiseFactory factory
The factory to use for callbacks and scheduled operations.
-
callbacks
private final java.util.concurrent.ConcurrentLinkedQueue<java.lang.Runnable> callbacks
A ConcurrentLinkedQueue to hold the callbacks for this Promise, so no additional synchronization is required to write to or read from the queue.
-
-
Constructor Detail
-
PromiseImpl
PromiseImpl(PromiseFactory factory)
Initialize this Promise.- Parameters:
factory
- The factory to use for callbacks and scheduled operations.
-
-
Method Detail
-
deferred
<V> DeferredPromiseImpl<V> deferred()
Return a newDeferredPromiseImpl
using thePromiseFactory
of this PromiseImpl.- Returns:
- A new DeferredPromiseImpl.
- Since:
- 1.1
-
resolved
<V> ResolvedPromiseImpl<V> resolved(V v)
Return a newResolvedPromiseImpl
using thePromiseFactory
of this PromiseImpl.- Parameters:
v
- Value for the ResolvedPromiseImpl.- Returns:
- A new ResolvedPromiseImpl.
- Since:
- 1.1
-
failed
<V> FailedPromiseImpl<V> failed(java.lang.Throwable f)
Return a newFailedPromiseImpl
using thePromiseFactory
of this PromiseImpl.- Parameters:
f
- Failure for the FailedPromiseImpl.- Returns:
- A new FailedPromiseImpl.
- Since:
- 1.1
-
onResolve
public Promise<T> onResolve(java.lang.Runnable callback)
Register a callback to be called when this Promise is resolved.The specified callback is called when this Promise is resolved either successfully or with a failure.
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.
-
notifyCallbacks
void notifyCallbacks()
Call any registered callbacks if this Promise is resolved.
-
schedule
java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable operation, long delay, java.util.concurrent.TimeUnit unit)
Schedule a operation on the scheduled executor.- Since:
- 1.1
-
uncaughtException
static void uncaughtException(java.lang.Throwable t)
Handle an uncaught exception from a Runnable.- Parameters:
t
- The uncaught exception.- Since:
- 1.1
-
chain
static <V> void chain(Promise<V> promise, java.lang.Runnable chain)
Run the specified chain when the specified promise is resolved.- Parameters:
promise
- The promise associated with the chain.chain
- The chain to run when the promise is resolved.
-
onSuccess
public Promise<T> onSuccess(Consumer<? super T> success)
Register a callback to be called with the result of this Promise when this Promise is resolved successfully. The callback will not be called if this Promise is resolved with a failure.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.
-
onFailure
public Promise<T> onFailure(Consumer<? super java.lang.Throwable> failure)
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
public <R> Promise<R> then(Success<? super T,? extends R> success, Failure failure)
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>
- 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.
-
then
public <R> Promise<R> then(Success<? super T,? extends R> success)
Chain a new Promise to this Promise with a Success callback.This method performs the same function as calling
Promise.then(Success, Failure)
with the specified Success callback andnull
for the Failure callback.- Specified by:
then
in interfacePromise<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.- 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, if any, is executed.
- See Also:
Promise.then(Success, Failure)
-
thenAccept
public Promise<T> thenAccept(Consumer<? super T> consumer)
Chain a new Promise to this Promise with a Consumer callback that receives the value of this Promise when it is successfully resolved.The specified
Consumer
is called when this Promise is resolved successfully.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 callback is executed. If the callback throws an exception, the returned Promise is failed with that exception. Otherwise the returned Promise is resolved with the success value from this Promise.
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:
thenAccept
in interfacePromise<T>
- Parameters:
consumer
- The Consumer callback that receives the value of this Promise. Must not benull
.- Returns:
- A new Promise which is chained to this Promise. The returned Promise must be resolved when this Promise is resolved after the specified Consumer is executed.
-
filter
public Promise<T> filter(Predicate<? super T> predicate)
Filter the value of this Promise.If this Promise is successfully resolved, the returned Promise must either be resolved with the value of this Promise, if the specified Predicate accepts that value, or failed with a
NoSuchElementException
, if the specified Predicate does not accept that value. If the specified Predicate throws an exception, the returned Promise must be failed with the exception.If this Promise is resolved with a failure, the returned Promise must be failed with that failure.
This method may be called at any time including before and after this Promise has been resolved.
-
map
public <R> Promise<R> map(Function<? super T,? extends R> mapper)
Map the value of this Promise.If this Promise is successfully resolved, the returned Promise must be resolved with the value of specified Function as applied to the value of this Promise. If the specified Function throws an exception, the returned Promise must be failed with the exception.
If this Promise is resolved with a failure, the returned Promise must be failed with that failure.
This method may be called at any time including before and after this Promise has been resolved.
- Specified by:
map
in interfacePromise<T>
- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
mapper
- The Function that must map the value of this Promise to the value that must be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that returns the value of this Promise as mapped by the specified Function.
-
flatMap
public <R> Promise<R> flatMap(Function<? super T,Promise<? extends R>> mapper)
FlatMap the value of this Promise.If this Promise is successfully resolved, the returned Promise must be resolved with the Promise from the specified Function as applied to the value of this Promise. If the specified Function throws an exception, the returned Promise must be failed with the exception.
If this Promise is resolved with a failure, the returned Promise must be failed with that failure.
This method may be called at any time including before and after this Promise has been resolved.
- Specified by:
flatMap
in interfacePromise<T>
- Type Parameters:
R
- The value type associated with the returned Promise.- Parameters:
mapper
- The Function that must flatMap the value of this Promise to a Promise that must be used to resolve the returned Promise. Must not benull
.- Returns:
- A Promise that returns the value of this Promise as mapped by the specified Function.
-
recover
public Promise<T> recover(Function<Promise<?>,? extends T> recovery)
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>
- 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
public Promise<T> recoverWith(Function<Promise<?>,Promise<? extends T>> recovery)
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>
- 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
public Promise<T> fallbackTo(Promise<? extends T> fallback)
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>
- 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
public Promise<T> timeout(long millis)
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
.
-
delay
public Promise<T> delay(long millis)
Delay after the resolution of this Promise.Once this Promise is resolved, resolve the returned Promise with this Promise after the specified delay.
-
collect
abstract PromiseImpl.Result<T> collect()
Return a holder of the result of this PromiseImpl.- Since:
- 1.1
-
collect
static <R> PromiseImpl.Result<R> collect(Promise<? extends R> promise)
Return a holder of the result of the specified Promise.- Since:
- 1.1
-
-