net.sf.asyncobjects
Class When<I,R>

java.lang.Object
  extended by net.sf.asyncobjects.When<I,R>
Type Parameters:
I - a type of input value
R - a type of result
All Implemented Interfaces:
ExplicitSharing
Direct Known Subclasses:
AsyncProcess.ProcessWhen, CloseableWrapper.InvalidatingWhen

public abstract class When<I,R>
extends Object
implements ExplicitSharing

When construct support class. The construct is inspired by when/catch/finally exprssion in E programming language. The class is an utility implementation of resolver interface. The class is used by many other control construct to implement more specialized control flows.

Subclasses override the following methods:

resolved(Object)
This method is invoked when argument promise is resolved successfully. This method must be overriden.
bodySmashed(Throwable)
This method is invoked when resolved(Object) fails. By default, it delegates to smashed(Throwable).
smashed(Throwable)
This method is invoked when argument promise is smashed or if bodySmashed(Throwable) is not overriden. resolved(Object) fails.
finallyDo()
Is called when resolved(Object) or smashed(Throwable) finish.

See AsyncUnicastServer for definition of "fails", "finishes", and "finishes successfully".


Nested Class Summary
 class When.InternalArgumentListener
          This is an internal class.
 class When.InternalBodyPromiseResolver
          This is an internal class.
 class When.InternalFinallyListenerAfterFault
          This is an internal class.
 class When.InternalFinallyListenerAfterSuccess
          This is an internal class.
 class When.InternalResultPromiseResolver
          This is internal class.
 
Constructor Summary
When(I p)
          A constructor from value.
When(Promise<? extends I> p)
          A constructor from promise.
 
Method Summary
protected  Promise<? extends R> bodySmashed(Throwable problem)
          This method is called if the method resolved(Object) throws exception or when promise returned from that method is smashed.
protected  Promise<?> finallyDo()
           This action is called when promise is already either resolved or smashed if this method returns promise, the result promise is not resolved until a promise returned by this method is resolved.
 Promise<R> promise()
           
protected abstract  Promise<? extends R> resolved(I value)
          This method is called when promise is overloaded in subclasses.
protected  Promise<? extends R> smashed(Throwable problem)
           This method is called when argument promise is broken.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

When

public When(Promise<? extends I> p)
A constructor from promise.

Parameters:
p - promise to listen to

When

public When(I p)
A constructor from value.

Parameters:
p - a possibly unresolved reference to asynchronous service
Method Detail

promise

public Promise<R> promise()
Returns:
a promise that resolves to result of when, it could be got only once

resolved

protected abstract Promise<? extends R> resolved(I value)
                                          throws Throwable
This method is called when promise is overloaded in subclasses.

Parameters:
value - value to which promise had been resolved
Returns:
a value becomes curren result of promise
Throws:
Throwable - if there is a problem

bodySmashed

protected Promise<? extends R> bodySmashed(Throwable problem)
                                    throws Throwable
This method is called if the method resolved(Object) throws exception or when promise returned from that method is smashed. By default the method calls the method smashed(Throwable).

Parameters:
problem - a problem that broken a promise.
Returns:
a value becomes curren result of promise
Throws:
Throwable - if there is a problem

smashed

protected Promise<? extends R> smashed(Throwable problem)
                                throws Throwable

This method is called when argument promise is broken. If method bodySmashed(Throwable) is not overriden, it is also called if the method resolved(Object) throws exception or when promise returned from that method is smashed.

The default implementation returns a smashed promise.

Parameters:
problem - a problem that broken a promise.
Returns:
a value becomes curren result of promise
Throws:
Throwable - if there is a problem

finallyDo

protected Promise<?> finallyDo()
                        throws Throwable

This action is called when promise is already either resolved or smashed if this method returns promise, the result promise is not resolved until a promise returned by this method is resolved. If this method throws exception or promise returned by this method is smashed, the resulting promise is smashed too.

Default implementation returns null.

Returns:
a value becomes curren result of promise
Throws:
Throwable - if there is a problem


Copyright © 2002-2007 Constantine Plotnikov. All Rights Reserved.