Enumeration

  1. Enumeration Model
  2. Diagram presentation
  3. Tutorials
    1. Simple mode
    2. Advanced mode
    3. Interface provider connection

1. Enumeration Model

An enumeration in Java is a specific kind of class, which can have some private constructors, attributes, methods and static instances as literals. The literals are the static members.

Here is a simple example:

public enum Category
{
	History,  Drama,  ScienceFiction;
}

It should be possible that a literal has some additional attributes. For example to add the nature language name:

public enum Category
{
	Documentary("Documentary"),  Drama("Drama"),  ScienceFiction("Science Fiction");
	protected String name;
	private Category(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return name;
	}
}

All properties must be read-only. An enumeration cannot have a super class, but it is possible to implement some interfaces.

There is a mismatch in terms of model between Java and UML2 Modeler . In Java, it is possible to have an inner enumeration:

public enum Outer
{
	Literal1;
	public enum Inner
	{
		Literal1;
	}
}

2. Diagram presentation

For the graphical presentation, all literals are grouped in a new compartment on top of the attribute compartment:

We introduce a new enumeration mode, which contains only the literals. This will be used as the default mode:

This mode can be switched to “Advanced mode” via the context menu “Switch to advanced mode”. It can be brought back to “Simple mode” via the context menu “Switch to simple mode”.

Each literal can be hidden individually like attribute and method. It can be selected to show up via the “View selector”, or the “undo” operation of “hide”. In general, we show or hide all literals together.

3. Enumeration Tutorials

These tutorials illustrate how to create and edit an enumeration from a simple case to a more complex one.

3.1. Simple mode

1. Open a diagram in a UML project with JDK 1.5 features

First we need to create a diagram using the New Wizard or package context menu eUML2->Class diagram editor.

2. Create an enumeration via the tool bar

Select the enumeration icon tool in the tool bar of the diagram editor.

In the Enumeration Wizard, type the name “Category”. You will have an enumeration shown as below.

3. Add a literal

Select the enumeration above; open the context menu and select New->Literal.

Type the new literal name “History”, and then click the button OK to validate this operation.

A new literal is created.

Here is the Java code generated for this enumeration:

Package company;

public enum Category
{
	History;
}

3.2. Advanced mode

1. Open a diagram in a UML project with JDK 1.5 features

Same as the simple mode.

2. Create an enumeration via the tool bar

Same as the simple mode.

3. Switch to advanced mode

Right click on the enumeration and select the context menu “Switch to advanced mode”.

Afterwards, this enumeration is considered as a class and the menus to create/insert properties and methods are shown.

4. Create a property

Open the enumeration context menu and select New -> Attribute.

Put the “name” in the field name and click the button OK with the default options.

A new attribute and a read-only accessor are created.

5. Create the default constructor

Open the enumeration context menu and select New -> Constructor.

In the constructor wizard, all parameters and body implementation are pre-filled automatically. Just click on the OK button.

6. Switch back to simple mode

Select enumeration and open the context menu, select Switch to simple mode.

7. Add a literal

Same as operation 3 in the simple mode, open the context menu and select New->Literal.

In this new look wizard, enter the literal name "History"; and a constant expression in the second column for the literal initialization. If there is more than one constructor, it is possible to select the appropriate one in the list. The corresponding parameters will be updated in the table below.

Here is the Java code generated for this enumeration:

Package model;

public enum Category
{
	History("History");

	private String name = "";
	
	private Category(String name) {
		this.name = name;
	}

	/**
	 * Getter of the property <tt>name</tt>
	 * 
	 * @return Returns the name.
	 * @uml.property name="name"
	 */
	public String getName() {
		return name;
	}
} 

3.3. Interface provider connection

An enumeration can implement an interface so it can be an interface provider. Like a class, it is possible to create an interface provider via the tool bar.

In our example, suppose we need a serializable enumeration. Select the “interface provider” tool icon and then highlight the enumeration.

A new interface selection dialog is shown.

To create a new interface, click on the button “New…”. Or select an existing one via the button “Browse…”. In our case, we select the existing one java.util.Serializable.

This operation in fact creates an implementation relationship between the enumeration and the interface. Here is the generated code:

Package model;

public enum Category implements java.io.Serializable {
}

In the same way, we can create a “required interface”.

Here is the code generated:

Package model;

/**
 * @uml.dependency supplier="java.io.Serializable" 
 */
public enum Category implements java.io.Serializable 
{
	History;
}

For an enumeration with an “interface provider” and “required interface”, it is possible to establish the connection by dragging one interface icon into another or using the “Interface connection” icon in the tool bar to create the connection in one step.

© Soyatec 2002-2004. All Rights Reserved.