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