Collection

Collection #

This page contains the following content:

The Collection classes are containers that can store each kind of data objects. Typical restrictions are the maximum and minimum number of elements and the base data class of which objects can be instantiated. The Collection class is an abstract class that can not be instantiated directly.

There are two available class types for defining collection objects:

  1. List: A list class is a collection class, where the elements have a specific order. This class can contain duplicates, if they are added to it.
  2. Set: A set class is a collection class, where it is not possible to specify the order of the elements. Thus, each element is unique within the set.

To create a collection class in the model.xml file, a new subclass of the class List or the class Set must be created. The element class is defined using an inner tag ElementClass. The restrictions for the number of elements can be set here. By using the tags maxCardinality and minCardinality, the required maximum and minimum number of elements for each instance is defined.

List #

A List class is a subclass of the Collection class, where it is possible to specify the order of the elements in the List object. Thus, the elements can be accessed by using an index. Furthermore, it is possible to have duplicate objects in lists.

The class has to be defined in the model.xml file:

<ListClass name="exampleListClass">
   <ElementClass name="exampleAtomicClass"/>
</ListClass>

To create this class in the code, the following java code must be used:

ListClass listClass = (ListClass) ModelFactory.getDefaultModel().getClass(ListClass.CLASS_NAME);
ListClass exampleListClass = (ListClass) listClass.createSubclass("exampleListClass");
exampleListClass.setElementClass(ModelFactory.getDefaultModel().getClass("exampleAtomicClass"));
exampleListClass.finishEditing();

To create an object of this list class, the following java code has to be used:

ListObject listObject = (ListObject) ModelFactory.getDefaultModel().createObject("exampleListClass");

Set #

A Set class is a subclass of the Collection class, where it is not possible to specify the order of the elements in the Set object. It is also not possible to have duplicates in the set. Duplicate values can be added but if the Set already contains such a value, nothing happens.

The class has to be defined in the model.xml file:

<SetClass name="exampleSetClass">
   <ElementClass name="exampleAtomicClass"/>
</SetClass>

To create this class in the code, the following java code has to be used:

SetClass setClass = (SetClass) ModelFactory.getDefaultModel().getClass(SetClass.CLASS_NAME);
SetClass exampleSetClass = (SetClass) setClass.createSubclass("exampleSetClass");
exampleSetClass.setElementClass(ModelFactory.getDefaultModel().getClass("exampleAtomicClass"));
exampleSetClass.finishEditing();

To create an object of this list class, the following java code has to be used:

SetObject setObject = (SetObject) ModelFactory.getDefaultModel().createObject("exampleSetClass");