Fix latest batch of null warnings
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / EventDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2014 Ericsson, Ecole Polytechnique de Montreal and others
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: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.event;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
20 import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
21 import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
22 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
23 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
24 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
25 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
26 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
27 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
28
29 /**
30 * Representation of a particular instance of an event.
31 */
32 public final class EventDefinition implements IDefinitionScope {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 /**
39 * Value representing an unknown cpu number.
40 *
41 * @since 2.0
42 */
43 public static final int UNKNOWN_CPU = -1;
44
45 /**
46 * A null event, can be used for testing or poison pilling
47 */
48 public static final @NonNull EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), UNKNOWN_CPU, -1L, null, null, null, null, null);
49
50 /**
51 * The corresponding event declaration.
52 */
53 private final IEventDeclaration fDeclaration;
54
55 /**
56 * The timestamp of the current event.
57 */
58 private final long fTimestamp;
59
60 private final ICompositeDefinition fEventHeaderDefinition;
61
62 /**
63 * The event context structure definition.
64 */
65 private final ICompositeDefinition fEventContext;
66
67 private final ICompositeDefinition fStreamContext;
68
69 private final ICompositeDefinition fPacketContext;
70
71 /**
72 * The event fields structure definition.
73 */
74 private final ICompositeDefinition fFields;
75
76 /**
77 * The current cpu, could be @link {@link IPacketHeader#UNKNOWN_CPU}
78 */
79 private final int fCpu;
80
81 // ------------------------------------------------------------------------
82 // Constructors
83 // ------------------------------------------------------------------------
84
85 /**
86 * Constructs an event definition.
87 *
88 * @param declaration
89 * The corresponding event declaration
90 * @param cpu
91 * The cpu source of the event. You can use UNKNOWN_CPU if it is
92 * not known.
93 * @param timestamp
94 * event timestamp
95 * @param eventHeaderDefinition
96 * The event header definition, can be null if there is no header
97 * definition
98 * @param eventContext
99 * The event context
100 * @param packetContext
101 * the packet context (the one with content size, not magic number)
102 * @param streamContext
103 * the stream context
104 * @param fields
105 * The event fields
106 * @since 2.0
107 */
108 public EventDefinition(IEventDeclaration declaration,
109 int cpu,
110 long timestamp,
111 ICompositeDefinition eventHeaderDefinition,
112 ICompositeDefinition streamContext,
113 ICompositeDefinition eventContext,
114 ICompositeDefinition packetContext,
115 ICompositeDefinition fields) {
116 fDeclaration = declaration;
117 fEventHeaderDefinition = eventHeaderDefinition;
118 fCpu = cpu;
119 fTimestamp = timestamp;
120 fFields = fields;
121 fEventContext = eventContext;
122 fPacketContext = packetContext;
123 fStreamContext = streamContext;
124 }
125
126 // ------------------------------------------------------------------------
127 // Getters/Setters/Predicates
128 // ------------------------------------------------------------------------
129
130 /**
131 * @since 1.0
132 */
133 @Override
134 public ILexicalScope getScopePath() {
135 String eventName = fDeclaration.getName();
136 if (eventName == null) {
137 return null;
138 }
139 ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
140 if (myScope == null) {
141 myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
142 }
143 return myScope;
144 }
145
146 /**
147 * Gets the declaration (the form) of the data
148 *
149 * @return the event declaration
150 */
151 public IEventDeclaration getDeclaration() {
152 return fDeclaration;
153 }
154
155 /**
156 * Get the event header
157 *
158 * @return the event header
159 * @since 1.1
160 */
161 public ICompositeDefinition getEventHeader() {
162 return fEventHeaderDefinition;
163 }
164
165 /**
166 * Gets the fields of a definition
167 *
168 * @return the fields of a definition in struct form. Can be null.
169 * @since 1.0
170 */
171 public ICompositeDefinition getFields() {
172 return fFields;
173 }
174
175 /**
176 * Gets the context of this event without the context of the stream
177 *
178 * @return the context in struct form
179 * @since 1.0
180 */
181 public ICompositeDefinition getEventContext() {
182 return fEventContext;
183 }
184
185 /**
186 * Gets the context of this event within a stream
187 *
188 * @return the context in struct form
189 * @since 1.0
190 */
191 public ICompositeDefinition getContext() {
192
193 /* Most common case so far */
194 if (fStreamContext == null) {
195 return fEventContext;
196 }
197
198 /* streamContext is not null, but the context of the event is null */
199 if (fEventContext == null) {
200 return fStreamContext;
201 }
202
203 // TODO: cache if this is a performance issue
204
205 /* The stream context and event context are assigned. */
206 StructDeclaration mergedDeclaration = new StructDeclaration(1);
207
208 List<Definition> fieldValues = new ArrayList<>();
209
210 /* Add fields from the stream */
211 List<@NonNull String> fieldNames = fStreamContext.getFieldNames();
212 for (String fieldName : fieldNames) {
213 Definition definition = fStreamContext.getDefinition(fieldName);
214 mergedDeclaration.addField(fieldName, definition.getDeclaration());
215 fieldValues.add(definition);
216 }
217
218 /*
219 * Add fields from the event context, overwrite the stream ones if
220 * needed.
221 */
222 for (String fieldName : fEventContext.getFieldNames()) {
223 Definition definition = fEventContext.getDefinition(fieldName);
224 mergedDeclaration.addField(fieldName, definition.getDeclaration());
225 if (fieldNames.contains(fieldName)) {
226 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
227 } else {
228 fieldValues.add(definition);
229 }
230 }
231 return new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
232 fieldValues.toArray(new Definition[fieldValues.size()]));
233 }
234
235 /**
236 * Gets the context of packet the event is in.
237 *
238 * @return the packet context
239 * @since 1.0
240 */
241 public ICompositeDefinition getPacketContext() {
242 return fPacketContext;
243 }
244
245 /**
246 * gets the CPU the event was generated by. Slightly LTTng specific
247 *
248 * @return The CPU the event was generated by
249 */
250 public int getCPU() {
251 return fCpu;
252 }
253
254 /**
255 * @return the timestamp
256 */
257 public long getTimestamp() {
258 return fTimestamp;
259 }
260
261 // ------------------------------------------------------------------------
262 // Operations
263 // ------------------------------------------------------------------------
264
265 /**
266 * @since 1.0
267 */
268 @Override
269 public IDefinition lookupDefinition(String lookupPath) {
270 if (lookupPath.equals("context")) { //$NON-NLS-1$
271 return fEventContext;
272 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
273 return fFields;
274 } else {
275 return null;
276 }
277 }
278
279 @Override
280 public String toString() {
281 Iterable<String> list;
282 StringBuilder retString = new StringBuilder();
283 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
284
285 retString.append("Event type: ").append(fDeclaration.getName()).append(cr); //$NON-NLS-1$
286 retString.append("Timestamp: ").append(Long.toString(fTimestamp)).append(cr); //$NON-NLS-1$
287
288 if (fEventContext != null) {
289 list = fEventContext.getFieldNames();
290
291 for (String field : list) {
292 retString.append(field).append(" : ").append(fEventContext.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
293 }
294 }
295
296 if (fFields != null) {
297 list = fFields.getFieldNames();
298
299 for (String field : list) {
300 retString.append(field).append(" : ").append(fFields.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
301 }
302 }
303
304 return retString.toString();
305 }
306
307 }
This page took 0.050438 seconds and 5 git commands to generate.