tmf: Bug 497347: Custom event aspects incorrect in mixed experiments
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomEvent.java
index f1bae00e172734ea2d8923b6015c230b7255f246..cc6b43302a865b38229815484a748edefed5a8c2 100644 (file)
 
 package org.eclipse.tracecompass.tmf.core.parsers.custom;
 
-import static org.eclipse.tracecompass.common.core.NonNullUtils.equalsNullable;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
 
 import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
@@ -25,12 +30,15 @@ import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn;
+import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 
+import com.google.common.collect.Iterables;
+
 /**
  * Base event for custom text parsers.
  *
@@ -38,7 +46,16 @@ import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
  */
 public class CustomEvent extends TmfEvent {
 
-    /** Input format key */
+    /** Payload data map key
+     * @since 2.1*/
+    protected enum Key {
+        /** Timestamp input format */
+        TIMESTAMP_INPUT_FORMAT
+    }
+
+    /** Input format key
+     * @deprecated Use {@link Key#TIMESTAMP_INPUT_FORMAT} instead. */
+    @Deprecated
     protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$
 
     /** Empty message */
@@ -56,10 +73,12 @@ public class CustomEvent extends TmfEvent {
     /** The trace to which this event belongs */
     protected CustomTraceDefinition fDefinition;
 
-    /** The payload data of this event, <field name, value> */
-    protected Map<String, String> fData;
-
-    private TmfEventField[] fColumnData;
+    /**
+     * The payload data of this event, where the key is one of: the {@link Tag},
+     * the field name string if the tag is {@link Tag#OTHER}, or
+     * {@link Key#TIMESTAMP_INPUT_FORMAT}.
+     */
+    protected Map<Object, String> fData;
 
     /**
      * Basic constructor.
@@ -212,21 +231,21 @@ public class CustomEvent extends TmfEvent {
      *            The ID/index of the field to display. This corresponds to the
      *            index in the event content.
      * @return The String to display in the cell
+     * @deprecated Use {@link ITmfEventField#getField(String...)} instead.
      */
+    @Deprecated
     public String getEventString(int index) {
-        if (fData != null) {
-            processData();
-        }
-        if (index < 0 || index >= fColumnData.length) {
+        Collection<? extends ITmfEventField> fields = getContent().getFields();
+        if (index < 0 || index >= fields.size()) {
             return ""; //$NON-NLS-1$
         }
 
-        return fColumnData[index].getValue().toString();
+        return nullToEmptyString(checkNotNull(Iterables.get(fields, index)).getValue());
     }
 
     private void processData() {
-        String timestampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP);
-        String timestampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY);
+        String timestampString = fData.get(Tag.TIMESTAMP);
+        String timestampInputFormat = fData.get(Key.TIMESTAMP_INPUT_FORMAT);
         ITmfTimestamp timestamp = null;
         if (timestampInputFormat != null && timestampString != null) {
             TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timestampInputFormat);
@@ -242,24 +261,23 @@ public class CustomEvent extends TmfEvent {
         }
 
         // Update the custom event type of this event if set
-        String eventName = fData.get(CustomTraceDefinition.TAG_EVENT_TYPE);
+        String eventName = fData.get(Tag.EVENT_TYPE);
         ITmfEventType type = getType();
         if (eventName != null && type instanceof CustomEventType) {
             ((CustomEventType) type).setName(eventName);
         }
 
-        int i = 0;
-        fColumnData = new TmfEventField[fDefinition.outputs.size()];
+        List<ITmfEventField> fields = new ArrayList<>(fDefinition.outputs.size());
         for (OutputColumn outputColumn : fDefinition.outputs) {
-            String value = fData.get(outputColumn.name);
-            if (outputColumn.name.equals(CustomTraceDefinition.TAG_TIMESTAMP) && timestamp != null) {
+            Object key = (outputColumn.tag.equals(Tag.OTHER) ? outputColumn.name : outputColumn.tag);
+            if (outputColumn.tag.equals(Tag.TIMESTAMP) && timestamp != null) {
                 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(fDefinition.timeStampOutputFormat);
-                fColumnData[i++] = new TmfEventField(outputColumn.name, timestampFormat.format(timestamp.getValue()), null);
-            } else {
-                fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$
+                fields.add(new TmfEventField(outputColumn.name, timestampFormat.format(timestamp.getValue()), null));
+            } else if (!outputColumn.tag.equals(Tag.EVENT_TYPE)){
+                fields.add(new TmfEventField(outputColumn.name, nullToEmptyString(fData.get(key)), null));
             }
         }
-        setContent(new CustomEventContent(customEventContent.getName(), customEventContent.getValue(), fColumnData));
+        setContent(new CustomEventContent(customEventContent.getName(), customEventContent.getValue(), fields.toArray(new ITmfEventField[0])));
         fData = null;
     }
 
@@ -286,7 +304,7 @@ public class CustomEvent extends TmfEvent {
             return false;
         }
         CustomEvent other = (CustomEvent) obj;
-        if (!equalsNullable(fDefinition, other.fDefinition)) {
+        if (!Objects.equals(fDefinition, other.fDefinition)) {
             return false;
         }
 
@@ -294,11 +312,11 @@ public class CustomEvent extends TmfEvent {
             return false;
         }
 
-        if (!equalsNullable(customEventContent, other.customEventContent)) {
+        if (!Objects.equals(customEventContent, other.customEventContent)) {
             return false;
         }
 
-        if (!equalsNullable(customEventType, other.customEventType)) {
+        if (!Objects.equals(customEventType, other.customEventType)) {
             return false;
         }
         return true;
This page took 0.027576 seconds and 5 git commands to generate.