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