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