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