Working with Enumerated Types (ENUMs)

If you want to categorize objects as different types, instead of using a number or a free format string, use an Enumerated Type (ENUM). Enumerated Types provide a better solution because they are restricted, in that they can only take a fixed limited number of values. The names of the values can be made meaningful.

Note: The use of ambiguous enumerations in script, when two enumerations with the same name exist in the same xpdl package, is managed by using a fully-qualified name (qualified by the package name) for the enumerations in the script.

The qualified name of enumerations to be used in script is similar to the Factory names, with the qualified name formatted to replace dot '.' by '_' an underscore character. For example, com.example.shared.ColorEnum will be used as com_example_shared_ColorEnum in script.

Unqualified names (for example, Color.GREEN) are supported only in validation for unambiguous situations (the unqualified name will not be available in content assist).

An ENUM is created in the BOM editor by selecting the Enumeration type from the Elements section of the Palette. Having selected the Enumeration Element, it can be named, and values can be added to it. The following is an example of an Enumerated type called SpaceType, with PLANET, MOON, ASTEROID, and STAR values:

Having defined the Enumeration type, a class attribute can be set to that type:

The following is an example of a loop, which can be used to calculate the average weight of the planets in a list of astronomical bodies.

var dTotalKgs    = 0.0;
var dPlanetCount = 0;
for (var iterator = solarSystem.objectList.listIterator(); iterator.hasNext(); )
{
     var body = iterator.next();
     if (SpaceType.PLANET == body.type)
     {
         dTotalKgs = dTotalKgs + body.weightKgs;
         dPlanetCount ++;
     }
}
solarSystem.averagePlanetWeight = dTotalKgs / dPlanetCount;

A Business Object attribute that is configured to be of a particular Enumeration type can only be assigned with values of that enumeration type. Either constants of that type, such as:

	body.type = SpaceType.PLANET;

or other attributes of that type, such as:

	aggregation.type = body.type;

An attribute of an enumeration type cannot be assigned from any other type. For example, the following is not valid:

body.type = "PLANET"; // This is wrong!

The Enumerated Type also enables you to get a specific enumeration literal from its text value. For example, if you want to pass an enumeration value as a string to an external application and then pass it back to a process. You can use this in scripts with all available enumeration data types. Either text data types, as in the following example:

spaceTypeEnum = SpaceType.get('PLANET');

and non-text data types, such as:

OrderSizeEnum = OrderSize.get('100');
Note: For non-text data types, the mapped text value must be the text used in the enumeration value and not its enumeration attribute name. For example, for an integer OrderSizeEnum (Large-100, Medium-50, Small-20) the mapping must use the values '100', '50', '20', and not the enumeration attribute names, Large, Medium, Small.