ENUMERATED

The ASN.1 ENUMERATED type is converted into a Java class that extends the Asn1Enumerated run-time class. In version 6.1, the generated code was changed to conform to Joshua Bloch's static enumeration pattern (as explained in Effective Java). Enumerated values are created as singletons to allow for lazy initialization. A specially named object, dec, is created to hold decoded values. In combination, these changes improve application performance, since only a fixed number of objects are allocated for any execution of the application.

The following shows the basic mapping from ASN.1 type to Java class definition:

ASN.1 Production

   <name> ::= ENUMERATED { <e1>, <e2>, ..., <en> }

XSD Types

Any type with an <xsd:enumeration> restriction.

Generated Java class

   public class <name> extends Asn1Enumerated {
      private static <name> <e1> = null;
      private static <name> <e2> = null;
      ...
      private static <name> dec = new <name> (-1);
      
      protected <name> (int value_) {
         super (value_);
      }

      public static <name> <e1>() {
         if (<e1> == null) <e1> = new <name>(<v1>);

         return <e1>;
      }
      ...

      public static <name> valueOf(int value_) { ... }

      protected static <name> dec() { return dec; }

      public void decode () { ... }
      public int encode () { ... }
      public void print () { ... }
   }

Note

  1. The ... notation used in the ASN.1 definition above does not represent the ASN.1 extensibility notation. It is used to show a continuation of the enumerated sequence of values.

  2. The <e1>, <e2>, etc. items denote enumerated constants. These can be in identifier only format or identifier(value) format. The <v1>, <v2>, etc. items denote the enumerated values. These are sequential numbers starting at zero if no values are provided. Otherwise, the actual enumerated values are used.

  3. The public methods that are generated are shown without arguments or function bodies for brevity.

In the case of the enumerated type, encode/decode methods are always generated. These verify that the given value is within the defined set. An Asn1InvalidEnumException is thrown if the value is not in the defined set unless the enumeration is extensible. In this case, no exception is thrown.

If an extensibility marker (...) is present in the ASN.1 definition, it will not affect the generated constants. A constant will be generated for all options – both root and extended. However, in the ValueOf method, an "undefined" constant will be returned to indicate that the value is not in the original specification.