1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
;
15 import java
.util
.HashMap
;
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
;
32 * Representation of one type of event. A bit like "int" or "long" but for trace
35 public class EventDeclaration
implements IEventDeclaration
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
47 * Event context structure declaration
49 private StructDeclaration fContext
= null;
52 * Event fields structure declaration
54 private StructDeclaration fFields
= null;
57 * Stream to which belongs this event.
59 private CTFStream fStream
= null;
62 * Loglevel of an event
64 private long fLogLevel
;
66 /** Map of this event type's custom CTF attributes */
67 private final Map
<String
, String
> fCustomAttributes
= new HashMap
<>();
69 private int fId
= (int) UNSET_EVENT_ID
;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * Default constructor. Use the setters afterwards to set the fields
79 public EventDeclaration() {
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;
90 // a bit lttng specific
91 // CTF doesn't require a timestamp,
92 // but it's passed to us
93 return new EventDefinition(
103 // ------------------------------------------------------------------------
104 // Getters/Setters/Predicates
105 // ------------------------------------------------------------------------
108 * Sets a name for an event Declaration
113 public void setName(String name
) {
118 public String
getName() {
123 * Sets the context for an event declaration (see CTF specification)
126 * the context in structdeclaration format
128 public void setContext(StructDeclaration context
) {
133 * Sets the fields of an event declaration
136 * the fields in structdeclaration format
138 public void setFields(StructDeclaration fields
) {
143 public StructDeclaration
getFields() {
148 public StructDeclaration
getContext() {
153 * Sets the id of an event declaration
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$
166 public Long
getId() {
167 return Long
.valueOf(fId
);
171 * Faster get id assuming you have less than a billion event types
173 * @return the event id
180 * Sets the stream of an event declaration
185 public void setStream(CTFStream stream
) {
190 public CTFStream
getStream() {
195 * Is the name of the event declaration set
197 * @return is the name set?
199 public boolean nameIsSet() {
200 return fName
!= null;
206 * @return is the context set
208 public boolean contextIsSet() {
209 return fContext
!= null;
215 * @return Is the field set?
217 public boolean fieldsIsSet() {
218 return fFields
!= null;
224 * @return is the id set?
226 public boolean idIsSet() {
227 return (fId
!= UNSET_EVENT_ID
);
233 * @return is the stream set?
235 public boolean streamIsSet() {
236 return fStream
!= null;
240 public long getLogLevel() {
250 public void setLogLevel(long level
) {
255 public Set
<String
> getCustomAttributes() {
256 return fCustomAttributes
.keySet();
260 public String
getCustomAttribute(String key
) {
261 return fCustomAttributes
.get(key
);
265 * Sets a custom attribute value.
268 * the key of the attribute
270 * the value of the attribute
272 public void setCustomAttribute(String key
, String value
) {
273 fCustomAttributes
.put(key
, value
);
276 // ------------------------------------------------------------------------
278 // ------------------------------------------------------------------------
281 public boolean equals(Object obj
) {
288 if (!(obj
instanceof EventDeclaration
)) {
291 EventDeclaration other
= (EventDeclaration
) obj
;
292 if (fContext
== null) {
293 if (other
.fContext
!= null) {
296 } else if (!fContext
.equals(other
.fContext
)) {
299 if (fFields
== null) {
300 if (other
.fFields
!= null) {
303 } else if (!fFields
.equals(other
.fFields
)) {
306 if (fId
!= (other
.fId
)) {
310 if (other
.fName
!= null) {
313 } else if (!fName
.equals(other
.fName
)) {
316 if (fStream
== null) {
317 if (other
.fStream
!= null) {
320 } else if (!fStream
.equals(other
.fStream
)) {
323 if (!fCustomAttributes
.equals(other
.fCustomAttributes
)) {
330 public int hashCode() {
331 final int prime
= 31;
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();