Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventField.java
index f06eb5b4c7f973565142ae68952f0d1e3b381123..4b82324fd3fa0168c6e5b0ab57103df029810a23 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011-2013 Ericsson
+ * Copyright (c) 2011, 2013 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -8,9 +8,10 @@
  *
  * Contributors:
  *  Matthew Khouzam - Initial API and implementation
- *  Alexendre Montplaisir - Initial API and implementation, extend TmfEventField
+ *  Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
  *  Bernd Hufmann - Add Enum field handling
- *  Geneviève Bastien - Add support for Struct fields
+ *  Geneviève Bastien - Add Struct and Variant field handling
+ *  Jean-Christian Kouame - Correct handling of unsigned integer fields
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
@@ -31,6 +32,8 @@ import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.internal.tmf.core.Messages;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
 
@@ -43,28 +46,6 @@ import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
  */
 public abstract class CtfTmfEventField extends TmfEventField {
 
-    // ------------------------------------------------------------------------
-    // Class attributes
-    // ------------------------------------------------------------------------
-
-    /** @since 1.2 */
-    protected static final int FIELDTYPE_INTEGER = 0;
-
-    /** @since 1.2 */
-    protected static final int FIELDTYPE_STRING = 1;
-
-    /** @since 1.2 */
-    protected static final int FIELDTYPE_INTEGER_ARRAY = 2;
-
-    /** @since 1.2 */
-    protected static final int FIELDTYPE_FLOAT = 3;
-
-    /** @since 2.0 */
-    protected static final int FIELDTYPE_ENUM = 4;
-
-    /** @since 2.0 */
-    protected static final int FIELDTYPE_STRUCT = 5;
-
     // ------------------------------------------------------------------------
     // Constructor
     // ------------------------------------------------------------------------
@@ -109,7 +90,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
         if (fieldDef instanceof IntegerDefinition) {
             IntegerDefinition intDef = (IntegerDefinition) fieldDef;
             int base = intDef.getDeclaration().getBase();
-            field = new CTFIntegerField(fieldName, intDef.getValue(), base);
+            field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
 
         } else if (fieldDef instanceof EnumDefinition) {
             EnumDefinition enumDef = (EnumDefinition) fieldDef;
@@ -136,7 +117,8 @@ public abstract class CtfTmfEventField extends TmfEventField {
                 for (int i = 0; i < arrayDecl.getLength(); i++) {
                     values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue());
                 }
-                field = new CTFIntegerArrayField(fieldName, values);
+                field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) arrayDecl.getElementType()).getBase(),
+                        ((IntegerDeclaration) arrayDecl.getElementType()).isSigned());
             }
             /* Add other types of arrays here */
 
@@ -156,7 +138,8 @@ public abstract class CtfTmfEventField extends TmfEventField {
                 for (int i = 0; i < seqDef.getLength(); i++) {
                     values.add(((IntegerDefinition) seqDef.getElem(i)).getValue());
                 }
-                field = new CTFIntegerArrayField(fieldName, values);
+                field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) seqDecl.getElementType()).getBase(),
+                        ((IntegerDeclaration) seqDecl.getElementType()).isSigned());
             }
             /* Add other Sequence types here */
 
@@ -175,26 +158,34 @@ public abstract class CtfTmfEventField extends TmfEventField {
                 list.add(curField);
             }
             field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
+
+        } else if (fieldDef instanceof VariantDefinition) {
+            VariantDefinition varDef = (VariantDefinition) fieldDef;
+
+            String curFieldName = varDef.getCurrentFieldName();
+            Definition curFieldDef = varDef.getDefinitions().get(curFieldName);
+            if (curFieldDef != null) {
+                CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
+                field = new CTFVariantField(fieldName, subField);
+            } else {
+                /* A safe-guard, but curFieldDef should never be null */
+                field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
+            }
+
+        } else {
+            /*
+             * Safe-guard, to avoid null exceptions later, field is expected not
+             * to be null
+             */
+            field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString());
         }
         return field;
     }
 
     @Override
     public String toString() {
-        return getName() + '=' + getValue().toString();
+        return getName() + '=' + getFormattedValue();
     }
-
-    // ------------------------------------------------------------------------
-    // Abstract methods (to be implemented by each specific field type)
-    // ------------------------------------------------------------------------
-
-    /**
-     * Return the int representing this field's value type
-     *
-     * @return The field type
-     */
-    public abstract int getFieldType();
-
 }
 
 /**
@@ -205,63 +196,35 @@ public abstract class CtfTmfEventField extends TmfEventField {
 final class CTFIntegerField extends CtfTmfEventField {
 
     private final int base;
+    private final boolean signed;
 
     /**
      * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
      * Java parser this is interpreted as a long.
      *
-     * @param longValue
-     *            The integer value of this field
      * @param name
      *            The name of this field
+     * @param longValue
+     *            The integer value of this field
+     * @param signed
+     *            Is the value signed or not
      */
-    CTFIntegerField(String name, long longValue, int base) {
+    CTFIntegerField(String name, long longValue, int base, boolean signed) {
         super(name, longValue, null);
+        this.signed = signed;
         this.base = base;
     }
 
-    @Override
-    public int getFieldType() {
-        return FIELDTYPE_INTEGER;
-    }
-
     @Override
     public Long getValue() {
         return (Long) super.getValue();
     }
 
-    /**
-     * Custom-format the integer values depending on their base.
-     */
     @Override
-    public String toString() {
-        StringBuilder sb = new StringBuilder(getName());
-        sb.append('=');
-
-        /* Format the number correctly according to the integer's base */
-        switch (base) {
-        case 2:
-            sb.append("0b"); //$NON-NLS-1$
-            sb.append(Long.toBinaryString(getValue()));
-            break;
-        case 8:
-            sb.append('0');
-            sb.append(Long.toOctalString(getValue()));
-            break;
-        case 10:
-            sb.append(getValue());
-            break;
-        case 16:
-            sb.append("0x"); //$NON-NLS-1$
-            sb.append(Long.toHexString(getValue()));
-            break;
-        default:
-            /* Non-standard base, we'll just print it as a decimal number */
-            sb.append(getValue().toString());
-            break;
-        }
-        return sb.toString();
+    public String getFormattedValue() {
+        return IntegerDefinition.formatNumber(getValue(), base, signed);
     }
+
 }
 
 /**
@@ -283,11 +246,6 @@ final class CTFStringField extends CtfTmfEventField {
         super(name, strValue, null);
     }
 
-    @Override
-    public int getFieldType() {
-        return FIELDTYPE_STRING;
-    }
-
     @Override
     public String getValue() {
         return (String) super.getValue();
@@ -301,28 +259,44 @@ final class CTFStringField extends CtfTmfEventField {
  */
 final class CTFIntegerArrayField extends CtfTmfEventField {
 
+    private final int base;
+    private final boolean signed;
+    private String formattedValue = null;
+
     /**
      * Constructor for CTFIntegerArrayField.
      *
+     * @param name
+     *            The name of this field
      * @param longValues
      *            The array of integers (as longs) that compose this field's
      *            value
-     * @param name
-     *            The name of this field
+     * @param signed
+     *            Are the values in the array signed or not
      */
-    CTFIntegerArrayField(String name, List<Long> longValues) {
+    CTFIntegerArrayField(String name, List<Long> longValues, int base, boolean signed) {
         super(name, longValues, null);
+        this.base = base;
+        this.signed = signed;
     }
 
     @Override
-    public int getFieldType() {
-        return FIELDTYPE_INTEGER_ARRAY;
+    public List<Long> getValue() {
+        return (List<Long>) super.getValue();
     }
 
     @Override
-    public List<Long> getValue() {
-        return (List<Long>) super.getValue();
+    public String getFormattedValue() {
+        if (formattedValue == null) {
+            List<String> strings = new ArrayList<String>();
+            for (Long value : getValue()) {
+                strings.add(IntegerDefinition.formatNumber(value, base, signed));
+            }
+            formattedValue = strings.toString();
+        }
+        return formattedValue;
     }
+
 }
 
 /**
@@ -344,11 +318,6 @@ final class CTFFloatField extends CtfTmfEventField {
         super(name, value, null);
     }
 
-    @Override
-    public int getFieldType() {
-        return FIELDTYPE_FLOAT;
-    }
-
     @Override
     public Double getValue() {
         return (Double) super.getValue();
@@ -366,18 +335,14 @@ final class CTFEnumField extends CtfTmfEventField {
      * Constructor for CTFEnumField.
      *
      * @param enumValue
-     *            The Enum value consisting of a pair of Enum value name and its long value
+     *            The Enum value consisting of a pair of Enum value name and its
+     *            long value
      * @param name
      *            The name of this field
      */
     CTFEnumField(String name, CtfEnumPair enumValue) {
         super(name, new CtfEnumPair(enumValue.getFirst(),
-                                    enumValue.getSecond().longValue()), null);
-    }
-
-    @Override
-    public int getFieldType() {
-        return FIELDTYPE_ENUM;
+                enumValue.getSecond().longValue()), null);
     }
 
     @Override
@@ -387,17 +352,17 @@ final class CTFEnumField extends CtfTmfEventField {
 }
 
 /**
- * The CTF field implementation for struct fields with sub-types
+ * The CTF field implementation for struct fields with sub-fields
  *
  * @author gbastien
  */
 final class CTFStructField extends CtfTmfEventField {
 
     /**
-     * Constructor for CTFStringField.
+     * Constructor for CTFStructField.
      *
-     * @param strValue
-     *            The string value of this field
+     * @param fields
+     *            The children of this field
      * @param name
      *            The name of this field
      */
@@ -406,19 +371,41 @@ final class CTFStructField extends CtfTmfEventField {
     }
 
     @Override
-    public int getFieldType() {
-        return FIELDTYPE_STRUCT;
+    public CtfTmfEventField[] getValue() {
+        return (CtfTmfEventField[]) super.getValue();
     }
 
     @Override
-    public CtfTmfEventField[] getValue() {
-        return (CtfTmfEventField[]) super.getValue();
+    public String getFormattedValue() {
+        return Arrays.toString(getValue());
+    }
+
+}
+
+/**
+ * The CTF field implementation for variant fields its child
+ *
+ * @author gbastien
+ */
+final class CTFVariantField extends CtfTmfEventField {
+
+    /**
+     * Constructor for CTFVariantField.
+     *
+     * @param field
+     *            The field selected for this variant
+     * @param name
+     *            The name of this field
+     */
+    CTFVariantField(String name, CtfTmfEventField field) {
+        super(name, field, new CtfTmfEventField[] { field });
     }
 
     @Override
-    public String toString() {
-        return getName() + '=' + Arrays.toString(getValue());
+    public CtfTmfEventField getValue() {
+        return (CtfTmfEventField) super.getValue();
     }
+
 }
 
 /* Implement other possible fields types here... */
This page took 0.028705 seconds and 5 git commands to generate.