Typesafe Enums
I do not use J2SE 5.0, and I need to use an enumerator-like construct. My original code was:
// Constants to be used with runnersOnBase
publicstaticfinalshort RUNNER_ON_FIRST = 1;
publicstaticfinalshort RUNNER_ON_SECOND = 2;
publicstaticfinalshort RUNNER_ON_THIRD = 4;
Obviously this is not a great practice, so I decided to try typesafe enums, which are explained here: http://java.sun.com/developer/Books/shiftintojava/page1.html#replaceenums
The problem I'm having comes from the fact that, in my program, I can add the integer constants together. Is there a similar way to do that with typesafe enums, and if so, how should the enum class be set up? Thanks a lot. Take care.
[1158 byte] By [
Xperta] at [2007-10-2 5:45:38]

> The problem I'm having comes from the fact that, in
> my program, I can add the integer constants together.
> Is there a similar way to do that with typesafe
> enums, and if so, how should the enum class be set
> up? Thanks a lot. Take care.
Something like
public class Enum {
private static Map enums;
public static Enum A = new Enum();
public static Enum B = new Enum();
public static Enum C = new Enum();
static {
enums.add( Integer.valueOf( 0 ), A );
enums.add( Integer.valueOf( 0 ), B );
enums.add( Integer.valueOf( 0 ), C );
}
public static Enum add( Enum a, Enum b ) {
int av = enums.get( a );
int bv = enums.get( b );
Enum value = enums.get( Integer.valueOf( av + bv ) );
//if( value == null ) throw some exception maybe?
return value;
}
}
maybe?
mlka at 2007-7-16 1:55:30 >
