Fix some null warnings
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / event / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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 * Bernd Hufmann - Updated for source and model lookup interfaces
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ctf.core.event;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
26 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
28 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
29 import org.eclipse.tracecompass.tmf.core.event.ITmfCustomAttributes;
30 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
32 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
33 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
34 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfModelLookup;
35 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
36 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
37 import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
38 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
39
40 /**
41 * A wrapper class around CTF's Event Definition/Declaration that maps all types
42 * of Declaration to native Java types.
43 *
44 * @author Alexandre Montplaisir
45 */
46 @NonNullByDefault
47 public class CtfTmfEvent extends TmfEvent
48 implements ITmfModelLookup, ITmfCustomAttributes {
49
50 // ------------------------------------------------------------------------
51 // Constants
52 // ------------------------------------------------------------------------
53
54 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
55
56 // ------------------------------------------------------------------------
57 // Support attributes
58 // Not part of this event's "definition", but used to populate lazy-loaded
59 // fields.
60 // ------------------------------------------------------------------------
61
62 private final @Nullable IEventDeclaration fEventDeclaration;
63 private final EventDefinition fEvent;
64
65 // ------------------------------------------------------------------------
66 // Attributes
67 // ------------------------------------------------------------------------
68
69 /* Fields that are introduced by and part of this event's definition. */
70 private final int fSourceCpu;
71 private final String fChannel;
72
73 /** Field to override {@link TmfEvent#getName()}, to bypass the type-getting */
74 private final String fEventName;
75
76 /** Lazy-loaded field containing the event's payload */
77 private transient @Nullable ITmfEventField fContent;
78
79 /** Lazy-loaded field for the type, overriding TmfEvent's field */
80 private transient @Nullable CtfTmfEventType fEventType;
81
82 // ------------------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------------------
85
86 /**
87 * Constructor, used by {@link CtfTmfEventFactory#createEvent}.
88 *
89 * Only subclasses should call this. It is imperative that the subclass also
90 * has a constructor with the EXACT same parameter signature, because the
91 * factory will look for a constructor with the same arguments.
92 *
93 * @param trace
94 * The trace to which this event belongs
95 * @param rank
96 * The rank of the event
97 * @param timestamp
98 * The timestamp
99 * @param channel
100 * The CTF channel of this event
101 * @param cpu
102 * The event's CPU
103 * @param declaration
104 * The event declaration
105 * @param eventDefinition
106 * The event definition
107 * @since 2.0
108 */
109 protected CtfTmfEvent(CtfTmfTrace trace,
110 long rank,
111 TmfNanoTimestamp timestamp,
112 String channel,
113 int cpu,
114 IEventDeclaration declaration,
115 EventDefinition eventDefinition) {
116 super(trace,
117 rank,
118 timestamp,
119 /*
120 * Event type. We don't use TmfEvent's field here, we
121 * re-implement getType().
122 */
123 null,
124 /*
125 * Content handled with a lazy-loaded field re-implemented in
126 * getContent().
127 */
128 null);
129
130 fEventDeclaration = declaration;
131 fSourceCpu = cpu;
132 fEventName = checkNotNull(declaration.getName());
133 fEvent = eventDefinition;
134 fChannel = channel;
135 }
136
137 /**
138 * Inner constructor to create "null" events. Don't use this directly in
139 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
140 * get an instance of an empty event.
141 *
142 * There is no need to give higher visibility to this method than package
143 * visible.
144 *
145 * @param trace
146 * The trace associated with this event
147 */
148 CtfTmfEvent(CtfTmfTrace trace) {
149 super(trace,
150 ITmfContext.UNKNOWN_RANK,
151 new TmfNanoTimestamp(-1),
152 null,
153 new TmfEventField("", null, new CtfTmfEventField[0])); //$NON-NLS-1$
154 fSourceCpu = -1;
155 fEventName = EMPTY_CTF_EVENT_NAME;
156 fEventDeclaration = null;
157 fEvent = EventDefinition.NULL_EVENT;
158 fChannel = ""; //$NON-NLS-1$
159 }
160
161 /**
162 * Default constructor. Do not use directly, but it needs to be present
163 * because it's used in extension points, and the framework will use this
164 * constructor to get the class type.
165 *
166 * @deprecated Should not be called by normal code
167 */
168 @Deprecated
169 public CtfTmfEvent() {
170 super();
171 fSourceCpu = -1;
172 fEventName = EMPTY_CTF_EVENT_NAME;
173 fEventDeclaration = null;
174 fEvent = EventDefinition.NULL_EVENT;
175 fChannel = ""; //$NON-NLS-1$
176 }
177
178 // ------------------------------------------------------------------------
179 // Getters/Setters/Predicates
180 // ------------------------------------------------------------------------
181
182 /**
183 * Gets the cpu core the event was recorded on.
184 *
185 * @return The cpu id for a given source. In lttng it's from CPUINFO
186 */
187 public int getCPU() {
188 return fSourceCpu;
189 }
190
191 /**
192 * Return the CTF trace's channel from which this event originates.
193 *
194 * @return The event's channel
195 * @since 2.0
196 */
197 public String getChannel() {
198 return fChannel;
199 }
200
201 /**
202 * Return this event's reference.
203 *
204 * @return The event's reference
205 * @deprecated This method was replaced by {@link #getChannel()}.
206 */
207 @Deprecated
208 public String getReference() {
209 return getChannel();
210 }
211
212 // ------------------------------------------------------------------------
213 // TmfEvent
214 // ------------------------------------------------------------------------
215
216 @Override
217 public CtfTmfTrace getTrace() {
218 /*
219 * Should be of the right type, since we take a CtfTmfTrace at the
220 * constructor
221 */
222 return (CtfTmfTrace) super.getTrace();
223 }
224
225 @Override
226 public synchronized ITmfEventType getType() {
227 CtfTmfEventType type = fEventType;
228 if (type == null) {
229 type = new CtfTmfEventType(fEventName, getContent());
230
231 /*
232 * Register the event type in the owning trace, but only if there is
233 * one
234 */
235 getTrace().registerEventType(type);
236 fEventType = type;
237 }
238 return type;
239 }
240
241 @Override
242 public String getName() {
243 return fEventName;
244 }
245
246 @Override
247 public synchronized ITmfEventField getContent() {
248 ITmfEventField content = fContent;
249 if (content == null) {
250 content = new TmfEventField(
251 ITmfEventField.ROOT_FIELD_ID, null, parseFields(fEvent));
252 fContent = content;
253 }
254 return content;
255 }
256
257 /**
258 * Extract the field information from the structDefinition haze-inducing
259 * mess, and put them into something ITmfEventField can cope with.
260 */
261 private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
262 List<CtfTmfEventField> fields = new ArrayList<>();
263
264 ICompositeDefinition structFields = eventDef.getFields();
265 if (structFields != null) {
266 if (structFields.getFieldNames() != null) {
267 for (String curFieldName : structFields.getFieldNames()) {
268 String fn = checkNotNull(curFieldName);
269 fields.add(CtfTmfEventField.parseField((IDefinition) structFields.getDefinition(fn), fn));
270 }
271 }
272 }
273 /* Add context information as CtfTmfEventField */
274 ICompositeDefinition structContext = eventDef.getContext();
275 if (structContext != null) {
276 for (String contextName : structContext.getFieldNames()) {
277 /* Prefix field name */
278 String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + contextName;
279 fields.add(CtfTmfEventField.parseField((IDefinition) structContext.getDefinition(contextName), curContextName));
280 }
281 }
282
283 return checkNotNull(fields.toArray(new CtfTmfEventField[fields.size()]));
284 }
285
286 // ------------------------------------------------------------------------
287 // ITmfCustomAttributes
288 // ------------------------------------------------------------------------
289
290 @Override
291 public Set<String> listCustomAttributes() {
292 IEventDeclaration declaration = fEventDeclaration;
293 if (declaration == null) {
294 return new HashSet<>();
295 }
296 return declaration.getCustomAttributes();
297 }
298
299 @Override
300 public @Nullable String getCustomAttribute(@Nullable String name) {
301 IEventDeclaration declaration = fEventDeclaration;
302 if (declaration == null) {
303 return null;
304 }
305 return declaration.getCustomAttribute(name);
306 }
307
308 @Override
309 public @Nullable String getModelUri() {
310 return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
311 }
312
313 // ------------------------------------------------------------------------
314 // Object
315 // ------------------------------------------------------------------------
316
317 @Override
318 public int hashCode() {
319 final int prime = 31;
320 int result = super.hashCode();
321 result = prime * result + getCPU();
322 result = prime * result + getChannel().hashCode();
323 return result;
324 }
325
326 @Override
327 public boolean equals(@Nullable Object obj) {
328 if (!super.equals(obj)) {
329 return false;
330 }
331 /* super.equals() checks that the classes are the same */
332 CtfTmfEvent other = checkNotNull((CtfTmfEvent) obj);
333 if (getCPU() != other.getCPU()) {
334 return false;
335 }
336 if (!getChannel().equals(other.getChannel())) {
337 return false;
338 }
339 return true;
340 }
341
342 }
This page took 0.054936 seconds and 5 git commands to generate.