Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
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 | ||
ce2388e0 | 13 | package org.eclipse.linuxtools.internal.ctf.core.trace; |
866e5b51 FC |
14 | |
15 | import java.util.HashMap; | |
16 | import java.util.HashSet; | |
17 | import java.util.Set; | |
18 | ||
19 | import org.eclipse.linuxtools.ctf.core.event.EventDeclaration; | |
866e5b51 | 20 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
ce2388e0 FC |
21 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
22 | import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; | |
866e5b51 FC |
23 | |
24 | /** | |
25 | * <b><u>Stream</u></b> | |
26 | * <p> | |
27 | * Represents a stream in a trace. | |
28 | */ | |
29 | public class Stream { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
35 | ||
36 | /** | |
37 | * The numerical ID of the stream | |
38 | */ | |
39 | private Long id = null; | |
40 | ||
41 | /** | |
42 | * Declarations of the stream-specific structures | |
43 | */ | |
44 | private StructDeclaration packetContextDecl = null; | |
45 | private StructDeclaration eventHeaderDecl = null; | |
46 | private StructDeclaration eventContextDecl = null; | |
47 | ||
48 | /** | |
49 | * The trace to which the stream belongs | |
50 | */ | |
51 | private CTFTrace trace = null; | |
52 | ||
53 | /** | |
54 | * Maps event ids to events | |
55 | */ | |
788ddcbc | 56 | private HashMap<Long, EventDeclaration> events; |
866e5b51 FC |
57 | |
58 | /** | |
59 | * The inputs associated to this stream | |
60 | */ | |
61 | private final Set<StreamInput> inputs = new HashSet<StreamInput>(); | |
62 | ||
63 | // ------------------------------------------------------------------------ | |
64 | // Constructors | |
65 | // ------------------------------------------------------------------------ | |
66 | ||
67 | /** | |
68 | * Constructs a Stream that belongs to a Trace | |
69 | * | |
70 | * @param trace | |
71 | * The trace to which belongs this stream. | |
72 | */ | |
73 | public Stream(CTFTrace trace) { | |
74 | this.trace = trace; | |
75 | } | |
76 | ||
77 | // ------------------------------------------------------------------------ | |
78 | // Getters/Setters/Predicates | |
79 | // ------------------------------------------------------------------------ | |
80 | ||
81 | public void setId(long id) { | |
82 | this.id = id; | |
788ddcbc | 83 | this.events = trace.createEvents(this.id); |
866e5b51 FC |
84 | } |
85 | ||
86 | public Long getId() { | |
87 | return id; | |
88 | } | |
89 | ||
90 | public boolean idIsSet() { | |
91 | return id != null; | |
92 | } | |
93 | ||
94 | public boolean eventHeaderIsSet() { | |
95 | return eventHeaderDecl != null; | |
96 | } | |
97 | ||
98 | public boolean eventContextIsSet() { | |
99 | return eventContextDecl != null; | |
100 | } | |
101 | ||
102 | public boolean packetContextIsSet() { | |
103 | return packetContextDecl != null; | |
104 | } | |
105 | ||
106 | public void setEventHeader(StructDeclaration eventHeader) { | |
107 | this.eventHeaderDecl = eventHeader; | |
108 | } | |
109 | ||
110 | public void setEventContext(StructDeclaration eventContext) { | |
111 | this.eventContextDecl = eventContext; | |
112 | } | |
113 | ||
114 | public void setPacketContext(StructDeclaration packetContext) { | |
115 | this.packetContextDecl = packetContext; | |
116 | } | |
117 | ||
118 | public StructDeclaration getEventHeaderDecl() { | |
119 | return eventHeaderDecl; | |
120 | } | |
121 | ||
122 | public StructDeclaration getEventContextDecl() { | |
123 | return eventContextDecl; | |
124 | } | |
125 | ||
126 | public StructDeclaration getPacketContextDecl() { | |
127 | return packetContextDecl; | |
128 | } | |
129 | ||
130 | public Set<StreamInput> getStreamInputs() { | |
131 | return inputs; | |
132 | } | |
133 | ||
134 | public CTFTrace getTrace() { | |
135 | return trace; | |
136 | } | |
137 | ||
138 | public HashMap<Long, EventDeclaration> getEvents() { | |
139 | return events; | |
140 | } | |
141 | ||
142 | // ------------------------------------------------------------------------ | |
143 | // Operations | |
144 | // ------------------------------------------------------------------------ | |
145 | ||
146 | /** | |
147 | * Adds an event to the event map. | |
148 | * | |
149 | * An event in a stream can omit its id if it is the only event in this | |
150 | * stream. An event for which no id has been specified has a null id. It is | |
151 | * thus not possible to add an event with the null key if the map is not | |
152 | * empty. It is also not possible to add an event to the map if the null key | |
153 | * is present in the map. | |
154 | * | |
155 | * @param event | |
156 | * The event to add. | |
157 | * @throws ParseException | |
158 | */ | |
159 | public void addEvent(EventDeclaration event) throws ParseException { | |
160 | /* | |
161 | * If there is an event without id (the null key), it must be the only | |
162 | * one | |
163 | */ | |
164 | if (events.get(null) != null) { | |
165 | throw new ParseException( | |
166 | "Event without id with multiple events in a stream"); //$NON-NLS-1$ | |
167 | } | |
168 | ||
169 | /* | |
170 | * If there is an event without id (the null key), it must be the only | |
171 | * one | |
172 | */ | |
173 | if ((event.getId() == null) && (events.size() != 0)) { | |
174 | throw new ParseException( | |
175 | "Event without id with multiple events in a stream"); //$NON-NLS-1$ | |
176 | } | |
177 | ||
178 | /* Check if an event with the same ID already exists */ | |
179 | if (events.get(event.getId()) != null) { | |
180 | throw new ParseException("Event id already exists"); //$NON-NLS-1$ | |
181 | } | |
182 | ||
183 | /* Put the event in the map */ | |
184 | events.put(event.getId(), event); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Add an input to this Stream | |
189 | * | |
190 | * @param input | |
191 | * The StreamInput to add. | |
192 | */ | |
193 | public void addInput(StreamInput input) { | |
194 | inputs.add(input); | |
195 | } | |
196 | ||
197 | ||
198 | /* (non-Javadoc) | |
199 | * @see java.lang.Object#toString() | |
200 | */ | |
201 | @Override | |
202 | public String toString() { | |
203 | return "Stream [id=" + id + ", packetContextDecl=" + packetContextDecl //$NON-NLS-1$ //$NON-NLS-2$ | |
204 | + ", eventHeaderDecl=" + eventHeaderDecl //$NON-NLS-1$ | |
205 | + ", eventContextDecl=" + eventContextDecl + ", trace=" + trace //$NON-NLS-1$ //$NON-NLS-2$ | |
206 | + ", events=" + events + ", inputs=" + inputs + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
207 | } | |
208 | } |