A minor glitch in type-safe enums

Saturday, January 14th, 2006

I’m quite fond of the type-safe enum design pattern. However, Wolfgang Hoscheck recently identified a place in XOM where I had introduced a bug as a result of it. Here’s an example:

public class FontStyle  {

  private int value;

  public static FontStyle PLAIN  = new FontStyle(1);
  public static FontStyle BOLD   = new FontStyle(2);
  public static FontStyle ITALIC = new FontStyle(3);
  public static FontStyle BOLD_ITALIC = new FontStyle(4);

  private FontStyle(int value) {
    this.value = value;


The bug shows up in another class that reference the FontStyle

  private FontStyle style;

  public void setStyle(FontStyle style) {
    this.style = style;

Do you see the bug? The problem is that there is one thing that can be passed into setStyle that is not one of the four legal values. Can you guess what it is?

Do the Wrong Thing

Thursday, January 12th, 2006

There’s a disturbing belief found throughout the programming community that it’s OK for an application to do the wrong thing by default, as long as there’s a way for the user to do the right thing. Stated that blatantly, it’s obviously false; and yet I keep running up against this belief time and time again in widely separated areas. Here are just a few:


The Cafes Reloaded

Wednesday, January 11th, 2006

Friday I spent most of the day converting the old custom written site into a new spiffy WordPress 2.0 site. Why I did that was outlined in my previous post, Why Mokka mit Schlag?

Then I promptly caught writer’s block. For a good 48 hours I could think of absolutely nothing to say here. I could have pulled an old article out of the vault, but I wanted to relaunch with something new. So I went on a parrot walk at Brooklyn College, did a little owling in Prospect Park, and got to work on my notes for Macintosh Development with Java at Software Development 2006 in Santa Clara in March. This is a revised version of the Macifying SWT talk I gave at EclipseCon in New York in the Fall. However, this time the focus will be on Swing rather than the SWT. (Swing does a much better job at handling the Mac.)

Verifying SSH Host Fingerprints

Tuesday, January 10th, 2006

If you use ssh you’re familiar with this message:

~$ ssh rich.elharo.com
The authenticity of host 'rich.elharo.com (' can't be established.
RSA key fingerprint is 5a:65:0f:5f:21:bb:fd:6a:5a:68:cd:62:58:5d:fb:9f.
Are you sure you want to continue connecting (yes/no)?

If you’re like me, you regularly just type yes and continue without much thought:

Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘rich.elharo.com,’ (RSA) to the list of known hosts.

But is that really safe? (more…)

Ant Tip 1: Write a master build file

Monday, January 9th, 2006

Many Java projects are divided into multiple subprojects or modules, each in its own directory. Often you’ll want to build subprojects individually, without necessarily building all of the larger master project. For example, in my XOM project, I have one master build.xml file that builds the software itself, and another build.xml file in the web directory that builds the web site. jEdit is divided into separate jedit, jeditshell, macros, and plugins directories, each of which has its own build.xml file.

Furthermore, projects may have dependencies on other projects. For instance XOM 1.1, JDOM, and dom4j all depend on Jaxen. For the latest and greatest JAR, Jaxen should be rebuilt using its own build.xml file, rather than bundling a stale JAR archive that’s months or even years beyond its expiration date.

You could cd into each separate diretory and type ant compile in each one, but that’s time conmsuming and error prone. Plus interproject dependencies may require this to be done in a precise order that makes this even more error-prone. It’s preferable to create a master build file at the top level that compiles everything by invoking targets in the other build files. The ant task lets you do this. For example, this task executes the build.xml file in the directory website (relative to the directory where the current build.xml file is) :