1 /*******************************************************************************
2 * Copyright (c) 2011-2013 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
.linuxtools
.internal
.ctf
.core
.event
;
15 import java
.nio
.ByteOrder
;
16 import java
.util
.HashMap
;
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
.types
.Encoding
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.Stream
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.StreamInputReader
;
30 * Representation of one type of event. A bit like "int" or "long" but for trace
33 public class EventDeclaration
implements IEventDeclaration
{
35 /** Id of lost events */
36 public static final long LOST_EVENT_ID
= -1L;
38 /** Id of events when not set */
39 public static final long UNSET_EVENT_ID
= -2L;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
51 * Event context structure declaration
53 private StructDeclaration context
= null;
56 * Event fields structure declaration
58 private StructDeclaration fields
= null;
61 * Event id (can be null if only event in the stream).
63 private Long id
= UNSET_EVENT_ID
;
66 * Stream to which belongs this event.
68 private Stream stream
= null;
71 * Loglevel of an event
73 private long logLevel
;
75 /** Map of this event type's custom CTF attributes */
76 private final Map
<String
, String
> customAttributes
= new HashMap
<String
, String
>();
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
83 * Default constructor. Use the setters afterwards to set the fields
86 public EventDeclaration() {
90 public EventDefinition
createDefinition(StreamInputReader streamInputReader
) {
91 EventDefinition event
= new EventDefinition(this, streamInputReader
);
93 if (context
!= null) {
94 event
.setContext(context
.createDefinition(event
, "context")); //$NON-NLS-1$
97 if (this.fields
!= null) {
98 event
.setFields(this.fields
.createDefinition(event
, "fields")); //$NON-NLS-1$
105 * Creates a "lost" event. This is a synthetic event that is there to show
106 * that there should be something there.
108 * @return the lost event
110 public static synchronized EventDeclaration
getLostEventDeclaration() {
111 EventDeclaration lostEvent
= new EventDeclaration();
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);
115 lostEvent
.fields
= new StructDeclaration(1);
116 lostEvent
.fields
.addField(CTFStrings
.LOST_EVENTS_FIELD
, lostEventsDeclaration
);
117 lostEvent
.fields
.addField(CTFStrings
.LOST_EVENTS_DURATION
, timestampDeclaration
);
118 lostEvent
.id
= LOST_EVENT_ID
;
119 lostEvent
.name
= CTFStrings
.LOST_EVENT_NAME
;
124 // ------------------------------------------------------------------------
125 // Getters/Setters/Predicates
126 // ------------------------------------------------------------------------
129 * Sets a name for an event Declaration
134 public void setName(String name
) {
139 public String
getName() {
144 * Sets the context for an event declaration (see CTF specification)
147 * the context in structdeclaration format
149 public void setContext(StructDeclaration context
) {
150 this.context
= context
;
154 * Sets the fields of an event declaration
157 * the fields in structdeclaration format
159 public void setFields(StructDeclaration fields
) {
160 this.fields
= fields
;
164 public StructDeclaration
getFields() {
169 public StructDeclaration
getContext() {
174 * Sets the id of an event declaration
179 public void setId(long id
) {
184 public Long
getId() {
189 * Sets the stream of an event declaration
195 public void setStream(Stream stream
) {
196 this.stream
= stream
;
200 public Stream
getStream() {
205 * Is the name of the event declaration set
207 * @return is the name set?
209 public boolean nameIsSet() {
216 * @return is the context set
218 public boolean contextIsSet() {
219 return context
!= null;
225 * @return Is the field set?
227 public boolean fieldsIsSet() {
228 return fields
!= null;
234 * @return is the id set?
236 public boolean idIsSet() {
237 return (id
!= null && id
!= UNSET_EVENT_ID
);
243 * @return is the stream set?
245 public boolean streamIsSet() {
246 return stream
!= null;
250 public long getLogLevel() {
260 public void setLogLevel(long level
) {
265 public Set
<String
> getCustomAttributes() {
266 return customAttributes
.keySet();
270 public String
getCustomAttribute(String key
) {
271 return customAttributes
.get(key
);
275 * Sets a custom attribute value.
278 * the key of the attribute
280 * the value of the attribute
283 public void setCustomAttribute(String key
, String value
) {
284 customAttributes
.put(key
, value
);
287 // ------------------------------------------------------------------------
289 // ------------------------------------------------------------------------
292 public boolean equals(Object obj
) {
299 if (!(obj
instanceof EventDeclaration
)) {
302 EventDeclaration other
= (EventDeclaration
) obj
;
303 if (context
== null) {
304 if (other
.context
!= null) {
307 } else if (!context
.equals(other
.context
)) {
310 if (fields
== null) {
311 if (other
.fields
!= null) {
314 } else if (!fields
.equals(other
.fields
)) {
318 if (other
.id
!= null) {
321 } else if (!id
.equals(other
.id
)) {
325 if (other
.name
!= null) {
328 } else if (!name
.equals(other
.name
)) {
331 if (stream
== null) {
332 if (other
.stream
!= null) {
335 } else if (!stream
.equals(other
.stream
)) {
338 if (!customAttributes
.equals(other
.customAttributes
)) {
345 public int hashCode() {
346 final int prime
= 31;
348 result
= (prime
* result
)
349 + ((context
== null) ?
0 : context
.hashCode());
350 result
= (prime
* result
) + ((fields
== null) ?
0 : fields
.hashCode());
351 result
= (prime
* result
) + ((id
== null) ?
0 : id
.hashCode());
352 result
= (prime
* result
) + ((name
== null) ?
0 : name
.hashCode());
353 result
= (prime
* result
) + ((stream
== null) ?
0 : stream
.hashCode());
354 result
= (prime
* result
) + customAttributes
.hashCode();