net.sf.staccatocommons.io.internal.lifecycle
Class Lifecycle<ResourceType,ResultType>

java.lang.Object
  extended by net.sf.staccatocommons.io.internal.lifecycle.Lifecycle<ResourceType,ResultType>
Type Parameters:
ResourceType - the type of resource handled by this lifecycle
ResultType - the type of result returned by this lifecycle
All Implemented Interfaces:
Callable<ResultType>, net.sf.staccatocommons.defs.Thunk<ResultType>
Direct Known Subclasses:
CloseableLifecycle

public abstract class Lifecycle<ResourceType,ResultType>
extends Object
implements net.sf.staccatocommons.defs.Thunk<ResultType>, Callable<ResultType>

A Lifecycle is a logic of initialization, use and dispose of a resource , that can be executed as a single unit of work. It can be executed in different fashions - using value(), call() , #run() or value() Lifecycles are abstract, so in order to use them, client code must implement the following methods

Author:
fbulgarelli

Constructor Summary
Lifecycle()
           
 
Method Summary
 ResultType call()
          Executes this Lifecycle, initializing the resource it handles, doing some work with it, disposing the resource and returning the work result.
protected  void dispose(ResourceType resource)
          Disposes the resource.
protected  void doVoidWork(ResourceType resource)
          Makes usage of a resource, without returning a result.
protected  ResultType doWork(ResourceType resource)
          Makes usage of a resource, and returns a result Lifecycles parameterized to have a Void result should not override this method, but doVoidWork(Object) instead
protected abstract  ResourceType initialize()
          Initializes and gets a resource of ResourceType
<E extends Exception>
ResultType
throwing(Class<E> exceptionClass)
          Handles exceptions of type exceptionClass that may occur when sending call().
<E1 extends Exception,E2 extends Exception>
ResultType
throwing(Class<E1> exceptionClass1, Class<E2> exceptionClass2)
          Handles exceptions of type exceptionClass1 and exceptionClass2 that may occur when sending call().
 ResultType value()
          Sends the call() message, softening any exception that may occur.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Lifecycle

public Lifecycle()
Method Detail

call

public ResultType call()
                throws Exception
Executes this Lifecycle, initializing the resource it handles, doing some work with it, disposing the resource and returning the work result. Any checked or unchecked exception produced during the flow execution will be catch, soften and rethrown.

Specified by:
call in interface Callable<ResultType>
Returns:
the result of the work over the resource
Throws:
RuntimeException - if any exception is thrown during the flow execution
Exception
See Also:
SoftException#soften(Exception)

value

public ResultType value()
Sends the call() message, softening any exception that may occur.

Specified by:
value in interface net.sf.staccatocommons.defs.Thunk<ResultType>
See Also:
SoftException.callOrSoften(Callable)

throwing

public final <E extends Exception> ResultType throwing(Class<E> exceptionClass)
                          throws E extends Exception
Handles exceptions of type exceptionClass that may occur when sending call(). This method is just a shortcut for Handle.throwing(this, exceptionClass)

Throws:
E extends Exception
See Also:
Handle.throwing(Callable, Class)

throwing

public final <E1 extends Exception,E2 extends Exception> ResultType throwing(Class<E1> exceptionClass1,
                                                                             Class<E2> exceptionClass2)
                          throws E1 extends Exception,
                                 E2 extends Exception
Handles exceptions of type exceptionClass1 and exceptionClass2 that may occur when sending call(). This method is just a shortcut for Handle.throwing(this, exceptionClass1, exceptionClass2)

Throws:
E1 extends Exception
See Also:
Handle.throwing(Callable, Class, Class)

initialize

protected abstract ResourceType initialize()
                                    throws Exception
Initializes and gets a resource of ResourceType

Returns:
the initialized resources
Throws:
Exception - if any error occurs

doWork

protected ResultType doWork(@NonNull
                            ResourceType resource)
                     throws Exception
Makes usage of a resource, and returns a result Lifecycles parameterized to have a Void result should not override this method, but doVoidWork(Object) instead

Parameters:
resource - the resource to use
Returns:
the result of using the resource, of ResultType. It may be null, if and only if ResultType is Void
Throws:
Exception - if any error occurs

doVoidWork

protected void doVoidWork(@NonNull
                          ResourceType resource)
                   throws Exception
Makes usage of a resource, without returning a result. This method should only be overriden in Lifecycles parameterized to have a Void result. If it is not the case, override doWork(Object) instead.

Parameters:
resource - the resource to use
Throws:
Exception - is any error occurs

dispose

protected void dispose(@NonNull
                       ResourceType resource)
                throws Exception
Disposes the resource. Default implementation does nothing, subclasses may want to override this method to add disposal logic

Parameters:
resource -
Throws:
Exception - if any error occurs


Copyright © 2010-2012 Staccatocommons. All Rights Reserved.