A Very Quick and Dirty Java Rules Engine

April 8, 2007

I was recently tasked with refactoring a mass of business logic that selected the appropriate machinery from a large group for a particular job configuration. The code as written was a huge tree of if/else statements inside a loop over the machines. Each statement would look at a particular set of job characteristics, and check if the current machine could support the job. This code was quite hard to maintain, as any several hundred lines of branching code is prone to be. What I needed was a little rules engine to evaluate my set of rules for every machine… Read the rest of this entry »


More Hibernate and Setters…

March 11, 2007

Over at the MD blog Darren has illustrated more dangers with Hibernate setters.


Serializable? Override hashCode

February 15, 2007

This is one of the rules of thumb when implementing Serializable - you need to override hashCode and equals. I was reminded of that today, when some code wasn’t working in client-server mode, but did in a non-RMI mode.
Read the rest of this entry »


Minimize RMI Round Trips: It’s a Best Practice for a Reason

October 4, 2006

Another tale from the profiler. The application I’m profiling uses RMI as communications transport between client and server. A particular operation took much longer in client-server mode than it did when we run in our “all-in-one-JVM” development mode. Which is a good lesson to try and run your app as it is deployed as much as possible during development.

Read the rest of this entry »


“Get” a Collection Element By Index Without Creating a New List or Array

September 25, 2006

More than a few times I’ve found myself needing the first element of a collection, and that collection wasn’t a List, with its nice get function. So I’ve ended up writing collection.iterator().next(), which just feels wrong. I’m not iterating.

Common Collections has a nice tool for this need: CollectionUtils.get(…). It can take a Map, List, Array, Set, Iterator, or Enumeration, and return the element at an index you specify. For example:


Set songs = new HashSet();
songs.add(”Frankenstein”);
songs.add(”Just Got Paid”);
System.out.println(songs.iterator().next());
System.out.println(CollectionUtils.get(songs, 0));

Mind you still have to check bounds.