Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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 | ||
8e964be1 | 13 | package org.eclipse.linuxtools.internal.ctf.core.event; |
866e5b51 | 14 | |
c26d0fe0 | 15 | import java.nio.ByteOrder; |
8e964be1 MK |
16 | import java.util.HashMap; |
17 | import java.util.Map; | |
18 | import java.util.Set; | |
19 | ||
c26d0fe0 | 20 | import org.eclipse.linuxtools.ctf.core.CTFStrings; |
8e964be1 MK |
21 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
22 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; | |
c26d0fe0 AM |
23 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; |
24 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
866e5b51 | 25 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
486efb2e | 26 | import org.eclipse.linuxtools.ctf.core.trace.Stream; |
866e5b51 FC |
27 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; |
28 | ||
29 | /** | |
be6df2d8 AM |
30 | * Representation of one type of event. A bit like "int" or "long" but for trace |
31 | * events. | |
866e5b51 | 32 | */ |
8e964be1 | 33 | public class EventDeclaration implements IEventDeclaration { |
866e5b51 | 34 | |
b73145e2 JCK |
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 | ||
866e5b51 FC |
41 | // ------------------------------------------------------------------------ |
42 | // Attributes | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
45 | /** | |
46 | * Name of the event | |
47 | */ | |
75259c16 | 48 | private String fName; |
866e5b51 FC |
49 | |
50 | /** | |
51 | * Event context structure declaration | |
52 | */ | |
75259c16 | 53 | private StructDeclaration fContext = null; |
866e5b51 FC |
54 | |
55 | /** | |
56 | * Event fields structure declaration | |
57 | */ | |
75259c16 | 58 | private StructDeclaration fFields = null; |
866e5b51 FC |
59 | |
60 | /** | |
61 | * Event id (can be null if only event in the stream). | |
62 | */ | |
75259c16 | 63 | private Long fId = UNSET_EVENT_ID; |
866e5b51 FC |
64 | |
65 | /** | |
66 | * Stream to which belongs this event. | |
67 | */ | |
75259c16 | 68 | private Stream fStream = null; |
866e5b51 | 69 | |
53047a66 MK |
70 | /** |
71 | * Loglevel of an event | |
72 | */ | |
75259c16 | 73 | private long fLogLevel; |
53047a66 | 74 | |
8e964be1 | 75 | /** Map of this event type's custom CTF attributes */ |
75259c16 | 76 | private final Map<String, String> fCustomAttributes = new HashMap<>(); |
8e964be1 | 77 | |
866e5b51 FC |
78 | // ------------------------------------------------------------------------ |
79 | // Constructors | |
80 | // ------------------------------------------------------------------------ | |
81 | ||
be6df2d8 AM |
82 | /** |
83 | * Default constructor. Use the setters afterwards to set the fields | |
84 | * accordingly. | |
85 | */ | |
8e964be1 MK |
86 | public EventDeclaration() { |
87 | } | |
be6df2d8 | 88 | |
8e964be1 | 89 | @Override |
866e5b51 FC |
90 | public EventDefinition createDefinition(StreamInputReader streamInputReader) { |
91 | EventDefinition event = new EventDefinition(this, streamInputReader); | |
92 | ||
75259c16 MK |
93 | if (fContext != null) { |
94 | event.setContext(fContext.createDefinition(event, "context")); //$NON-NLS-1$ | |
866e5b51 FC |
95 | } |
96 | ||
75259c16 MK |
97 | if (fFields != null) { |
98 | event.setFields(fFields.createDefinition(event, "fields")); //$NON-NLS-1$ | |
866e5b51 FC |
99 | } |
100 | ||
101 | return event; | |
102 | } | |
103 | ||
33656d8e MK |
104 | /** |
105 | * Creates a "lost" event. This is a synthetic event that is there to show | |
106 | * that there should be something there. | |
8e964be1 | 107 | * |
9ac2eb62 | 108 | * @return the lost event |
33656d8e | 109 | */ |
0594c61c | 110 | public static synchronized EventDeclaration getLostEventDeclaration() { |
debcffff | 111 | EventDeclaration lostEvent = new EventDeclaration(); |
c26d0fe0 AM |
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 | ||
75259c16 MK |
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; | |
c26d0fe0 | 120 | |
33656d8e MK |
121 | return lostEvent; |
122 | } | |
123 | ||
866e5b51 FC |
124 | // ------------------------------------------------------------------------ |
125 | // Getters/Setters/Predicates | |
126 | // ------------------------------------------------------------------------ | |
127 | ||
9ac2eb62 MK |
128 | /** |
129 | * Sets a name for an event Declaration | |
8e964be1 MK |
130 | * |
131 | * @param name | |
132 | * the name | |
9ac2eb62 | 133 | */ |
866e5b51 | 134 | public void setName(String name) { |
75259c16 | 135 | fName = name; |
866e5b51 FC |
136 | } |
137 | ||
8e964be1 | 138 | @Override |
866e5b51 | 139 | public String getName() { |
75259c16 | 140 | return fName; |
866e5b51 FC |
141 | } |
142 | ||
9ac2eb62 MK |
143 | /** |
144 | * Sets the context for an event declaration (see CTF specification) | |
8e964be1 MK |
145 | * |
146 | * @param context | |
147 | * the context in structdeclaration format | |
9ac2eb62 | 148 | */ |
866e5b51 | 149 | public void setContext(StructDeclaration context) { |
75259c16 | 150 | fContext = context; |
866e5b51 FC |
151 | } |
152 | ||
9ac2eb62 MK |
153 | /** |
154 | * Sets the fields of an event declaration | |
8e964be1 MK |
155 | * |
156 | * @param fields | |
157 | * the fields in structdeclaration format | |
9ac2eb62 | 158 | */ |
866e5b51 | 159 | public void setFields(StructDeclaration fields) { |
75259c16 | 160 | fFields = fields; |
866e5b51 FC |
161 | } |
162 | ||
8e964be1 | 163 | @Override |
866e5b51 | 164 | public StructDeclaration getFields() { |
75259c16 | 165 | return fFields; |
866e5b51 FC |
166 | } |
167 | ||
8e964be1 | 168 | @Override |
866e5b51 | 169 | public StructDeclaration getContext() { |
75259c16 | 170 | return fContext; |
866e5b51 FC |
171 | } |
172 | ||
9ac2eb62 | 173 | /** |
ecb12461 | 174 | * Sets the id of an event declaration |
8e964be1 MK |
175 | * |
176 | * @param id | |
177 | * the id | |
9ac2eb62 | 178 | */ |
866e5b51 | 179 | public void setId(long id) { |
75259c16 | 180 | fId = id; |
866e5b51 FC |
181 | } |
182 | ||
8e964be1 | 183 | @Override |
866e5b51 | 184 | public Long getId() { |
75259c16 | 185 | return fId; |
866e5b51 FC |
186 | } |
187 | ||
9ac2eb62 | 188 | /** |
ecb12461 | 189 | * Sets the stream of an event declaration |
8e964be1 MK |
190 | * |
191 | * @param stream | |
192 | * the stream | |
486efb2e | 193 | * @since 2.0 |
9ac2eb62 | 194 | */ |
866e5b51 | 195 | public void setStream(Stream stream) { |
75259c16 | 196 | fStream = stream; |
866e5b51 FC |
197 | } |
198 | ||
8e964be1 | 199 | @Override |
866e5b51 | 200 | public Stream getStream() { |
75259c16 | 201 | return fStream; |
866e5b51 FC |
202 | } |
203 | ||
9ac2eb62 MK |
204 | /** |
205 | * Is the name of the event declaration set | |
8e964be1 | 206 | * |
9ac2eb62 MK |
207 | * @return is the name set? |
208 | */ | |
866e5b51 | 209 | public boolean nameIsSet() { |
75259c16 | 210 | return fName != null; |
866e5b51 FC |
211 | } |
212 | ||
9ac2eb62 MK |
213 | /** |
214 | * Is the context set | |
8e964be1 | 215 | * |
9ac2eb62 MK |
216 | * @return is the context set |
217 | */ | |
866e5b51 | 218 | public boolean contextIsSet() { |
75259c16 | 219 | return fContext != null; |
866e5b51 FC |
220 | } |
221 | ||
9ac2eb62 MK |
222 | /** |
223 | * Is a field set? | |
8e964be1 | 224 | * |
9ac2eb62 MK |
225 | * @return Is the field set? |
226 | */ | |
866e5b51 | 227 | public boolean fieldsIsSet() { |
75259c16 | 228 | return fFields != null; |
866e5b51 FC |
229 | } |
230 | ||
9ac2eb62 MK |
231 | /** |
232 | * Is the id set? | |
8e964be1 | 233 | * |
9ac2eb62 MK |
234 | * @return is the id set? |
235 | */ | |
866e5b51 | 236 | public boolean idIsSet() { |
75259c16 | 237 | return (fId != null && fId != UNSET_EVENT_ID); |
866e5b51 FC |
238 | } |
239 | ||
9ac2eb62 MK |
240 | /** |
241 | * Is the stream set? | |
8e964be1 | 242 | * |
9ac2eb62 MK |
243 | * @return is the stream set? |
244 | */ | |
866e5b51 | 245 | public boolean streamIsSet() { |
75259c16 | 246 | return fStream != null; |
866e5b51 FC |
247 | } |
248 | ||
8e964be1 | 249 | @Override |
53047a66 | 250 | public long getLogLevel() { |
75259c16 | 251 | return fLogLevel; |
53047a66 MK |
252 | } |
253 | ||
9ac2eb62 MK |
254 | /** |
255 | * Sets the log level | |
8e964be1 MK |
256 | * |
257 | * @param level | |
258 | * the log level | |
9ac2eb62 | 259 | */ |
8e964be1 | 260 | public void setLogLevel(long level) { |
75259c16 | 261 | fLogLevel = level; |
53047a66 MK |
262 | } |
263 | ||
8e964be1 MK |
264 | @Override |
265 | public Set<String> getCustomAttributes() { | |
75259c16 | 266 | return fCustomAttributes.keySet(); |
8e964be1 MK |
267 | } |
268 | ||
269 | @Override | |
270 | public String getCustomAttribute(String key) { | |
75259c16 | 271 | return fCustomAttributes.get(key); |
8e964be1 MK |
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) { | |
75259c16 | 284 | fCustomAttributes.put(key, value); |
8e964be1 MK |
285 | } |
286 | ||
866e5b51 FC |
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; | |
75259c16 MK |
303 | if (fContext == null) { |
304 | if (other.fContext != null) { | |
866e5b51 FC |
305 | return false; |
306 | } | |
75259c16 | 307 | } else if (!fContext.equals(other.fContext)) { |
866e5b51 FC |
308 | return false; |
309 | } | |
75259c16 MK |
310 | if (fFields == null) { |
311 | if (other.fFields != null) { | |
866e5b51 FC |
312 | return false; |
313 | } | |
75259c16 | 314 | } else if (!fFields.equals(other.fFields)) { |
866e5b51 FC |
315 | return false; |
316 | } | |
75259c16 MK |
317 | if (fId == null) { |
318 | if (other.fId != null) { | |
866e5b51 FC |
319 | return false; |
320 | } | |
75259c16 | 321 | } else if (!fId.equals(other.fId)) { |
866e5b51 FC |
322 | return false; |
323 | } | |
75259c16 MK |
324 | if (fName == null) { |
325 | if (other.fName != null) { | |
866e5b51 FC |
326 | return false; |
327 | } | |
75259c16 | 328 | } else if (!fName.equals(other.fName)) { |
866e5b51 FC |
329 | return false; |
330 | } | |
75259c16 MK |
331 | if (fStream == null) { |
332 | if (other.fStream != null) { | |
866e5b51 FC |
333 | return false; |
334 | } | |
75259c16 | 335 | } else if (!fStream.equals(other.fStream)) { |
866e5b51 FC |
336 | return false; |
337 | } | |
75259c16 | 338 | if (!fCustomAttributes.equals(other.fCustomAttributes)) { |
8e964be1 MK |
339 | return false; |
340 | } | |
866e5b51 FC |
341 | return true; |
342 | } | |
343 | ||
344 | @Override | |
345 | public int hashCode() { | |
346 | final int prime = 31; | |
347 | int result = 1; | |
348 | result = (prime * result) | |
75259c16 MK |
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(); | |
866e5b51 FC |
355 | return result; |
356 | } | |
357 | ||
358 | } |