public abstract class RFuture<T> extends Reactor
map(react.Function<? super T, R>)
or flatMap(react.Function<? super T, react.RFuture<R>>)
it, and listen for success or failure via the
success(T)
and failure(java.lang.Throwable)
signals.
The benefit over just using Callback
is that results can be composed. You can
subscribe to an object, flatmap the result into a service call on that object which returns the
address of another object, flat map that into a request to subscribe to that object, and finally
pass the resulting object to some other code via a slot. Failure can be handled once for all of
these operations and you avoid nesting yourself three callbacks deep.
Modifier and Type | Class and Description |
---|---|
static class |
RFuture.T2<A,B>
Used by
sequence(RFuture,RFuture) . |
static class |
RFuture.T3<A,B,C>
Used by
sequence(RFuture,RFuture,RFuture) . |
Reactor.RListener
Constructor and Description |
---|
RFuture() |
Modifier and Type | Method and Description |
---|---|
RFuture<T> |
bindComplete(SignalView.Listener<Boolean> slot)
Convenience method to
ValueView.connectNotify(react.ValueView.Listener<? super T>) slot to isComplete() . |
static <T> RFuture<Collection<T>> |
collect(Collection<? extends RFuture<T>> futures)
Returns a future containing a list of all success results from
futures . |
static <T> RFuture<T> |
failure(Throwable cause)
Returns a future with a pre-existing failure value.
|
<R> RFuture<R> |
flatMap(Function<? super T,RFuture<R>> func)
Maps a successful result to a new result using
func when it arrives. |
ValueView<Boolean> |
isComplete()
Returns a value that indicates whether this future has completed.
|
boolean |
isCompleteNow()
Returns whether this future is complete right now.
|
<R> RFuture<R> |
map(Function<? super T,R> func)
Maps the value of a successful result using
func upon arrival. |
RFuture<T> |
onComplete(SignalView.Listener<? super Try<T>> slot)
Causes
slot to be notified when this future is completed. |
RFuture<T> |
onFailure(SignalView.Listener<? super Throwable> slot)
Causes
slot to be notified if/when this future is completed with failure. |
RFuture<T> |
onSuccess(SignalView.Listener<? super T> slot)
Causes
slot to be notified if/when this future is completed with success. |
RFuture<T> |
recover(Function<? super Throwable,T> func)
Maps the value of a failed result using
func upon arrival. |
abstract Try<T> |
result()
Returns the result of this future, or null if it is not yet complete.
|
static <T> RFuture<T> |
result(Try<T> result)
Returns a future with an already-computed result.
|
static <T> RFuture<List<T>> |
sequence(Collection<? extends RFuture<T>> futures)
Returns a future containing a list of all success results from
futures if all of
the futures complete successfully, or a MultiFailureException aggregating all
failures, if any of the futures fails. |
static <A,B> RFuture<RFuture.T2<A,B>> |
sequence(RFuture<A> a,
RFuture<B> b)
Returns a future containing the results of
a and b if both futures complete
successfully, or a MultiFailureException aggregating all failures, if either of the
futures fails. |
static <A,B,C> RFuture<RFuture.T3<A,B,C>> |
sequence(RFuture<A> a,
RFuture<B> b,
RFuture<C> c)
Returns a future containing the results of
a , b , and c if all
futures complete successfully, or a MultiFailureException aggregating all failures,
if any of the futures fails. |
static RFuture<Void> |
success()
Returns a future result for a
Void method. |
static <T> RFuture<T> |
success(T value)
Returns a future with a pre-existing success value.
|
<R> RFuture<R> |
transform(Function<Try<? super T>,Try<R>> func)
Transforms this future by mapping its result upon arrival.
|
clearConnections, hasConnections
public static <T> RFuture<T> success(T value)
public static <T> RFuture<T> failure(Throwable cause)
public static <T> RFuture<T> result(Try<T> result)
public static <T> RFuture<List<T>> sequence(Collection<? extends RFuture<T>> futures)
futures
if all of
the futures complete successfully, or a MultiFailureException
aggregating all
failures, if any of the futures fails.
If futures
is an ordered collection, the resulting list will match the order of
the futures. If not, result list is in futures
' iteration order.
public static <A,B> RFuture<RFuture.T2<A,B>> sequence(RFuture<A> a, RFuture<B> b)
a
and b
if both futures complete
successfully, or a MultiFailureException
aggregating all failures, if either of the
futures fails.public static <A,B,C> RFuture<RFuture.T3<A,B,C>> sequence(RFuture<A> a, RFuture<B> b, RFuture<C> c)
a
, b
, and c
if all
futures complete successfully, or a MultiFailureException
aggregating all failures,
if any of the futures fails.public static <T> RFuture<Collection<T>> collect(Collection<? extends RFuture<T>> futures)
futures
. Any failure
results are simply omitted from the list. The success results are also in no particular
order. If all of futures
fail, the resulting list will be empty.public RFuture<T> onSuccess(SignalView.Listener<? super T> slot)
slot
to be notified if/when this future is completed with success. If it has
already succeeded, the slot will be notified immediately.public RFuture<T> onFailure(SignalView.Listener<? super Throwable> slot)
slot
to be notified if/when this future is completed with failure. If it has
already failed, the slot will be notified immediately.public RFuture<T> onComplete(SignalView.Listener<? super Try<T>> slot)
slot
to be notified when this future is completed. If it has already
completed, the slot will be notified immediately.public ValueView<Boolean> isComplete()
public boolean isCompleteNow()
isComplete()
to return a reactive view of completeness.public RFuture<T> bindComplete(SignalView.Listener<Boolean> slot)
ValueView.connectNotify(react.ValueView.Listener<? super T>)
slot
to isComplete()
.
This is useful for binding the disabled state of UI elements to this future's completeness
(i.e. disabled while the future is incomplete, then reenabled when it is completed).public <R> RFuture<R> transform(Function<Try<? super T>,Try<R>> func)
public <R> RFuture<R> map(Function<? super T,R> func)
func
upon arrival.public RFuture<T> recover(Function<? super Throwable,T> func)
func
upon arrival. Ideally one could
generalize the type T
here but Java doesn't allow type parameters with lower
bounds.public <R> RFuture<R> flatMap(Function<? super T,RFuture<R>> func)
func
when it arrives. Failure on the
original result or the mapped result are both dispatched to the mapped result. This is
useful for chaining asynchronous actions. It's also known as monadic bind.public abstract Try<T> result()
NOTE: don't use this method! You should wire up reactions to the completion of
this future via onSuccess(react.SignalView.Listener<? super T>)
or onFailure(react.SignalView.Listener<? super java.lang.Throwable>)
. React is not a blocking async
library where on might block a calling thread on the result of a future and then obtain the
result synchronously. This is only appropriate when you're trying to abstract over
synchronous and asynchronous variants of a computation, and you want to use the future
machinery in both cases, but in the synchronous case you know that your future will be
complete by the time you want to obtain its result.
Copyright © 2017. All rights reserved.