ctf: Move CTF plugins to Java 7 and fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / VariantDefinition.java
index 4c249440b3f24c4c7df9aca3e341011b54805032..120df59147aad174cd419652bee36c3e1ec43c61 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -15,10 +15,19 @@ package org.eclipse.linuxtools.ctf.core.event.types;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 
 /**
- * <b><u>VariantDefinition</u></b>
+ * A CTF variant definition (similar to a C union).
+ *
+ * A variant is similar to a C union, only taking the minimum size of the types,
+ * it is a compound data type that contains other datatypes in fields. they are
+ * stored in an hashmap and indexed by names which are strings.
+ *
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Simon Marchi
  */
 public class VariantDefinition extends Definition implements IDefinitionScope {
 
@@ -29,7 +38,7 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
     private VariantDeclaration declaration;
 
     private EnumDefinition tagDefinition;
-    private HashMap<String, Definition> definitions = new HashMap<String, Definition>();
+    private Map<String, Definition> definitions = new HashMap<>();
     private String currentField;
 
     // ------------------------------------------------------------------------
@@ -49,13 +58,6 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
         this.declaration = declaration;
 
         Definition tagDef = definitionScope.lookupDefinition(declaration.getTag());
-        /*
-         * if (tagDef == null) { throw new
-         * Exception("Variant tag field not found"); }
-         *
-         * if (!(tagDef instanceof EnumDefinition)) { throw new
-         * Exception("Variant tag field not enum"); }
-         */
         this.tagDefinition = (EnumDefinition) tagDef;
 
         for (Map.Entry<String, IDeclaration> field : declaration.getFields().entrySet()) {
@@ -101,16 +103,18 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
     /**
      * Get the definitions in the variant
      * @return the definitions
+     * @since 2.0
      */
-    public HashMap<String, Definition> getDefinitions() {
+    public Map<String, Definition> getDefinitions() {
         return definitions;
     }
 
     /**
      * Set the definitions in a variant
      * @param definitions the definitions
+     * @since 2.0
      */
-    public void setDefinitions(HashMap<String, Definition> definitions) {
+    public void setDefinitions(Map<String, Definition> definitions) {
         this.definitions = definitions;
     }
 
@@ -122,11 +126,6 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
         this.currentField = currentField;
     }
 
-    @Override
-    public String getPath() {
-        return path;
-    }
-
     /**
      * Get the current field name
      * @return the current field name
@@ -149,11 +148,13 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
     // ------------------------------------------------------------------------
 
     @Override
-    public void read(BitBuffer input) {
+    public void read(BitBuffer input) throws CTFReaderException {
         currentField = tagDefinition.getValue();
 
         Definition field = definitions.get(currentField);
-
+        if (field == null) {
+            throw new CTFReaderException("Variant was not defined for: "+ currentField); //$NON-NLS-1$
+        }
         field.read(input);
     }
 
@@ -259,4 +260,10 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
                 : null);
     }
 
+    @Override
+    public String toString() {
+        return "{ " + getCurrentFieldName() + //$NON-NLS-1$
+                " = " + getCurrentField() + //$NON-NLS-1$
+                " }"; //$NON-NLS-1$
+    }
 }
This page took 0.027434 seconds and 5 git commands to generate.