ctf/tmf: allow multiple traces to be open with name clashing events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventField.java
index 193025efe5c85dc0b9dadd2316f163e5aa5ada2a..6e77376bdd5085ce2112c123c189d9a4792025d1 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson, École Polytechnique de Montréal
+ * Copyright (c) 2011, 2014 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
@@ -11,6 +11,8 @@
  *  Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
  *  Bernd Hufmann - Add Enum field handling
  *  Geneviève Bastien - Add Struct and Variant field handling
+ *  Jean-Christian Kouame - Correct handling of unsigned integer fields
+ *  François Doray - Add generic array field type
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
@@ -20,7 +22,6 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map.Entry;
 
-import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
@@ -45,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
     // ------------------------------------------------------------------------
@@ -111,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;
@@ -126,22 +105,25 @@ public abstract class CtfTmfEventField extends TmfEventField {
 
         } else if (fieldDef instanceof ArrayDefinition) {
             ArrayDefinition arrayDef = (ArrayDefinition) fieldDef;
-            ArrayDeclaration arrayDecl = arrayDef.getDeclaration();
 
-            if (arrayDef.isString()) {
+            if (arrayDef.getDeclaration().isString()) {
                 /* This is an array of UTF-8 bytes, a.k.a. a String! */
                 field = new CTFStringField(fieldName, fieldDef.toString());
 
-            } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) {
-                /* This is a an array of CTF Integers */
-                List<Long> values = new ArrayList<Long>(arrayDecl.getLength());
-                for (int i = 0; i < arrayDecl.getLength(); i++) {
-                    values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue());
+            } else {
+                /* Arrays of elements of any other type */
+                Definition[] definitions = arrayDef.getDefinitions();
+                CtfTmfEventField[] elements = new CtfTmfEventField[definitions.length];
+
+                /* Parse the elements of the array. */
+                for (int i = 0; i < definitions.length; i++) {
+                    CtfTmfEventField curField = CtfTmfEventField.parseField(
+                            definitions[i], fieldName + '[' + i + ']');
+                    elements[i] = curField;
                 }
-                field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) arrayDecl.getElementType()).getBase());
-            }
-            /* Add other types of arrays here */
 
+                field = new CTFArrayField(fieldName, elements);
+            }
         } else if (fieldDef instanceof SequenceDefinition) {
             SequenceDefinition seqDef = (SequenceDefinition) fieldDef;
             SequenceDeclaration seqDecl = seqDef.getDeclaration();
@@ -154,11 +136,14 @@ public abstract class CtfTmfEventField extends TmfEventField {
                 field = new CTFStringField(fieldName, seqDef.toString());
             } else if (seqDecl.getElementType() instanceof IntegerDeclaration) {
                 /* Sequence of integers => CTFIntegerArrayField */
-                List<Long> values = new ArrayList<Long>(seqDef.getLength());
+                long[] values = new long[seqDef.getLength()];
                 for (int i = 0; i < seqDef.getLength(); i++) {
-                    values.add(((IntegerDefinition) seqDef.getElem(i)).getValue());
+                    values[i] = ((IntegerDefinition) seqDef.getElem(i)).getValue();
                 }
-                field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) seqDecl.getElementType()).getBase());
+                field = new CTFIntegerArrayField(fieldName, values,
+                        ((IntegerDeclaration) seqDecl.getElementType()).getBase(),
+                        ((IntegerDeclaration) seqDecl.getElementType()).isSigned());
+
             }
             /* Add other Sequence types here */
 
@@ -168,7 +153,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
             String curFieldName = null;
             Definition curFieldDef;
             CtfTmfEventField curField;
-            List<ITmfEventField> list = new ArrayList<ITmfEventField>();
+            List<ITmfEventField> list = new ArrayList<>();
             /* Recursively parse the fields */
             for (Entry<String, Definition> entry : strDef.getDefinitions().entrySet()) {
                 curFieldName = entry.getKey();
@@ -177,20 +162,25 @@ 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) {
-                field = CtfTmfEventField.parseField(curFieldDef, curFieldName);
+                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 */
+            /*
+             * 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;
@@ -198,52 +188,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
 
     @Override
     public String toString() {
-        return getName() + '=' + getValue().toString();
-    }
-
-    // ------------------------------------------------------------------------
-    // 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();
-
-    /**
-     * Print a numeric value as a string in a given base
-     *
-     * @param value
-     *            The value to print as string
-     * @param base
-     *            The base for this value
-     * @return formatted number string
-     * @since 2.0
-     */
-    protected final static String formatNumber(long value, int base) {
-        String s;
-        /* Format the number correctly according to the integer's base */
-        switch (base) {
-        case 2:
-            s = "0b" + Long.toBinaryString(value); //$NON-NLS-1$
-            break;
-        case 8:
-            s = "0" + Long.toOctalString(value); //$NON-NLS-1$
-            break;
-        case 10:
-            s = Long.toString(value);
-            break;
-        case 16:
-            s = "0x" + Long.toHexString(value); //$NON-NLS-1$
-            break;
-        default:
-            /* Non-standard base, we'll just print it as a decimal number */
-            s = Long.toString(value);
-            break;
-        }
-        return s;
+        return getName() + '=' + getFormattedValue();
     }
 
 }
@@ -255,25 +200,24 @@ public abstract class CtfTmfEventField extends TmfEventField {
  */
 final class CTFIntegerField extends CtfTmfEventField {
 
-    private final int base;
+    private final int fBase;
+    private final boolean fSigned;
 
     /**
      * 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.base = base;
-    }
-
-    @Override
-    public int getFieldType() {
-        return FIELDTYPE_INTEGER;
+        fSigned = signed;
+        fBase = base;
     }
 
     @Override
@@ -283,16 +227,9 @@ final class CTFIntegerField extends CtfTmfEventField {
 
     @Override
     public String getFormattedValue() {
-        return formatNumber(getValue(), base);
+        return IntegerDefinition.formatNumber(getValue(), fBase, fSigned);
     }
 
-    /**
-     * Custom-format the integer values depending on their base.
-     */
-    @Override
-    public String toString() {
-        return getName() + '=' + formatNumber(getValue(), base);
-    }
 }
 
 /**
@@ -314,11 +251,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();
@@ -332,51 +264,82 @@ final class CTFStringField extends CtfTmfEventField {
  */
 final class CTFIntegerArrayField extends CtfTmfEventField {
 
-    private final int base;
-    private String formattedValue = null;
+    private final int fBase;
+    private final boolean fSigned;
+    private String fFormattedValue = 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, int base) {
+    CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
         super(name, longValues, null);
-        this.base = base;
-    }
-
-    @Override
-    public int getFieldType() {
-        return FIELDTYPE_INTEGER_ARRAY;
+        fBase = base;
+        fSigned = signed;
     }
 
     @Override
-    public List<Long> getValue() {
-        return (List<Long>) super.getValue();
+    public long[] getValue() {
+        return (long[]) super.getValue();
     }
 
     @Override
-    public String getFormattedValue() {
-        if (formattedValue == null) {
-            List<String> strings = new ArrayList<String>();
-            for (Long value : getValue()) {
-                strings.add(formatNumber(value, base));
+    public synchronized String getFormattedValue() {
+        if (fFormattedValue == null) {
+            List<String> strings = new ArrayList<>();
+            for (long value : getValue()) {
+                strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned));
             }
-            formattedValue = strings.toString();
+            fFormattedValue = strings.toString();
         }
-        return formattedValue;
+        return fFormattedValue;
     }
 
+}
+
+/**
+ * CTF field implementation for arrays of arbitrary types.
+ *
+ * @author fdoray
+ */
+final class CTFArrayField extends CtfTmfEventField {
+
+    private String fFormattedValue = null;
+
     /**
-     * Custom-format the integer values depending on their base.
+     * Constructor for CTFArrayField.
+     *
+     * @param name
+     *            The name of this field
+     * @param elements
+     *            The array elements of this field
      */
+    CTFArrayField(String name, CtfTmfEventField[] elements) {
+        super(name, elements, elements);
+    }
+
     @Override
-    public String toString() {
-        return getName() + '=' + getFormattedValue();
+    public CtfTmfEventField[] getValue() {
+        return (CtfTmfEventField[]) super.getValue();
+    }
+
+    @Override
+    public synchronized String getFormattedValue() {
+        if (fFormattedValue == null) {
+            List<String> strings = new ArrayList<>();
+            for (CtfTmfEventField element : getValue()) {
+                strings.add(element.getFormattedValue());
+            }
+            fFormattedValue = strings.toString();
+        }
+        return fFormattedValue;
     }
 }
 
@@ -399,11 +362,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();
@@ -421,18 +379,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()), null);
     }
 
     @Override
@@ -442,17 +396,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
      */
@@ -461,19 +415,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.030665 seconds and 5 git commands to generate.