Hi,

Stanislav showed me a really cool pattern that can be used with visitors. Basically, if a visitor is visiting a structure and it needs to throw an exception, for whatever, reason, it is possible to do so in a type-safe way by declaring the exception through generics.

The visitor interface might look like this:
interface EntityStateVisitor<ThrowableType extends Exception>
{
        void visitEntityState( EntityState entityState )
            throws ThrowableType;
}
--
and the visit methods that take such a visitor would be defined as:
<ThrowableType extends Exception> EntityStoreUnitOfWork visitEntityStates( EntityStateVisitor<ThrowableType> visitor, ModuleSPI module )
        throws ThrowableType;
---

If a visitor implementation needs to be able to throw for example SQLException you would specify that in the visitor implementation, and then do try/catch on it around the visitEntityStates call.

This makes it easy to abort a visitor traversal, and also allows the traversal code to do any necessary cleanup.

I have changed *ALL* visitor interfaces and methods (quite a bit) in Qi4j to use this pattern.

You will probably want to make an update to get this, as it affects quite a bit of code.

regards, Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to