play.libs
Class F.Promise<A>

java.lang.Object
  extended by play.libs.F.Promise<A>
Direct Known Subclasses:
F.RedeemablePromise
Enclosing class:
F

public static class F.Promise<A>
extends java.lang.Object

A promise to produce a result of type A.


Constructor Summary
F.Promise(scala.concurrent.Future<A> future)
          Deprecated. Since 2.2. Use wrap(Future) instead.
 
Method Summary
static
<A> F.Promise<A>
delayed(F.Function0<A> function, long delay, java.util.concurrent.TimeUnit unit)
          Create a Promise which, after a delay, will be redeemed with the result of a given function.
static
<A> F.Promise<A>
delayed(F.Function0<A> function, long delay, java.util.concurrent.TimeUnit unit, scala.concurrent.ExecutionContext ec)
          Create a Promise which, after a delay, will be redeemed with the result of a given function.
 F.Promise<A> fallbackTo(F.Promise<A> fallback)
          Creates a new promise which holds the result of this promise if it was completed successfully, otherwise the result of the fallback promise if it completed successfully.
 F.Promise<A> filter(F.Predicate<? super A> predicate)
          Creates a new promise by filtering the value of the current promise with a predicate.
 F.Promise<A> filter(F.Predicate<? super A> predicate, scala.concurrent.ExecutionContext ec)
          Creates a new promise by filtering the value of the current promise with a predicate.
<B> F.Promise<B>
flatMap(F.Function<? super A,F.Promise<B>> function)
          Maps the result of this promise to a promise for a result of type B, and flattens that to be a single promise for B.
<B> F.Promise<B>
flatMap(F.Function<? super A,F.Promise<B>> function, scala.concurrent.ExecutionContext ec)
          Maps the result of this promise to a promise for a result of type B, and flattens that to be a single promise for B.
 A get(long timeout)
          Awaits for the promise to get the result.
Throws a Throwable if the calculation providing the promise threw an exception
 A get(long timeout, java.util.concurrent.TimeUnit unit)
          Awaits for the promise to get the result.
Throws a Throwable if the calculation providing the promise threw an exception
<B> F.Promise<B>
map(F.Function<? super A,B> function)
          Maps this promise to a promise of type B.
<B> F.Promise<B>
map(F.Function<? super A,B> function, scala.concurrent.ExecutionContext ec)
          Maps this promise to a promise of type B.
 void onFailure(F.Callback<java.lang.Throwable> action)
          Perform the given action callback if the promise encounters an exception.
 void onFailure(F.Callback<java.lang.Throwable> action, scala.concurrent.ExecutionContext ec)
          Perform the given action callback if the promise encounters an exception.
 void onRedeem(F.Callback<A> action)
          Perform the given action callback when the Promise is redeemed.
 void onRedeem(F.Callback<A> action, scala.concurrent.ExecutionContext ec)
          Perform the given action callback when the Promise is redeemed.
<B> F.Promise<F.Either<A,B>>
or(F.Promise<B> another)
          combines the current promise with another promise using `or`
static
<A> F.Promise<A>
promise(F.Function0<A> function)
          Create a Promise which will be redeemed with the result of a given function.
static
<A> F.Promise<A>
promise(F.Function0<A> function, scala.concurrent.ExecutionContext ec)
          Create a Promise which will be redeemed with the result of a given Function0.
static
<A> F.Promise<A>
pure(A a)
          Create a new pure promise, that is, a promise with a constant value from the start.
 F.Promise<A> recover(F.Function<java.lang.Throwable,A> function)
          Wraps this promise in a promise that will handle exceptions thrown by this Promise.
 F.Promise<A> recover(F.Function<java.lang.Throwable,A> function, scala.concurrent.ExecutionContext ec)
          Wraps this promise in a promise that will handle exceptions thrown by this Promise.
 F.Promise<A> recoverWith(F.Function<java.lang.Throwable,F.Promise<A>> function)
          Creates a new promise that will handle thrown exceptions by assigning to the value of another promise.
 F.Promise<A> recoverWith(F.Function<java.lang.Throwable,F.Promise<A>> function, scala.concurrent.ExecutionContext ec)
          Creates a new promise that will handle thrown exceptions by assigning to the value of another promise.
static
<A> F.Promise<java.util.List<A>>
sequence(scala.concurrent.ExecutionContext ec, F.Promise<A>... promises)
          Combine the given promises into a single promise for the list of results.
static
<A> F.Promise<java.util.List<A>>
sequence(F.Promise<A>... promises)
          Combine the given promises into a single promise for the list of results.
static
<A> F.Promise<java.util.List<A>>
sequence(java.lang.Iterable<F.Promise<A>> promises)
          Combine the given promises into a single promise for the list of results.
static
<A> F.Promise<java.util.List<A>>
sequence(java.lang.Iterable<F.Promise<A>> promises, scala.concurrent.ExecutionContext ec)
          Combine the given promises into a single promise for the list of results.
static
<A> F.Promise<A>
throwing(java.lang.Throwable throwable)
          Create a new promise throwing an exception.
static
<A> F.Promise<A>
timeout(A message, long delay)
          Create a Promise that is redeemed after a timeout.
static
<A> F.Promise<A>
timeout(A message, long delay, java.util.concurrent.TimeUnit unit)
          Create a Promise that is redeemed after a timeout.
static
<A> F.Promise<scala.Unit>
timeout(long delay)
          Create a Promise timer that throws a TimeoutException after a given timeout.
static
<A> F.Promise<scala.Unit>
timeout(long delay, java.util.concurrent.TimeUnit unit)
          Create a Promise timer that throws a TimeoutException after a given timeout.
<B> F.Promise<B>
transform(F.Function<? super A,B> onSuccess, F.Function<java.lang.Throwable,java.lang.Throwable> onFailure)
          Creates a new promise by applying the onSuccess function to a successful result, or the onFailure function to a failed result.
<B> F.Promise<B>
transform(F.Function<? super A,B> onSuccess, F.Function<java.lang.Throwable,java.lang.Throwable> onFailure, scala.concurrent.ExecutionContext ec)
          Creates a new promise by applying the onSuccess function to a successful result, or the onFailure function to a failed result.
static
<A> F.Promise<A>
wrap(scala.concurrent.Future<A> future)
          Creates a Promise that wraps a Scala Future.
 scala.concurrent.Future<A> wrapped()
          Gets the Scala Future wrapped by this Promise.
<B> F.Promise<F.Tuple<A,B>>
zip(F.Promise<B> another)
          Zips the values of this promise with another, and creates a new promise holding the tuple of their results
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

F.Promise

@Deprecated
public F.Promise(scala.concurrent.Future<A> future)
Deprecated. Since 2.2. Use wrap(Future) instead.

Creates a Promise that wraps a Scala Future.

Parameters:
future - The Scala Future to wrap
Method Detail

wrap

public static <A> F.Promise<A> wrap(scala.concurrent.Future<A> future)
Creates a Promise that wraps a Scala Future.

Parameters:
future - The Scala Future to wrap

sequence

public static <A> F.Promise<java.util.List<A>> sequence(F.Promise<A>... promises)
Combine the given promises into a single promise for the list of results. The sequencing operations are performed in the default ExecutionContext.

Parameters:
promises - The promises to combine
Returns:
A single promise whose methods act on the list of redeemed promises

sequence

public static <A> F.Promise<java.util.List<A>> sequence(scala.concurrent.ExecutionContext ec,
                                                        F.Promise<A>... promises)
Combine the given promises into a single promise for the list of results.

Parameters:
ec - Used to execute the sequencing operations.
promises - The promises to combine
Returns:
A single promise whose methods act on the list of redeemed promises

timeout

public static <A> F.Promise<A> timeout(A message,
                                       long delay,
                                       java.util.concurrent.TimeUnit unit)
Create a Promise that is redeemed after a timeout.

Parameters:
message - The message to use to redeem the Promise.
delay - The delay (expressed with the corresponding unit).
unit - The Unit.

timeout

public static <A> F.Promise<A> timeout(A message,
                                       long delay)
Create a Promise that is redeemed after a timeout.

Parameters:
message - The message to use to redeem the Promise.
delay - The delay expressed in milliseconds.

timeout

public static <A> F.Promise<scala.Unit> timeout(long delay)
Create a Promise timer that throws a TimeoutException after a given timeout. The returned Promise is usually combined with other Promises.

Parameters:
delay - The delay expressed in milliseconds.
Returns:
a promise without a real value

timeout

public static <A> F.Promise<scala.Unit> timeout(long delay,
                                                java.util.concurrent.TimeUnit unit)
Create a Promise timer that throws a TimeoutException after a given timeout. The returned Promise is usually combined with other Promises.

Parameters:
delay - The delay (expressed with the corresponding unit).
unit - The Unit.
Returns:
a promise without a real value

sequence

public static <A> F.Promise<java.util.List<A>> sequence(java.lang.Iterable<F.Promise<A>> promises)
Combine the given promises into a single promise for the list of results. The sequencing operations are performed in the default ExecutionContext.

Parameters:
promises - The promises to combine
Returns:
A single promise whose methods act on the list of redeemed promises

sequence

public static <A> F.Promise<java.util.List<A>> sequence(java.lang.Iterable<F.Promise<A>> promises,
                                                        scala.concurrent.ExecutionContext ec)
Combine the given promises into a single promise for the list of results.

Parameters:
promises - The promises to combine
ec - Used to execute the sequencing operations.
Returns:
A single promise whose methods act on the list of redeemed promises

pure

public static <A> F.Promise<A> pure(A a)
Create a new pure promise, that is, a promise with a constant value from the start.

Parameters:
a - the value for the promise

throwing

public static <A> F.Promise<A> throwing(java.lang.Throwable throwable)
Create a new promise throwing an exception.

Parameters:
throwable - Value to throw

promise

public static <A> F.Promise<A> promise(F.Function0<A> function)
Create a Promise which will be redeemed with the result of a given function. The Function0 will be run in the default ExecutionContext.

Parameters:
function - Used to fulfill the Promise.

promise

public static <A> F.Promise<A> promise(F.Function0<A> function,
                                       scala.concurrent.ExecutionContext ec)
Create a Promise which will be redeemed with the result of a given Function0.

Parameters:
function - Used to fulfill the Promise.
ec - The ExecutionContext to run the function in.

delayed

public static <A> F.Promise<A> delayed(F.Function0<A> function,
                                       long delay,
                                       java.util.concurrent.TimeUnit unit)
Create a Promise which, after a delay, will be redeemed with the result of a given function. The function will be called after the delay. The function will be run in the default ExecutionContext.

Parameters:
function - The function to call to fulfill the Promise.
delay - The time to wait.
unit - The units to use for the delay.

delayed

public static <A> F.Promise<A> delayed(F.Function0<A> function,
                                       long delay,
                                       java.util.concurrent.TimeUnit unit,
                                       scala.concurrent.ExecutionContext ec)
Create a Promise which, after a delay, will be redeemed with the result of a given function. The function will be called after the delay.

Parameters:
function - The function to call to fulfill the Promise.
delay - The time to wait.
unit - The units to use for the delay.
ec - The ExecutionContext to run the Function0 in.

get

public A get(long timeout,
             java.util.concurrent.TimeUnit unit)
Awaits for the promise to get the result.
Throws a Throwable if the calculation providing the promise threw an exception

Parameters:
timeout - A user defined timeout
unit - timeout for timeout
Returns:
The promised result

get

public A get(long timeout)
Awaits for the promise to get the result.
Throws a Throwable if the calculation providing the promise threw an exception

Parameters:
timeout - A user defined timeout in milliseconds
Returns:
The promised result

or

public <B> F.Promise<F.Either<A,B>> or(F.Promise<B> another)
combines the current promise with another promise using `or`

Parameters:
another -

onRedeem

public void onRedeem(F.Callback<A> action)
Perform the given action callback when the Promise is redeemed. The callback will be run in the default execution context.

Parameters:
action - The action to perform.

onRedeem

public void onRedeem(F.Callback<A> action,
                     scala.concurrent.ExecutionContext ec)
Perform the given action callback when the Promise is redeemed.

Parameters:
action - The action to perform.
ec - The ExecutionContext to execute the action in.

map

public <B> F.Promise<B> map(F.Function<? super A,B> function)
Maps this promise to a promise of type B. The function function is applied as soon as the promise is redeemed. The function will be run in the default execution context.

Parameters:
function - The function to map A to B.
Returns:
A wrapped promise that maps the type from A to B.

map

public <B> F.Promise<B> map(F.Function<? super A,B> function,
                            scala.concurrent.ExecutionContext ec)
Maps this promise to a promise of type B. The function function is applied as soon as the promise is redeemed.

Parameters:
function - The function to map A to B.
ec - The ExecutionContext to execute the function in.
Returns:
A wrapped promise that maps the type from A to B.

recover

public F.Promise<A> recover(F.Function<java.lang.Throwable,A> function)
Wraps this promise in a promise that will handle exceptions thrown by this Promise. The function will be run in the default execution context.

Parameters:
function - The function to handle the exception. This may, for example, convert the exception into something of type T, or it may throw another exception, or it may do some other handling.
Returns:
A wrapped promise that will only throw an exception if the supplied function throws an exception.

recover

public F.Promise<A> recover(F.Function<java.lang.Throwable,A> function,
                            scala.concurrent.ExecutionContext ec)
Wraps this promise in a promise that will handle exceptions thrown by this Promise.

Parameters:
function - The function to handle the exception. This may, for example, convert the exception into something of type T, or it may throw another exception, or it may do some other handling.
ec - The ExecutionContext to execute the function in.
Returns:
A wrapped promise that will only throw an exception if the supplied function throws an exception.

recoverWith

public F.Promise<A> recoverWith(F.Function<java.lang.Throwable,F.Promise<A>> function)
Creates a new promise that will handle thrown exceptions by assigning to the value of another promise. The function will be run in the default execution context.

Parameters:
function - The function to handle the exception, and which returns another promise
Returns:
A promise that will delegate to another promise on failure

recoverWith

public F.Promise<A> recoverWith(F.Function<java.lang.Throwable,F.Promise<A>> function,
                                scala.concurrent.ExecutionContext ec)
Creates a new promise that will handle thrown exceptions by assigning to the value of another promise.

Parameters:
function - The function to handle the exception, and which returns another promise
ec - The ExecutionContext to execute the function in
Returns:
A promise that will delegate to another promise on failure

fallbackTo

public F.Promise<A> fallbackTo(F.Promise<A> fallback)
Creates a new promise which holds the result of this promise if it was completed successfully, otherwise the result of the fallback promise if it completed successfully. If both promises failed, the resulting promise holds the throwable of this promise.

Parameters:
fallback - The promise to fallback to if this promise has failed
Returns:
A promise that will delegate to another promise on failure

onFailure

public void onFailure(F.Callback<java.lang.Throwable> action)
Perform the given action callback if the promise encounters an exception. This action will be run in the default exceution context.

Parameters:
action - The action to perform.

onFailure

public void onFailure(F.Callback<java.lang.Throwable> action,
                      scala.concurrent.ExecutionContext ec)
Perform the given action callback if the promise encounters an exception.

Parameters:
action - The action to perform.
ec - The ExecutionContext to execute the callback in.

flatMap

public <B> F.Promise<B> flatMap(F.Function<? super A,F.Promise<B>> function)
Maps the result of this promise to a promise for a result of type B, and flattens that to be a single promise for B. The function will be run in the default execution context.

Parameters:
function - The function to map A to a promise for B.
Returns:
A wrapped promise for a result of type B

flatMap

public <B> F.Promise<B> flatMap(F.Function<? super A,F.Promise<B>> function,
                                scala.concurrent.ExecutionContext ec)
Maps the result of this promise to a promise for a result of type B, and flattens that to be a single promise for B.

Parameters:
function - The function to map A to a promise for B.
ec - The ExecutionContext to execute the function in.
Returns:
A wrapped promise for a result of type B

filter

public F.Promise<A> filter(F.Predicate<? super A> predicate)
Creates a new promise by filtering the value of the current promise with a predicate. If the predicate fails, the resulting promise will fail with a `NoSuchElementException`.

Parameters:
predicate - The predicate to test the current value.
Returns:
A new promise with the current value, if the predicate is satisfied.

filter

public F.Promise<A> filter(F.Predicate<? super A> predicate,
                           scala.concurrent.ExecutionContext ec)
Creates a new promise by filtering the value of the current promise with a predicate. If the predicate fails, the resulting promise will fail with a `NoSuchElementException`.

Parameters:
predicate - The predicate to test the current value.
ec - The ExecutionContext to execute the filtering in.
Returns:
A new promise with the current value, if the predicate is satisfied.

transform

public <B> F.Promise<B> transform(F.Function<? super A,B> onSuccess,
                                  F.Function<java.lang.Throwable,java.lang.Throwable> onFailure)
Creates a new promise by applying the onSuccess function to a successful result, or the onFailure function to a failed result. The function will be run in the default execution context.

Parameters:
onSuccess - The function to map a successful result from A to B
onFailure - The function to map the Throwable when failed
Returns:
A new promise mapped by either the onSuccess or onFailure functions

transform

public <B> F.Promise<B> transform(F.Function<? super A,B> onSuccess,
                                  F.Function<java.lang.Throwable,java.lang.Throwable> onFailure,
                                  scala.concurrent.ExecutionContext ec)
Creates a new promise by applying the onSuccess function to a successful result, or the onFailure function to a failed result.

Parameters:
onSuccess - The function to map a successful result from A to B
onFailure - The function to map the Throwable when failed
ec - The ExecutionContext to execute functions in
Returns:
A new promise mapped by either the onSuccess or onFailure functions

zip

public <B> F.Promise<F.Tuple<A,B>> zip(F.Promise<B> another)
Zips the values of this promise with another, and creates a new promise holding the tuple of their results

Parameters:
another -

wrapped

public scala.concurrent.Future<A> wrapped()
Gets the Scala Future wrapped by this Promise.

Returns:
The Scala Future