com.atlassian.util.concurrent
Class Promises

java.lang.Object
  extended by com.atlassian.util.concurrent.Promises

@Beta
public final class Promises
extends java.lang.Object

Library of utility Promise functions

Since:
2.4

Method Summary
static
<A> Promise<A>
forFuture(java.util.concurrent.Future<A> future)
          Creates a promise from the given future.
static
<A> Promise<A>
forListenableFuture(com.google.common.util.concurrent.ListenableFuture<A> future)
          Creates a promise from the given future.
static
<A> com.google.common.util.concurrent.FutureCallback<A>
futureCallback(Effect<A> success, Effect<java.lang.Throwable> failure)
          Create a FutureCallback by composing two Effects.
static
<A> com.google.common.util.concurrent.FutureCallback<A>
onFailureDo(Effect<java.lang.Throwable> effect)
          Create a FutureCallback from an Effect to be run if there is a failure.
static
<A> com.google.common.util.concurrent.FutureCallback<A>
onSuccessDo(Effect<A> effect)
          Create a FutureCallback from an Effect to be run if there is a success.
static
<A> Promise<A>
promise(A value)
          Creates a new, resolved promise for the specified concrete value.
static Effect<java.lang.Throwable> reject(com.google.common.util.concurrent.SettableFuture<?> delegate)
          Creates a new Effect that forwards a promise's fail events to the specified future delegate's SettableFuture.setException(Throwable) method -- that is, the new callback rejects the delegate future if invoked.
static
<A> Promise<A>
rejected(java.lang.Throwable throwable, java.lang.Class<A> resultType)
          Creates a new, rejected promise from the given Throwable and result type.
static
<A> Promise<A>
toRejectedPromise(java.lang.Throwable t, java.lang.Class<A> resultType)
          Creates a new, rejected promise from the given Throwable and result type.
static
<A> Promise<A>
toResolvedPromise(A value)
          Creates a new, resolved promise for the specified concrete value.
static
<A> Promise<java.util.List<A>>
when(java.lang.Iterable<? extends Promise<? extends A>> promises)
          Returns a new Promise representing the status of a list of other promises.
static
<A> Promise<java.util.List<A>>
when(Promise<? extends A>... promises)
          Returns a new Promise representing the status of a list of other promises.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

when

public static <A> Promise<java.util.List<A>> when(Promise<? extends A>... promises)
Returns a new Promise representing the status of a list of other promises.

Parameters:
promises - The promises that the new promise should track
Returns:
The new, aggregate promise

when

public static <A> Promise<java.util.List<A>> when(java.lang.Iterable<? extends Promise<? extends A>> promises)
Returns a new Promise representing the status of a list of other promises. More generally this is known as {code}sequence{code} as both List and Promise are traversable monads.

Parameters:
promises - The promises that the new promise should track
Returns:
The new, aggregate promise

promise

public static <A> Promise<A> promise(A value)
Creates a new, resolved promise for the specified concrete value.

Parameters:
value - The value for which a promise should be created
Returns:
The new promise

toResolvedPromise

public static <A> Promise<A> toResolvedPromise(A value)
Creates a new, resolved promise for the specified concrete value.

Synonym for promise(Object).

Parameters:
value - The value for which a promise should be created
Returns:
The new promise

rejected

public static <A> Promise<A> rejected(java.lang.Throwable throwable,
                                      java.lang.Class<A> resultType)
Creates a new, rejected promise from the given Throwable and result type.

Parameters:
throwable - The throwable
resultType - The result type
Returns:
The new promise

toRejectedPromise

public static <A> Promise<A> toRejectedPromise(java.lang.Throwable t,
                                               java.lang.Class<A> resultType)
Creates a new, rejected promise from the given Throwable and result type.

Synonym for rejected(Throwable, Class)

Parameters:
t - The throwable
resultType - The result type
Returns:
The new promise

forListenableFuture

public static <A> Promise<A> forListenableFuture(com.google.common.util.concurrent.ListenableFuture<A> future)
Creates a promise from the given future.

Parameters:
future - The future delegate for the new promise
Returns:
The new promise

forFuture

public static <A> Promise<A> forFuture(java.util.concurrent.Future<A> future)
Creates a promise from the given future.

Parameters:
future - The future delegate for the new promise
Returns:
The new promise

reject

public static Effect<java.lang.Throwable> reject(com.google.common.util.concurrent.SettableFuture<?> delegate)
Creates a new Effect that forwards a promise's fail events to the specified future delegate's SettableFuture.setException(Throwable) method -- that is, the new callback rejects the delegate future if invoked.

Parameters:
delegate - The future to be rejected on a fail event
Returns:
The fail callback

futureCallback

public static <A> com.google.common.util.concurrent.FutureCallback<A> futureCallback(Effect<A> success,
                                                                                     Effect<java.lang.Throwable> failure)
Create a FutureCallback by composing two Effects.

Parameters:
success - To run if the Future is successful
failure - To run if the Future fails
Returns:
The composed futureCallback

onSuccessDo

public static <A> com.google.common.util.concurrent.FutureCallback<A> onSuccessDo(Effect<A> effect)
Create a FutureCallback from an Effect to be run if there is a success.

Parameters:
effect - To be passed the produced value if it happens
Returns:
The FutureCallback with a no-op onFailure

onFailureDo

public static <A> com.google.common.util.concurrent.FutureCallback<A> onFailureDo(Effect<java.lang.Throwable> effect)
Create a FutureCallback from an Effect to be run if there is a failure.

Parameters:
effect - To be passed an exception if it happens
Returns:
The FutureCallback with a no-op onSuccess


Copyright © 2014 Atlassian. All Rights Reserved.