|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.asyncobjects.Promise<T>
T
- a value type for promisepublic final class Promise<T>
A promise represents outcome of some asynchronous operation. The promise resolves to some value if operation is successful or is smashed with failure otherwise. An asychronous operation might never finish, in such case a promise stays unresolved forever.
A promise should be never directly accessed outside of vat where it were created. There are two indirect ways to access it from other vats:
The promise has three fundamental states:
UNRESOLVED
RESOLVED
AResolver.resolve(Object)
method has been called on promise resolver.
This is final state, and promise cannot change state after this state is
reached. The promise can be created in the resolved state using the
constructor Promise(Object)
or the method with(Object)
SMASHED
AResolver.smash(Throwable)
method has been called on promise
resolver. The promise could be also created in the smashed state using the
method smashed(Throwable)
.
Note if promise is resolved with implementation of AsyncObject interface for
which AsyncObject.isImmediate()
== false, it does not consider itself
resolved. Instead it invokes AsyncObject.dereference(AResolver)
method with resolver()
in order to receive actual resolution.
When promise is either resolved or smashed, it notifies downstream resolvers
that has been registered using dereference(AResolver)
method. After downstream resolver is registered, it is notified at most once.
If resolver is registered after promise is resolved or smashed, the
downstream resolver is notifed immediatly about outcome of operation.
Nested Class Summary | |
---|---|
class |
Promise.InternalPromiseResolverImpl
This is an internal class. |
Field Summary | |
---|---|
static int |
RESOLVED
resolved state |
static int |
SMASHED
broken state |
static int |
UNRESOLVED
unresolved state |
Constructor Summary | |
---|---|
Promise()
A constructor for promise |
|
Promise(T value)
A constructor for promise. |
Method Summary | ||
---|---|---|
|
cast(Class<O> type)
Cast promise to another type. |
|
void |
dereference(AResolver<? super T> l)
Register resolver for the promise. |
|
static void |
ensureSharable(Object o)
Ensure that value is sharable |
|
Promise<Void> |
finished()
|
|
static Promise<Void> |
finished(Promise<?> in)
|
|
boolean |
isResolved()
|
|
boolean |
isSmashed()
|
|
boolean |
isUnresolved()
|
|
static
|
notNull(Promise<? extends O> p)
Return promise that never returns null. |
|
static
|
notNull(Promise<? extends O> p,
String text)
Return promise that never returns null. |
|
static
|
nullPromise()
Return a promise resolved to null. |
|
Throwable |
problem()
|
|
AResolver<T> |
resolver()
Get resolver for this promise. |
|
static
|
smashed(Throwable t)
Create smashed promise |
|
Promise<Void> |
toVoid()
Convert promise to promise for void |
|
static Promise<Void> |
toVoid(Promise<?> in)
Convert promise to promise for void |
|
static
|
upcast(Promise<? extends A> p)
Upcast the promise. |
|
T |
value()
|
|
|
willBe(Class<P> proxyType)
This method allows creation of the proxy that allows sending message to resolution before promise is resolved. |
|
static
|
with(T value)
Get promise with value |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int SMASHED
public static final int RESOLVED
public static final int UNRESOLVED
Constructor Detail |
---|
public Promise(T value)
value
- a value to resolve promise with.public Promise()
Method Detail |
---|
public AResolver<T> resolver()
public <P extends AsyncObject> P willBe(Class<P> proxyType)
This method allows creation of the proxy that allows sending message to resolution before promise is resolved.
If promise is resolved with some value, it is checked if the resolution
supports interface represented by _class parameter. If the interface is
supported, all pending invocations are sent to resolution including
dereference calls.* If the interface is not supported by resolution, the
behavior is that same as if promise were smashed with
ClassCastException
. If promise is resolves to null, the behavior
of reference is the same as if it were smashed with
NullPointerException
. If promise was smashed with some
exception, all promises and eventual proxies returned by previous method
invocations are smashed with this exception. And the method
AResolver.smash(Throwable)
will be called on pending
dereference() calls.
P
- A proxy typeproxyType
-
public boolean isResolved()
public boolean isSmashed()
public boolean isUnresolved()
public Throwable problem()
public T value()
public void dereference(AResolver<? super T> l)
l
- listener to registerpublic static void ensureSharable(Object o)
o
- an object to checkpublic Promise<Void> toVoid()
public Promise<Void> finished()
public static Promise<Void> finished(Promise<?> in)
in
- an input promise
public static Promise<Void> toVoid(Promise<?> in)
in
- an input promise
public static <T> Promise<T> nullPromise()
T
- a type of promise
public static <T> Promise<T> with(T value)
T
- a value typevalue
- a value
public static <T> Promise<T> smashed(Throwable t)
T
- a type of promiset
- a promise value
public <O> Promise<O> cast(Class<O> type)
ClassCastException
if cast
is impossible.
O
- a new type of promisetype
- a new expected type
public static <A> Promise<A> upcast(Promise<? extends A> p)
resolver()
.
A
- a supper typep
- a promise to upcast
public static <O> Promise<O> notNull(Promise<? extends O> p)
NullPointerException
if argument promise resolves to
the null.
O
- a typep
- an arugment promise
public static <O> Promise<O> notNull(Promise<? extends O> p, String text)
NullPointerException
if argument promise resolves to
the null.
O
- a typep
- an arugment promisetext
- a text for NullPointerException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |