tmf: Make TmfEvent immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEvent.java
index eda49afb0fb504771e193f2b87ea988672aed83f..c7a2129d743b6ec1bee0f9226caacb077d359c01 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011 Ericsson
+ * Copyright (c) 2011-2013 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -14,18 +14,25 @@ package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map.Entry;
+import java.util.Set;
 
+import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
+import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
+import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource;
+import org.eclipse.ui.views.properties.IPropertySource;
 
 /**
  * A wrapper class around CTF's Event Definition/Declaration that maps all
@@ -33,6 +40,7 @@ import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
  *
  * @version 1.0
  * @author Alexandre Montplaisir
+ * @since 2.0
  */
 public final class CtfTmfEvent implements ITmfEvent, Cloneable {
 
@@ -58,6 +66,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
     private final String fileName;
 
     private final TmfEventField fContent;
+    private final IEventDeclaration fDeclaration;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -85,6 +94,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
             this.fileName = NO_STREAM;
             this.eventName = EMPTY_CTF_EVENT_NAME;
             this.fContent = null;
+            this.fDeclaration = null;
             return;
         }
 
@@ -98,18 +108,21 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
 
         /* Read the fields */
         this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef));
+
+        /* Keep a reference to this event's CTF declaration */
+        this.fDeclaration = eventDef.getDeclaration();
     }
 
     /**
      * Extract the field information from the structDefinition haze-inducing
      * mess, and put them into something ITmfEventField can cope with.
      */
-    private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
+    private CtfTmfEventField[] parseFields(EventDefinition eventDef) {
         List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
 
         StructDefinition structFields = eventDef.getFields();
         HashMap<String, Definition> definitions = structFields.getDefinitions();
-        String curFieldName;
+        String curFieldName = null;
         Definition curFieldDef;
         CtfTmfEventField curField;
         Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator();
@@ -122,6 +135,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
         }
 
         /* Add context information as CtfTmfEventField */
+        long ip = -1;
         StructDefinition structContext = eventDef.getContext();
         if (structContext != null) {
             definitions = structContext.getDefinitions();
@@ -131,6 +145,11 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
             it = definitions.entrySet().iterator();
             while(it.hasNext()) {
                 Entry<String, Definition> entry = it.next();
+                /* This is to get the instruction pointer if available */
+                if (entry.getKey().equals("_ip") && //$NON-NLS-1$
+                        (entry.getValue() instanceof IntegerDefinition)) {
+                    ip = ((IntegerDefinition) entry.getValue()).getValue();
+                }
                 /* Prefix field name to */
                 curContextName = CONTEXT_FIELD_PREFIX + entry.getKey();
                 curContextDef = entry.getValue();
@@ -138,6 +157,20 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
                 fields.add(curContext);
             }
         }
+        /* Add callsite */
+        final String name = eventDef.getDeclaration().getName();
+        List<CTFCallsite> eventList = fTrace.getCTFTrace().getCallsiteCandidates(name);
+        if (!eventList.isEmpty()) {
+            final String callsite = "callsite"; //$NON-NLS-1$
+            if (eventList.size() == 1 || ip == -1) {
+                CTFCallsite cs = eventList.get(0);
+                fields.add(new CTFStringField(cs.toString(), callsite));
+            } else {
+                fields.add(new CTFStringField(
+                        fTrace.getCTFTrace().getCallsite(name, ip).toString(),
+                        callsite));
+            }
+        }
 
         return fields.toArray(new CtfTmfEventField[fields.size()]);
     }
@@ -152,12 +185,8 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
         /* There is only one reference to the trace, so we can shallow-copy it */
         this.fTrace = other.getTrace();
 
-        /*
-         * Copy the timestamp
-         * FIXME This can be switched to a shallow-copy once timestamps are
-         * made immutable.
-         */
-        this.fTimestamp = new CtfTmfTimestamp(other.fTimestamp.getValue());
+        /* Copy the timestamp (immutable) */
+        this.fTimestamp = other.fTimestamp;
 
         /* Primitives, those will be copied by value */
         this.sourceCPU = other.sourceCPU;
@@ -167,8 +196,14 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
         this.eventName = other.eventName;
         this.fileName = other.fileName;
 
-        /* Copy the fields over */
-        this.fContent = other.fContent.clone();
+        /* Copy the fields over (immutable) */
+        this.fContent = other.fContent;
+
+        /*
+         * Copy the reference to the custom attributes (should be the same
+         * object for all events of this type)
+         */
+        this.fDeclaration = other.fDeclaration;
     }
 
     /**
@@ -187,6 +222,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
         this.fileName = NO_STREAM;
         this.eventName = EMPTY_CTF_EVENT_NAME;
         this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$
+        this.fDeclaration = null;
     }
 
     // ------------------------------------------------------------------------
@@ -284,8 +320,49 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable {
         return getChannelName();
     }
 
+    /**
+     * List the custom CTF attributes for events of this type.
+     *
+     * @return The list of custom attribute names. Should not be null, but could
+     *         be empty.
+     * @since 2.0
+     */
+    public Set<String> listCustomAttributes() {
+        if (fDeclaration == null) {
+            return new HashSet<String>();
+        }
+        return fDeclaration.getCustomAttributes();
+    }
+
+    /**
+     * Get the value of a custom CTF attributes for this event's type.
+     *
+     * @param name
+     *            Name of the the custom attribute
+     * @return Value of this attribute, or null if there is no attribute with
+     *         that name
+     * @since 2.0
+     */
+    public String getCustomAttribute(String name) {
+        if (fDeclaration == null) {
+            return null;
+        }
+        return fDeclaration.getCustomAttribute(name);
+    }
+
     @Override
     public CtfTmfEvent clone() {
         return new CtfTmfEvent(this);
     }
+
+    /**
+     * @since 2.0
+     */
+    @Override
+    public Object getAdapter(Class adapter) {
+        if (adapter == IPropertySource.class) {
+            return new TmfEventPropertySource(this);
+        }
+        return null;
+    }
 }
This page took 0.025735 seconds and 5 git commands to generate.