Package com.tapkey.mobile.concurrent
Class PromiseSource<T>
- java.lang.Object
-
- com.tapkey.mobile.concurrent.PromiseSource<T>
-
- Type Parameters:
T
- The type of value represented by thePromise
produced by an instance of this class.
public class PromiseSource<T> extends Object
Instances of this class are used to create and completePromise
instances. Instances of this class are produced with a reference to anAsyncScheduler
, which is used to invoke continuations on the returnedPromise
. By default, the current scheduler is captured when calling the constructor.
-
-
Constructor Summary
Constructors Constructor Description PromiseSource()
Creates an instance based on the current schedule as returned by AsyncSchedulers#current().PromiseSource(AsyncScheduler scheduler)
Creates an instance based on the given scheduler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description net.tpky.mc.concurrent.AsyncStackTrace
getCallerStackTrace()
for internal usePromise<T>
getPromise()
Returns thePromise
associated with this instance.AsyncResult<? extends T>
getResult()
Returns the result this instance has been set to or null, if not in completed state yet.boolean
isCompleted()
Returns whether this instance and the returnedPromise
are in completed state, regardless of whether completed successfully or exceptionally.void
setAsyncResult(AsyncResult<? extends T> result)
Completes thePromise
associated with this instance with the givenAsyncResult
.void
setException(Exception e)
void
setResult(T result)
Completes thePromise
associated with this instance with the given value.boolean
trySetAsyncResult(AsyncResult<? extends T> result)
Tries to complete thePromise
associated with this instance with the givenAsyncResult
.boolean
trySetException(Exception e)
boolean
trySetResult(T result)
Tries to complete thePromise
associated with this instance with the given value.
-
-
-
Constructor Detail
-
PromiseSource
public PromiseSource()
Creates an instance based on the current schedule as returned by AsyncSchedulers#current().
-
PromiseSource
public PromiseSource(AsyncScheduler scheduler)
Creates an instance based on the given scheduler.- Parameters:
scheduler
-
-
-
Method Detail
-
getPromise
public Promise<T> getPromise()
Returns thePromise
associated with this instance.- Returns:
- the
Promise
associated with this instance.
-
isCompleted
public boolean isCompleted()
Returns whether this instance and the returnedPromise
are in completed state, regardless of whether completed successfully or exceptionally.- Returns:
- whether this instance and the returned
Promise
are in completed state.
-
setResult
public void setResult(T result)
Completes thePromise
associated with this instance with the given value. Throws if it has already been completed.- Parameters:
result
- the result value the associatedPromise
should be resolved to.- Throws:
IllegalStateException
- if already in completed state.
-
trySetResult
public boolean trySetResult(T result)
Tries to complete thePromise
associated with this instance with the given value. Ignores the given value if already in completed state.
-
setException
public void setException(Exception e)
Exceptionally completes thePromise
associated with this instance with the givenException
. Throws if already in completed state.- Parameters:
e
- theException
the associatedPromise
should be resolved to.- Throws:
IllegalStateException
- if already in completed state.
-
trySetException
public boolean trySetException(Exception e)
-
setAsyncResult
public void setAsyncResult(AsyncResult<? extends T> result)
Completes thePromise
associated with this instance with the givenAsyncResult
. Throws if it has already been completed.- Parameters:
result
- the async result the associatedPromise
should be resolved to.- Throws:
IllegalStateException
- if already in completed state.
-
trySetAsyncResult
public boolean trySetAsyncResult(AsyncResult<? extends T> result)
Tries to complete thePromise
associated with this instance with the givenAsyncResult
. Ignores the given value if already in completed state.
-
getCallerStackTrace
public net.tpky.mc.concurrent.AsyncStackTrace getCallerStackTrace()
for internal use
-
getResult
public AsyncResult<? extends T> getResult()
Returns the result this instance has been set to or null, if not in completed state yet.- Returns:
- the result this instance has been set to or null, if not in completed state yet.
-
-