CHOICE

The ASN.1 CHOICE type is converted to a Java class that extends the Asn1Choice run-time base class. This base class contains protected member variables to hold the choice element object and a selector value to specify which item in the CHOICE was chosen. Methods are generated to get and set the base class members.

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

ASN.1 Production

   <name> ::= CHOICE {
      <element1-name> <element1-type>,
      <element2-name> <element2-type>,
      ...
   }

XSD Types

<xsd:choice>, <xsd:union>

Generated Java class

   public class <name> extends Asn1Choice {
      public final static byte _<ELEMENT1-NAME> 1
      public final static byte _<ELEMENT2-NAME> 2
      ...

      public <name> () { super(); }
    
      public String getElemName() { ... }

      public void set_<element1-name> () { ... }
      public void set_<element2-name> () { ... }
      ...
      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 sequence elements.

  2. The public and private methods that are generated are shown without arguments or function bodies for brevity.

The compiler generates sequential identification constants for each of the defined elements in the CHOICE construct. The format used is the element names converted to all uppercase characters and preceded by an underscore. The constants represent the values returned by the base class getChoiceID method can therefore be used to determine what type of choice element was received in a decode operation.

The getElemName method is generated by the compiler and returns the name of the selected element.

A series of set_<element> methods are generated for setting the element value. In these declarations, <element> would be replaced with the actual element names. This is the only way an element value can be set for encoding; these methods ensure a consistent setting of both the element identifier and object reference values.

To access the value of a generated CHOICE object, the getChoiceID and getElement methods within the base class are used. This is generally done with an if or switch statement as follows:

   Asn1BMPString element;
   if (aliasAddress.getChoiceID() == AliasAddress._H323_ID) {
      element = (Asn1BMPString) aliasAddress.getElement();
   }

In this case, getChoiceID is invoked and the result tested to see if the expected value was received. If it was, the element is assigned using getElement with a cast operation.