TMF: Add support for StructDefinition fields to ctfadaptor
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
19 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
21 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
22 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource;
24 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
25 import org.eclipse.ui.views.properties.IPropertySource;
26
27 /**
28 * A wrapper class around CTF's Event Definition/Declaration that maps all
29 * types of Declaration to native Java types.
30 *
31 * @version 1.0
32 * @author Alexandre Montplaisir
33 * @since 2.0
34 */
35 public final class CtfTmfEvent extends TmfEvent {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
41 static final String NO_STREAM = "No stream"; //$NON-NLS-1$
42 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47
48 private final int sourceCPU;
49 private final long typeId;
50 private final String eventName;
51 private final IEventDeclaration fDeclaration;
52
53 // ------------------------------------------------------------------------
54 // Constructors
55 // ------------------------------------------------------------------------
56
57 /**
58 * Constructor used by {@link CtfTmfEventFactory#createEvent}
59 */
60 CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp,
61 ITmfEventField content, String fileName, int cpu,
62 IEventDeclaration declaration) {
63 super(trace,
64 rank,
65 timestamp,
66 String.valueOf(cpu), // Source
67 null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
68 content,
69 fileName // Reference
70 );
71
72 fDeclaration = declaration;
73 sourceCPU = cpu;
74 typeId = declaration.getId();
75 eventName = declaration.getName();
76 }
77
78 /**
79 * Inner constructor to create "null" events. Don't use this directly in
80 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
81 * instance of an empty event.
82 *
83 * This needs to be public however because it's used in extension points,
84 * and the framework will use this constructor to get the class type.
85 */
86 public CtfTmfEvent() {
87 super(null,
88 ITmfContext.UNKNOWN_RANK,
89 new CtfTmfTimestamp(-1),
90 null,
91 null,
92 new TmfEventField("", new CtfTmfEventField[0]), //$NON-NLS-1$
93 NO_STREAM);
94 this.sourceCPU = -1;
95 this.typeId = -1;
96 this.eventName = EMPTY_CTF_EVENT_NAME;
97 this.fDeclaration = null;
98 }
99
100 // ------------------------------------------------------------------------
101 // Getters/Setters/Predicates
102 // ------------------------------------------------------------------------
103
104 /**
105 * Gets the cpu core the event was recorded on.
106 *
107 * @return The cpu id for a given source. In lttng it's from CPUINFO
108 */
109 public int getCPU() {
110 return this.sourceCPU;
111 }
112
113 /**
114 * Return this event's ID, according to the trace's metadata.
115 *
116 * Watch out, this ID is not constant from one trace to another for the same
117 * event types! Use "getEventName()" for a constant reference.
118 *
119 * @return The event ID
120 */
121 public long getID() {
122 return this.typeId;
123 }
124
125 /**
126 * Gets the name of a current event.
127 *
128 * @return The event name
129 */
130 public String getEventName() {
131 return eventName;
132 }
133
134 @Override
135 public CtfTmfTrace getTrace() {
136 /* Should be of the right type, since we take a CtfTmfTrace at the constructor */
137 return (CtfTmfTrace) super.getTrace();
138 }
139
140 @Override
141 public ITmfEventType getType() {
142 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
143 if (ctfTmfEventType == null) {
144 /* Should only return null the first time */
145 ctfTmfEventType = new CtfTmfEventType(this.getEventName(), this.getContent());
146 }
147 return ctfTmfEventType;
148 }
149
150 /**
151 * List the custom CTF attributes for events of this type.
152 *
153 * @return The list of custom attribute names. Should not be null, but could
154 * be empty.
155 * @since 2.0
156 */
157 public Set<String> listCustomAttributes() {
158 if (fDeclaration == null) {
159 return new HashSet<String>();
160 }
161 return fDeclaration.getCustomAttributes();
162 }
163
164 /**
165 * Get the value of a custom CTF attributes for this event's type.
166 *
167 * @param name
168 * Name of the the custom attribute
169 * @return Value of this attribute, or null if there is no attribute with
170 * that name
171 * @since 2.0
172 */
173 public String getCustomAttribute(String name) {
174 if (fDeclaration == null) {
175 return null;
176 }
177 return fDeclaration.getCustomAttribute(name);
178 }
179
180 /**
181 * @since 2.0
182 */
183 @Override
184 public Object getAdapter(Class adapter) {
185 if (adapter == IPropertySource.class) {
186 return new TmfEventPropertySource(this);
187 }
188 return null;
189 }
190 }
This page took 0.035376 seconds and 6 git commands to generate.