1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
.trace
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDeclaration
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
22 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
25 * <b><u>Stream</u></b>
27 * Represents a stream in a trace.
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
37 * The numerical ID of the stream
39 private Long id
= null;
42 * Declarations of the stream-specific structures
44 private StructDeclaration packetContextDecl
= null;
45 private StructDeclaration eventHeaderDecl
= null;
46 private StructDeclaration eventContextDecl
= null;
49 * The trace to which the stream belongs
51 private CTFTrace trace
= null;
54 * Maps event ids to events
56 private HashMap
<Long
, EventDeclaration
> events
= new HashMap
<Long
, EventDeclaration
>();
59 * The inputs associated to this stream
61 private final Set
<StreamInput
> inputs
= new HashSet
<StreamInput
>();
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
68 * Constructs a Stream that belongs to a Trace
71 * The trace to which belongs this stream.
73 public Stream(CTFTrace trace
) {
77 // ------------------------------------------------------------------------
78 // Getters/Setters/Predicates
79 // ------------------------------------------------------------------------
82 * Sets the id of a stream
83 * @param id the id of a stream
85 public void setId(long id
) {
87 this.events
= trace
.createEvents(this.id
);
91 * Gets the id of a stream
92 * @return id the id of a stream
99 * Is the id of a stream set
101 * @return If the ID is set or not
103 public boolean isIdSet() {
109 * @return is the event header set (timestamp and stuff) (see Ctf Spec)
111 public boolean isEventHeaderSet() {
112 return eventHeaderDecl
!= null;
117 * @return is the event context set (pid and stuff) (see Ctf Spec)
119 public boolean isEventContextSet() {
120 return eventContextDecl
!= null;
125 * @return Is the packet context set (see Ctf Spec)
127 public boolean isPacketContextSet() {
128 return packetContextDecl
!= null;
133 * @param eventHeader the current event header for all events in this stream
135 public void setEventHeader(StructDeclaration eventHeader
) {
136 this.eventHeaderDecl
= eventHeader
;
141 * @param eventContext the context for all events in this stream
143 public void setEventContext(StructDeclaration eventContext
) {
144 this.eventContextDecl
= eventContext
;
149 * @param packetContext the packet context for all packets in this stream
151 public void setPacketContext(StructDeclaration packetContext
) {
152 this.packetContextDecl
= packetContext
;
157 * @return the event header declaration in structdeclaration form
159 public StructDeclaration
getEventHeaderDecl() {
160 return eventHeaderDecl
;
165 * @return the event context declaration in structdeclaration form
167 public StructDeclaration
getEventContextDecl() {
168 return eventContextDecl
;
173 * @return the packet context declaration in structdeclaration form
175 public StructDeclaration
getPacketContextDecl() {
176 return packetContextDecl
;
181 * @return the set of all stream inputs for this stream
183 public Set
<StreamInput
> getStreamInputs() {
189 * @return the parent trace
191 public CTFTrace
getTrace() {
197 * @return all the event declarations for this stream, using the id as a key for the hashmap.
199 public HashMap
<Long
, EventDeclaration
> getEvents() {
203 // ------------------------------------------------------------------------
205 // ------------------------------------------------------------------------
208 * Adds an event to the event map.
210 * An event in a stream can omit its id if it is the only event in this
211 * stream. An event for which no id has been specified has a null id. It is
212 * thus not possible to add an event with the null key if the map is not
213 * empty. It is also not possible to add an event to the map if the null key
214 * is present in the map.
218 * @throws ParseException
219 * If there was a problem reading the event or adding it to the
222 public void addEvent(EventDeclaration event
) throws ParseException
{
224 * If there is an event without id (the null key), it must be the only
227 if (events
.get(null) != null) {
228 throw new ParseException(
229 "Event without id with multiple events in a stream"); //$NON-NLS-1$
233 * If there is an event without id (the null key), it must be the only
236 if ((event
.getId() == null) && (events
.size() != 0)) {
237 throw new ParseException(
238 "Event without id with multiple events in a stream"); //$NON-NLS-1$
241 /* Check if an event with the same ID already exists */
242 if (events
.get(event
.getId()) != null) {
243 throw new ParseException("Event id already exists"); //$NON-NLS-1$
246 /* Put the event in the map */
247 events
.put(event
.getId(), event
);
251 * Add an input to this Stream
254 * The StreamInput to add.
256 public void addInput(StreamInput input
) {
262 * @see java.lang.Object#toString()
265 public String
toString() {
266 return "Stream [id=" + id
+ ", packetContextDecl=" + packetContextDecl
//$NON-NLS-1$ //$NON-NLS-2$
267 + ", eventHeaderDecl=" + eventHeaderDecl
//$NON-NLS-1$
268 + ", eventContextDecl=" + eventContextDecl
+ ", trace=" + trace
//$NON-NLS-1$ //$NON-NLS-2$
269 + ", events=" + events
+ ", inputs=" + inputs
+ "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$