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