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 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.internal.ctf.core.event; |
866e5b51 | 14 | |
8e964be1 MK |
15 | import java.util.HashMap; |
16 | import java.util.Map; | |
17 | import java.util.Set; | |
18 | ||
a4fa4e36 | 19 | import org.eclipse.jdt.annotation.NonNull; |
680f9173 | 20 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
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; | |
fbe6fa6f | 24 | import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope; |
778bce67 | 25 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; |
f357bcd4 AM |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; |
27 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
f357bcd4 AM |
28 | import org.eclipse.tracecompass.ctf.core.trace.CTFStream; |
29 | import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader; | |
866e5b51 FC |
30 | |
31 | /** | |
be6df2d8 AM |
32 | * Representation of one type of event. A bit like "int" or "long" but for trace |
33 | * events. | |
866e5b51 | 34 | */ |
8e964be1 | 35 | public class EventDeclaration implements IEventDeclaration { |
866e5b51 FC |
36 | |
37 | // ------------------------------------------------------------------------ | |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
42 | * Name of the event | |
43 | */ | |
75259c16 | 44 | private String fName; |
866e5b51 FC |
45 | |
46 | /** | |
47 | * Event context structure declaration | |
48 | */ | |
75259c16 | 49 | private StructDeclaration fContext = null; |
866e5b51 FC |
50 | |
51 | /** | |
52 | * Event fields structure declaration | |
53 | */ | |
75259c16 | 54 | private StructDeclaration fFields = null; |
866e5b51 | 55 | |
866e5b51 FC |
56 | /** |
57 | * Stream to which belongs this event. | |
58 | */ | |
d84419e1 | 59 | private CTFStream fStream = null; |
866e5b51 | 60 | |
53047a66 MK |
61 | /** |
62 | * Loglevel of an event | |
63 | */ | |
75259c16 | 64 | private long fLogLevel; |
53047a66 | 65 | |
8e964be1 | 66 | /** Map of this event type's custom CTF attributes */ |
75259c16 | 67 | private final Map<String, String> fCustomAttributes = new HashMap<>(); |
8e964be1 | 68 | |
5f715709 MK |
69 | private int fId = (int) UNSET_EVENT_ID; |
70 | ||
866e5b51 FC |
71 | // ------------------------------------------------------------------------ |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
be6df2d8 AM |
75 | /** |
76 | * Default constructor. Use the setters afterwards to set the fields | |
77 | * accordingly. | |
78 | */ | |
8e964be1 MK |
79 | public EventDeclaration() { |
80 | } | |
be6df2d8 | 81 | |
8e964be1 | 82 | @Override |
680f9173 | 83 | public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFException { |
a4fa4e36 | 84 | StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl(); |
fbe6fa6f | 85 | StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), ILexicalScope.STREAM_EVENT_CONTEXT, input) : null; |
778bce67 | 86 | ICompositeDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader(); |
fbe6fa6f MK |
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; | |
a4fa4e36 MK |
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); | |
866e5b51 FC |
101 | } |
102 | ||
103 | // ------------------------------------------------------------------------ | |
104 | // Getters/Setters/Predicates | |
105 | // ------------------------------------------------------------------------ | |
106 | ||
9ac2eb62 MK |
107 | /** |
108 | * Sets a name for an event Declaration | |
8e964be1 MK |
109 | * |
110 | * @param name | |
111 | * the name | |
9ac2eb62 | 112 | */ |
866e5b51 | 113 | public void setName(String name) { |
75259c16 | 114 | fName = name; |
866e5b51 FC |
115 | } |
116 | ||
8e964be1 | 117 | @Override |
866e5b51 | 118 | public String getName() { |
75259c16 | 119 | return fName; |
866e5b51 FC |
120 | } |
121 | ||
9ac2eb62 MK |
122 | /** |
123 | * Sets the context for an event declaration (see CTF specification) | |
8e964be1 MK |
124 | * |
125 | * @param context | |
126 | * the context in structdeclaration format | |
9ac2eb62 | 127 | */ |
866e5b51 | 128 | public void setContext(StructDeclaration context) { |
75259c16 | 129 | fContext = context; |
866e5b51 FC |
130 | } |
131 | ||
9ac2eb62 MK |
132 | /** |
133 | * Sets the fields of an event declaration | |
8e964be1 MK |
134 | * |
135 | * @param fields | |
136 | * the fields in structdeclaration format | |
9ac2eb62 | 137 | */ |
866e5b51 | 138 | public void setFields(StructDeclaration fields) { |
75259c16 | 139 | fFields = fields; |
866e5b51 FC |
140 | } |
141 | ||
8e964be1 | 142 | @Override |
866e5b51 | 143 | public StructDeclaration getFields() { |
75259c16 | 144 | return fFields; |
866e5b51 FC |
145 | } |
146 | ||
8e964be1 | 147 | @Override |
866e5b51 | 148 | public StructDeclaration getContext() { |
75259c16 | 149 | return fContext; |
866e5b51 FC |
150 | } |
151 | ||
9ac2eb62 | 152 | /** |
ecb12461 | 153 | * Sets the id of an event declaration |
8e964be1 MK |
154 | * |
155 | * @param id | |
156 | * the id | |
9ac2eb62 | 157 | */ |
866e5b51 | 158 | public void setId(long id) { |
5f715709 MK |
159 | if (id < 0 || id > Integer.MAX_VALUE) { |
160 | throw new IllegalArgumentException("id out of range"); //$NON-NLS-1$ | |
161 | } | |
162 | fId = (int) id; | |
866e5b51 FC |
163 | } |
164 | ||
8e964be1 | 165 | @Override |
866e5b51 | 166 | public Long getId() { |
5f715709 MK |
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() { | |
75259c16 | 176 | return fId; |
866e5b51 FC |
177 | } |
178 | ||
9ac2eb62 | 179 | /** |
ecb12461 | 180 | * Sets the stream of an event declaration |
8e964be1 MK |
181 | * |
182 | * @param stream | |
183 | * the stream | |
9ac2eb62 | 184 | */ |
d84419e1 | 185 | public void setStream(CTFStream stream) { |
75259c16 | 186 | fStream = stream; |
866e5b51 FC |
187 | } |
188 | ||
8e964be1 | 189 | @Override |
d84419e1 | 190 | public CTFStream getStream() { |
75259c16 | 191 | return fStream; |
866e5b51 FC |
192 | } |
193 | ||
9ac2eb62 MK |
194 | /** |
195 | * Is the name of the event declaration set | |
8e964be1 | 196 | * |
9ac2eb62 MK |
197 | * @return is the name set? |
198 | */ | |
866e5b51 | 199 | public boolean nameIsSet() { |
75259c16 | 200 | return fName != null; |
866e5b51 FC |
201 | } |
202 | ||
9ac2eb62 MK |
203 | /** |
204 | * Is the context set | |
8e964be1 | 205 | * |
9ac2eb62 MK |
206 | * @return is the context set |
207 | */ | |
866e5b51 | 208 | public boolean contextIsSet() { |
75259c16 | 209 | return fContext != null; |
866e5b51 FC |
210 | } |
211 | ||
9ac2eb62 MK |
212 | /** |
213 | * Is a field set? | |
8e964be1 | 214 | * |
9ac2eb62 MK |
215 | * @return Is the field set? |
216 | */ | |
866e5b51 | 217 | public boolean fieldsIsSet() { |
75259c16 | 218 | return fFields != null; |
866e5b51 FC |
219 | } |
220 | ||
9ac2eb62 MK |
221 | /** |
222 | * Is the id set? | |
8e964be1 | 223 | * |
9ac2eb62 MK |
224 | * @return is the id set? |
225 | */ | |
866e5b51 | 226 | public boolean idIsSet() { |
8e0c9d81 | 227 | return (fId != UNSET_EVENT_ID); |
866e5b51 FC |
228 | } |
229 | ||
9ac2eb62 MK |
230 | /** |
231 | * Is the stream set? | |
8e964be1 | 232 | * |
9ac2eb62 MK |
233 | * @return is the stream set? |
234 | */ | |
866e5b51 | 235 | public boolean streamIsSet() { |
75259c16 | 236 | return fStream != null; |
866e5b51 FC |
237 | } |
238 | ||
8e964be1 | 239 | @Override |
53047a66 | 240 | public long getLogLevel() { |
75259c16 | 241 | return fLogLevel; |
53047a66 MK |
242 | } |
243 | ||
9ac2eb62 MK |
244 | /** |
245 | * Sets the log level | |
8e964be1 MK |
246 | * |
247 | * @param level | |
248 | * the log level | |
9ac2eb62 | 249 | */ |
8e964be1 | 250 | public void setLogLevel(long level) { |
75259c16 | 251 | fLogLevel = level; |
53047a66 MK |
252 | } |
253 | ||
8e964be1 MK |
254 | @Override |
255 | public Set<String> getCustomAttributes() { | |
75259c16 | 256 | return fCustomAttributes.keySet(); |
8e964be1 MK |
257 | } |
258 | ||
259 | @Override | |
260 | public String getCustomAttribute(String key) { | |
75259c16 | 261 | return fCustomAttributes.get(key); |
8e964be1 MK |
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 | |
8e964be1 MK |
271 | */ |
272 | public void setCustomAttribute(String key, String value) { | |
75259c16 | 273 | fCustomAttributes.put(key, value); |
8e964be1 MK |
274 | } |
275 | ||
866e5b51 FC |
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; | |
75259c16 MK |
292 | if (fContext == null) { |
293 | if (other.fContext != null) { | |
866e5b51 FC |
294 | return false; |
295 | } | |
75259c16 | 296 | } else if (!fContext.equals(other.fContext)) { |
866e5b51 FC |
297 | return false; |
298 | } | |
75259c16 MK |
299 | if (fFields == null) { |
300 | if (other.fFields != null) { | |
866e5b51 FC |
301 | return false; |
302 | } | |
75259c16 | 303 | } else if (!fFields.equals(other.fFields)) { |
866e5b51 FC |
304 | return false; |
305 | } | |
5f715709 | 306 | if (fId != (other.fId)) { |
866e5b51 FC |
307 | return false; |
308 | } | |
75259c16 MK |
309 | if (fName == null) { |
310 | if (other.fName != null) { | |
866e5b51 FC |
311 | return false; |
312 | } | |
75259c16 | 313 | } else if (!fName.equals(other.fName)) { |
866e5b51 FC |
314 | return false; |
315 | } | |
75259c16 MK |
316 | if (fStream == null) { |
317 | if (other.fStream != null) { | |
866e5b51 FC |
318 | return false; |
319 | } | |
75259c16 | 320 | } else if (!fStream.equals(other.fStream)) { |
866e5b51 FC |
321 | return false; |
322 | } | |
75259c16 | 323 | if (!fCustomAttributes.equals(other.fCustomAttributes)) { |
8e964be1 MK |
324 | return false; |
325 | } | |
866e5b51 FC |
326 | return true; |
327 | } | |
328 | ||
329 | @Override | |
330 | public int hashCode() { | |
331 | final int prime = 31; | |
332 | int result = 1; | |
333 | result = (prime * result) | |
75259c16 MK |
334 | + ((fContext == null) ? 0 : fContext.hashCode()); |
335 | result = (prime * result) + ((fFields == null) ? 0 : fFields.hashCode()); | |
5f715709 | 336 | result = (prime * result) + fId; |
75259c16 MK |
337 | result = (prime * result) + ((fName == null) ? 0 : fName.hashCode()); |
338 | result = (prime * result) + ((fStream == null) ? 0 : fStream.hashCode()); | |
339 | result = (prime * result) + fCustomAttributes.hashCode(); | |
866e5b51 FC |
340 | return result; |
341 | } | |
342 | ||
343 | } |