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