5e40f57458d8d55798806fe58d5d12de4732d8dd
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / event / EventDeclaration.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.linuxtools.internal.ctf.core.event;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.linuxtools.ctf.core.CTFStrings;
21 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
22 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
23 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
24 import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
25 import org.eclipse.linuxtools.ctf.core.event.types.Declaration;
26 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
27 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
28 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
29 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
30 import org.eclipse.linuxtools.ctf.core.trace.CTFStream;
31 import org.eclipse.linuxtools.ctf.core.trace.CTFStreamInputReader;
32
33 /**
34 * Representation of one type of event. A bit like "int" or "long" but for trace
35 * events.
36 */
37 public class EventDeclaration implements IEventDeclaration {
38
39 /** Id of lost events */
40 public static final long LOST_EVENT_ID = -1L;
41
42 /** Id of events when not set */
43 public static final long UNSET_EVENT_ID = -2L;
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
49 /**
50 * Name of the event
51 */
52 private String fName;
53
54 /**
55 * Event context structure declaration
56 */
57 private StructDeclaration fContext = null;
58
59 /**
60 * Event fields structure declaration
61 */
62 private StructDeclaration fFields = null;
63
64 /**
65 * Event id (can be null if only event in the stream).
66 */
67 private Long fId = UNSET_EVENT_ID;
68
69 /**
70 * Stream to which belongs this event.
71 */
72 private CTFStream fStream = null;
73
74 /**
75 * Loglevel of an event
76 */
77 private long fLogLevel;
78
79 /** Map of this event type's custom CTF attributes */
80 private final Map<String, String> fCustomAttributes = new HashMap<>();
81
82 // ------------------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------------------
85
86 /**
87 * Default constructor. Use the setters afterwards to set the fields
88 * accordingly.
89 */
90 public EventDeclaration() {
91 }
92
93 @Override
94 public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException {
95 StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl();
96 StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), LexicalScope.STREAM_EVENT_CONTEXT, input) : null;
97 StructDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader();
98 StructDefinition eventContext = fContext != null ? fContext.createDefinition(fStream.getTrace(), LexicalScope.CONTEXT, input) : null;
99 StructDefinition eventPayload = fFields != null ? fFields.createDefinition(fStream.getTrace(), LexicalScope.FIELDS, input) : null;
100
101 // a bit lttng specific
102 // CTF doesn't require a timestamp,
103 // but it's passed to us
104 return new EventDefinition(
105 this,
106 streamInputReader,
107 timestamp,
108 streamEventContext,
109 eventContext,
110 packetContext,
111 eventPayload);
112 }
113
114 /**
115 * Creates a "lost" event. This is a synthetic event that is there to show
116 * that there should be something there.
117 *
118 * @return the lost event
119 */
120 public static synchronized EventDeclaration getLostEventDeclaration() {
121 EventDeclaration lostEvent = new EventDeclaration();
122 String[] fieldNames = new String[] { CTFStrings.LOST_EVENTS_FIELD, CTFStrings.LOST_EVENTS_DURATION };
123 Declaration[] fieldDeclarations = new Declaration[] { IntegerDeclaration.UINT_32B_DECL, IntegerDeclaration.UINT_64B_DECL };
124 lostEvent.fFields = new StructDeclaration(fieldNames, fieldDeclarations);
125 lostEvent.fId = LOST_EVENT_ID;
126 lostEvent.fName = CTFStrings.LOST_EVENT_NAME;
127 return lostEvent;
128 }
129
130 // ------------------------------------------------------------------------
131 // Getters/Setters/Predicates
132 // ------------------------------------------------------------------------
133
134 /**
135 * Sets a name for an event Declaration
136 *
137 * @param name
138 * the name
139 */
140 public void setName(String name) {
141 fName = name;
142 }
143
144 @Override
145 public String getName() {
146 return fName;
147 }
148
149 /**
150 * Sets the context for an event declaration (see CTF specification)
151 *
152 * @param context
153 * the context in structdeclaration format
154 */
155 public void setContext(StructDeclaration context) {
156 fContext = context;
157 }
158
159 /**
160 * Sets the fields of an event declaration
161 *
162 * @param fields
163 * the fields in structdeclaration format
164 */
165 public void setFields(StructDeclaration fields) {
166 fFields = fields;
167 }
168
169 @Override
170 public StructDeclaration getFields() {
171 return fFields;
172 }
173
174 @Override
175 public StructDeclaration getContext() {
176 return fContext;
177 }
178
179 /**
180 * Sets the id of an event declaration
181 *
182 * @param id
183 * the id
184 */
185 public void setId(long id) {
186 fId = id;
187 }
188
189 @Override
190 public Long getId() {
191 return fId;
192 }
193
194 /**
195 * Sets the stream of an event declaration
196 *
197 * @param stream
198 * the stream
199 * @since 2.0
200 */
201 public void setStream(CTFStream stream) {
202 fStream = stream;
203 }
204
205 @Override
206 public CTFStream getStream() {
207 return fStream;
208 }
209
210 /**
211 * Is the name of the event declaration set
212 *
213 * @return is the name set?
214 */
215 public boolean nameIsSet() {
216 return fName != null;
217 }
218
219 /**
220 * Is the context set
221 *
222 * @return is the context set
223 */
224 public boolean contextIsSet() {
225 return fContext != null;
226 }
227
228 /**
229 * Is a field set?
230 *
231 * @return Is the field set?
232 */
233 public boolean fieldsIsSet() {
234 return fFields != null;
235 }
236
237 /**
238 * Is the id set?
239 *
240 * @return is the id set?
241 */
242 public boolean idIsSet() {
243 return (fId != null && fId != UNSET_EVENT_ID);
244 }
245
246 /**
247 * Is the stream set?
248 *
249 * @return is the stream set?
250 */
251 public boolean streamIsSet() {
252 return fStream != null;
253 }
254
255 @Override
256 public long getLogLevel() {
257 return fLogLevel;
258 }
259
260 /**
261 * Sets the log level
262 *
263 * @param level
264 * the log level
265 */
266 public void setLogLevel(long level) {
267 fLogLevel = level;
268 }
269
270 @Override
271 public Set<String> getCustomAttributes() {
272 return fCustomAttributes.keySet();
273 }
274
275 @Override
276 public String getCustomAttribute(String key) {
277 return fCustomAttributes.get(key);
278 }
279
280 /**
281 * Sets a custom attribute value.
282 *
283 * @param key
284 * the key of the attribute
285 * @param value
286 * the value of the attribute
287 * @since 2.0
288 */
289 public void setCustomAttribute(String key, String value) {
290 fCustomAttributes.put(key, value);
291 }
292
293 // ------------------------------------------------------------------------
294 // Operations
295 // ------------------------------------------------------------------------
296
297 @Override
298 public boolean equals(Object obj) {
299 if (this == obj) {
300 return true;
301 }
302 if (obj == null) {
303 return false;
304 }
305 if (!(obj instanceof EventDeclaration)) {
306 return false;
307 }
308 EventDeclaration other = (EventDeclaration) obj;
309 if (fContext == null) {
310 if (other.fContext != null) {
311 return false;
312 }
313 } else if (!fContext.equals(other.fContext)) {
314 return false;
315 }
316 if (fFields == null) {
317 if (other.fFields != null) {
318 return false;
319 }
320 } else if (!fFields.equals(other.fFields)) {
321 return false;
322 }
323 if (fId == null) {
324 if (other.fId != null) {
325 return false;
326 }
327 } else if (!fId.equals(other.fId)) {
328 return false;
329 }
330 if (fName == null) {
331 if (other.fName != null) {
332 return false;
333 }
334 } else if (!fName.equals(other.fName)) {
335 return false;
336 }
337 if (fStream == null) {
338 if (other.fStream != null) {
339 return false;
340 }
341 } else if (!fStream.equals(other.fStream)) {
342 return false;
343 }
344 if (!fCustomAttributes.equals(other.fCustomAttributes)) {
345 return false;
346 }
347 return true;
348 }
349
350 @Override
351 public int hashCode() {
352 final int prime = 31;
353 int result = 1;
354 result = (prime * result)
355 + ((fContext == null) ? 0 : fContext.hashCode());
356 result = (prime * result) + ((fFields == null) ? 0 : fFields.hashCode());
357 result = (prime * result) + ((fId == null) ? 0 : fId.hashCode());
358 result = (prime * result) + ((fName == null) ? 0 : fName.hashCode());
359 result = (prime * result) + ((fStream == null) ? 0 : fStream.hashCode());
360 result = (prime * result) + fCustomAttributes.hashCode();
361 return result;
362 }
363
364 }
This page took 0.037484 seconds and 4 git commands to generate.