Posts

Showing posts from December, 2008

Java Enum relational modelling

As I have blogged about before, the Java Enum is almost perfect. I say almost because of the annoying restriction that its constructor cannot include a relation to itself, due to forward reference limitations when using the Sun compiler. The issue came up again recently when I wanted to model authority roles in Wicket, so I spent a bit more time with the issue and the following is what I've learned. First though, a recap of the original problem. Consider the following attempt at modeling states of a CD player:



publicclass IlligalForwardReference {
enum Player{
PLAYING(Player.PAUSED, Player.STOPPED),
PAUSED(Player.PLAYING, Player.STOPPED),
STOPPED(Player.PLAYING);

privatefinal EnumSet<Player> transitionStates;

Player(final Player... states){
this.transitionStates = EnumSet.copyOf( Arrays.asList(states));
}

public EnumSet<Player> getTransitionStates(){
return transitionStates;
}
}

publicstaticvoid main(String... args){
System.out.println(Player.PLAYING);
}
}


The compiler will complain wi…