Friday, October 9, 2009

Academic Excercise 1 for Java

So I was asking myself, "Self, would it be possible in Java, like in some of the coolboy languages, in one statement, to wrap an arbitrary hunk of code with resource allocation and cleanup?" It turns out there's a not-too-crazy way as long as your arguments can be marked final:

/**
* Provides resource-safe services for client code.
*/
public class DbKit {
protected DbKit()
{
}
interface DbTask<E>
{
abstract E run(Db db) throws Exception;
}

/**
* Along with DbTask, provides a means to wrap instantiation, opening,
* try { do } finally{close} around any task.
*/
private static <E> E run(DbTask<E> t) throws Exception
{
Db db= new Db();
db.open();
try
{
return t.run(db);
}
finally
{
db.close();
}
}
public static SomeRec insertSomeRec(final long lv,final String sv) throws Exception
{
return run(new DbTask<SomeRec>(){
public SomeRec run(Db db) throws Exception
{
SomeRec r = new SomeRec ();
r.setLongVal(lv);
r.setStrVal(sv);
db.insert(r);
return r;
}});
}
public static void updateRec(final long kv,final String newStatus) throws Exception
{
run(new DbTask<Object>(){
public Object run(Db db) throws Exception
{
SomeRec.update(db,kv,newStatus);
return null;
}});
}
}

Saturday, September 26, 2009

What to Work on Next

Testing, 1, 2, 3, the missiles are flying. My first post is a cod liver oil pill to be choked down before I begin to do what I fear I must do next. Lemons from lemonade - we'll explore the current situation and propose a maxim.

Suppose, like me, you're a programmer/developer/contractor/ISV who has the occasional luxury of choosing what task to work on, and that we like our profession, even find it extremely engaging at times - here's the maxim:


• if you're thinking about it in the shower - you must work on it

..or while exercising, or trying to sleep... even, alas, if it's a banal consulting assignment working with unfashionable technology; somehow it's driving your stray thoughts and must be purged before you can effectively return to those pursuits judged more important/interesting/(dare i say it:lucrative!) by your conscious mind.

I have my own business, two modestly profitable products, a promising product in early construction, an open-sourced creation for two platforms, a nascent interest in alternative programming languages, an urge to further explore native/embedded/mobile device products, and a desire to blog/teach/train/proselytize, and what's occupying my stray thoughts right now? A hunk of J2EE middleware to help BigCorp1217 move orders from xml to a dbms!

What could I do, Pancho Villa, he had a gun.