JaxMe 2 JaxMe 2

the JaxMe 2 site
 
   

Typesafe enumerations

PDF
PDF

Typesafe enumerations are classes with a private constructor and only a few defined instances. Each instance does have a name and a value. As of Java 1.5, they will be part of the Java language. For those of us, who are still stuck with older versions, a source generator is a good solution.

    <enumGenerator targetClass="org.apache.ws.jaxme.js.junit.EnumExample"
                   destDir="${build.src}">
       <item name="JOE" value="John Doe"/>
       <item name="POPEYE" value="Olivia's Lover"/>
       <item name="DONALD" value="The Duck King"/>
    </enumGenerator>
      

This will generate a class looking roughly like this:

    public class EnumExample {
      private String name;
      private String value;
      private EnumExample(String pName, String pValue) {
        name = pName;
        value = pValue;
      }
      public String getName() { return name; }
      public String getValue() { return value; }
      public final static EnumExample JOE = new EnumExample("JOE", "John Doe");
      public final static EnumExample POPEYE = new EnumExample("POPEYE", "Olivia's Lover");
      public final static EnumExample DONALD = new EnumExample("DONALD", "The Duck King");
    }
      

The important thing with this enumeration is that there cannot be other instances besides JOE, POPEYE and DONALD.