1414
1515package org .eclipse .tracecompass .ctf .core .event .types ;
1616
17+ import java .util .Arrays ;
1718import java .util .Collection ;
18- import java .util .Collections ;
1919import java .util .HashMap ;
2020import java .util .Map ;
2121import java .util .Map .Entry ;
@@ -44,7 +44,8 @@ public class VariantDeclaration extends Declaration {
4444
4545 private String fTag = null ;
4646 private static final long ALIGNMENT = 1 ;
47- private final Map <String , IDeclaration > fFields = Collections .synchronizedMap (new HashMap <String , IDeclaration >());
47+ private final Map <String , IDeclaration > fFields = new HashMap <>();
48+ private IDeclaration [] fFieldArray = new IDeclaration [0 ];
4849 private IDeclaration fDeclarationToPopulate ;
4950
5051 // ------------------------------------------------------------------------
@@ -122,16 +123,32 @@ public VariantDefinition createDefinition(IDefinitionScope definitionScope,
122123 String fieldName , BitBuffer input ) throws CTFException {
123124 alignRead (input );
124125 IDefinition def = definitionScope .lookupDefinition (fTag );
125- EnumDefinition tagDef = (EnumDefinition ) ((def instanceof EnumDefinition ) ? def : null );
126- if (tagDef == null ) {
127- throw new CTFException ("Tag is not defined " + fTag ); //$NON-NLS-1$
128- }
129- String varFieldName = tagDef .getStringValue ();
130- if (varFieldName == null ) {
131- throw new CTFException ("Undefined enum selector for variant " + //$NON-NLS-1$
132- definitionScope .getScopePath ().getPath ());
126+ String varFieldName = fTag ;
127+ SimpleDatatypeDefinition tagDef = null ;
128+ if (def instanceof IntegerDefinition ) {
129+ tagDef = (SimpleDatatypeDefinition ) def ;
130+ Long tagValue = ((IntegerDefinition ) tagDef ).getIntegerValue ();
131+ String mappings = ((IntegerDefinition ) tagDef ).getMappings ();
132+ if (mappings != null ) {
133+ fDeclarationToPopulate = fFields .get (mappings );
134+ } else if (tagValue != null ) {
135+ if (tagValue < 0 || tagValue >= fFieldArray .length ) {
136+ throw new CTFException ("Unknown value of " + tagValue + " for the variant " + toString ()); //$NON-NLS-1$ //$NON-NLS-2$
137+ }
138+ fDeclarationToPopulate = fFieldArray [(tagValue .intValue ())];
139+ }
140+ } else {
141+ tagDef = (EnumDefinition ) ((def instanceof EnumDefinition ) ? def : null );
142+ if (tagDef == null ) {
143+ throw new CTFException ("Tag is not defined " + fTag ); //$NON-NLS-1$
144+ }
145+ varFieldName = tagDef .getStringValue ();
146+ if (varFieldName == null ) {
147+ throw new CTFException ("Undefined enum selector for variant " + //$NON-NLS-1$
148+ definitionScope .getScopePath ().getPath ());
149+ }
150+ fDeclarationToPopulate = fFields .get (varFieldName );
133151 }
134- fDeclarationToPopulate = fFields .get (varFieldName );
135152 if (fDeclarationToPopulate == null ) {
136153 throw new CTFException ("Unknown enum selector for variant " + //$NON-NLS-1$
137154 definitionScope .getScopePath ().getPath ());
@@ -150,6 +167,9 @@ public VariantDefinition createDefinition(IDefinitionScope definitionScope,
150167 */
151168 public void addField (String fieldTag , IDeclaration declaration ) {
152169 fFields .put (fieldTag , declaration );
170+ int size = fFields .size ();
171+ fFieldArray = Arrays .copyOf (fFieldArray , size );
172+ fFieldArray [size - 1 ] = declaration ;
153173 }
154174
155175 @ Override
0 commit comments