ctf: make DeclarationScopes named [Bug 470846]
[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 @Override
83 public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFException {
84 StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl();
85 StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), ILexicalScope.STREAM_EVENT_CONTEXT, input) : null;
86 ICompositeDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader();
87 StructDefinition eventContext = fContext != null ? fContext.createDefinition(fStream.getTrace(), ILexicalScope.CONTEXT, input) : null;
88 StructDefinition eventPayload = fFields != null ? fFields.createDefinition(fStream.getTrace(), ILexicalScope.FIELDS, input) : null;
89
90 // a bit lttng specific
91 // CTF doesn't require a timestamp,
92 // but it's passed to us
93 return new EventDefinition(
94 this,
95 streamInputReader,
96 timestamp,
97 streamEventContext,
98 eventContext,
99 packetContext,
100 eventPayload);
101 }
102
103 // ------------------------------------------------------------------------
104 // Getters/Setters/Predicates
105 // ------------------------------------------------------------------------
106
107 /**
108 * Sets a name for an event Declaration
109 *
110 * @param name
111 * the name
112 */
113 public void setName(String name) {
114 fName = name;
115 }
116
117 @Override
118 public String getName() {
119 return fName;
120 }
121
122 /**
123 * Sets the context for an event declaration (see CTF specification)
124 *
125 * @param context
126 * the context in structdeclaration format
127 */
128 public void setContext(StructDeclaration context) {
129 fContext = context;
130 }
131
132 /**
133 * Sets the fields of an event declaration
134 *
135 * @param fields
136 * the fields in structdeclaration format
137 */
138 public void setFields(StructDeclaration fields) {
139 fFields = fields;
140 }
141
142 @Override
143 public StructDeclaration getFields() {
144 return fFields;
145 }
146
147 @Override
148 public StructDeclaration getContext() {
149 return fContext;
150 }
151
152 /**
153 * Sets the id of an event declaration
154 *
155 * @param id
156 * the id
157 */
158 public void setId(long id) {
159 if (id < 0 || id > Integer.MAX_VALUE) {
160 throw new IllegalArgumentException("id out of range"); //$NON-NLS-1$
161 }
162 fId = (int) id;
163 }
164
165 @Override
166 public Long getId() {
167 return Long.valueOf(fId);
168 }
169
170 /**
171 * Faster get id assuming you have less than a billion event types
172 *
173 * @return the event id
174 */
175 public int id() {
176 return fId;
177 }
178
179 /**
180 * Sets the stream of an event declaration
181 *
182 * @param stream
183 * the stream
184 */
185 public void setStream(CTFStream stream) {
186 fStream = stream;
187 }
188
189 @Override
190 public CTFStream getStream() {
191 return fStream;
192 }
193
194 /**
195 * Is the name of the event declaration set
196 *
197 * @return is the name set?
198 */
199 public boolean nameIsSet() {
200 return fName != null;
201 }
202
203 /**
204 * Is the context set
205 *
206 * @return is the context set
207 */
208 public boolean contextIsSet() {
209 return fContext != null;
210 }
211
212 /**
213 * Is a field set?
214 *
215 * @return Is the field set?
216 */
217 public boolean fieldsIsSet() {
218 return fFields != null;
219 }
220
221 /**
222 * Is the id set?
223 *
224 * @return is the id set?
225 */
226 public boolean idIsSet() {
227 return (fId != UNSET_EVENT_ID);
228 }
229
230 /**
231 * Is the stream set?
232 *
233 * @return is the stream set?
234 */
235 public boolean streamIsSet() {
236 return fStream != null;
237 }
238
239 @Override
240 public long getLogLevel() {
241 return fLogLevel;
242 }
243
244 /**
245 * Sets the log level
246 *
247 * @param level
248 * the log level
249 */
250 public void setLogLevel(long level) {
251 fLogLevel = level;
252 }
253
254 @Override
255 public Set<String> getCustomAttributes() {
256 return fCustomAttributes.keySet();
257 }
258
259 @Override
260 public String getCustomAttribute(String key) {
261 return fCustomAttributes.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 */
272 public void setCustomAttribute(String key, String value) {
273 fCustomAttributes.put(key, value);
274 }
275
276 // ------------------------------------------------------------------------
277 // Operations
278 // ------------------------------------------------------------------------
279
280 @Override
281 public boolean equals(Object obj) {
282 if (this == obj) {
283 return true;
284 }
285 if (obj == null) {
286 return false;
287 }
288 if (!(obj instanceof EventDeclaration)) {
289 return false;
290 }
291 EventDeclaration other = (EventDeclaration) obj;
292 if (fContext == null) {
293 if (other.fContext != null) {
294 return false;
295 }
296 } else if (!fContext.equals(other.fContext)) {
297 return false;
298 }
299 if (fFields == null) {
300 if (other.fFields != null) {
301 return false;
302 }
303 } else if (!fFields.equals(other.fFields)) {
304 return false;
305 }
306 if (fId != (other.fId)) {
307 return false;
308 }
309 if (fName == null) {
310 if (other.fName != null) {
311 return false;
312 }
313 } else if (!fName.equals(other.fName)) {
314 return false;
315 }
316 if (fStream == null) {
317 if (other.fStream != null) {
318 return false;
319 }
320 } else if (!fStream.equals(other.fStream)) {
321 return false;
322 }
323 if (!fCustomAttributes.equals(other.fCustomAttributes)) {
324 return false;
325 }
326 return true;
327 }
328
329 @Override
330 public int hashCode() {
331 final int prime = 31;
332 int result = 1;
333 result = (prime * result)
334 + ((fContext == null) ? 0 : fContext.hashCode());
335 result = (prime * result) + ((fFields == null) ? 0 : fFields.hashCode());
336 result = (prime * result) + fId;
337 result = (prime * result) + ((fName == null) ? 0 : fName.hashCode());
338 result = (prime * result) + ((fStream == null) ? 0 : fStream.hashCode());
339 result = (prime * result) + fCustomAttributes.hashCode();
340 return result;
341 }
342
343 }
This page took 0.044172 seconds and 5 git commands to generate.