Commit | Line | Data |
---|---|---|
6151d86c | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2010, 2014 Ericsson |
6151d86c PT |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.parsers.custom; |
6151d86c PT |
14 | |
15 | import java.text.ParseException; | |
6151d86c PT |
16 | import java.util.HashMap; |
17 | import java.util.Map; | |
18 | ||
ca5b04ad | 19 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
20 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
21 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventType; | |
22 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
23 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
24 | import org.eclipse.tracecompass.tmf.core.event.TmfEventType; | |
25 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn; | |
26 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
27 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp; | |
28 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
29 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; | |
0c7471fb | 30 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
2bdf0193 | 31 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
6151d86c | 32 | |
a0a88f65 AM |
33 | /** |
34 | * Base event for custom text parsers. | |
35 | * | |
36 | * @author Patrick Tassé | |
47aafe74 | 37 | * @since 3.0 |
a0a88f65 | 38 | */ |
6151d86c PT |
39 | public class CustomEvent extends TmfEvent { |
40 | ||
a0a88f65 | 41 | /** Input format key */ |
6151d86c | 42 | protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$ |
a0a88f65 AM |
43 | |
44 | /** Empty message */ | |
6151d86c | 45 | protected static final String NO_MESSAGE = ""; //$NON-NLS-1$ |
6151d86c | 46 | |
bd54d363 AM |
47 | /** Replacement for the super-class' timestamp field */ |
48 | private ITmfTimestamp customEventTimestamp; | |
49 | ||
50 | /** Replacement for the super-class' content field */ | |
51 | private ITmfEventField customEventContent; | |
52 | ||
53 | /** Replacement for the super-class' type field */ | |
54 | private ITmfEventType customEventType; | |
55 | ||
a0a88f65 | 56 | /** The trace to which this event belongs */ |
6151d86c | 57 | protected CustomTraceDefinition fDefinition; |
a0a88f65 AM |
58 | |
59 | /** The payload data of this event, <field name, value> */ | |
6151d86c | 60 | protected Map<String, String> fData; |
a0a88f65 | 61 | |
6151d86c PT |
62 | private TmfEventField[] fColumnData; |
63 | ||
a0a88f65 AM |
64 | /** |
65 | * Basic constructor. | |
66 | * | |
67 | * @param definition | |
68 | * The trace definition to which this event belongs | |
69 | */ | |
6151d86c | 70 | public CustomEvent(CustomTraceDefinition definition) { |
0c7471fb | 71 | super(null, ITmfContext.UNKNOWN_RANK, null, null, null, null, null); |
6151d86c | 72 | fDefinition = definition; |
507b1336 | 73 | fData = new HashMap<>(); |
6151d86c PT |
74 | } |
75 | ||
a0a88f65 AM |
76 | /** |
77 | * Build a new CustomEvent from an existing TmfEvent. | |
78 | * | |
79 | * @param definition | |
80 | * The trace definition to which this event belongs | |
81 | * @param other | |
82 | * The TmfEvent to copy | |
83 | */ | |
ca5b04ad | 84 | public CustomEvent(CustomTraceDefinition definition, @NonNull TmfEvent other) { |
6151d86c PT |
85 | super(other); |
86 | fDefinition = definition; | |
507b1336 | 87 | fData = new HashMap<>(); |
bd54d363 AM |
88 | |
89 | /* Set our overridden fields */ | |
90 | customEventTimestamp = other.getTimestamp(); | |
91 | customEventContent = other.getContent(); | |
92 | customEventType = other.getType(); | |
6151d86c PT |
93 | } |
94 | ||
a0a88f65 AM |
95 | /** |
96 | * Full constructor | |
97 | * | |
98 | * @param definition | |
99 | * Trace definition of this event | |
100 | * @param parentTrace | |
101 | * Parent trace object | |
102 | * @param timestamp | |
103 | * Timestamp of this event | |
104 | * @param source | |
105 | * Source of the event | |
106 | * @param type | |
107 | * Event type | |
108 | * @param reference | |
109 | * Event reference | |
110 | */ | |
111 | public CustomEvent(CustomTraceDefinition definition, ITmfTrace parentTrace, | |
112 | ITmfTimestamp timestamp, String source, TmfEventType type, | |
113 | String reference) { | |
bd54d363 | 114 | /* Do not use upstream's fields for stuff we override */ |
0c7471fb | 115 | super(parentTrace, ITmfContext.UNKNOWN_RANK, null, source, null, null, reference); |
6151d86c | 116 | fDefinition = definition; |
507b1336 | 117 | fData = new HashMap<>(); |
bd54d363 AM |
118 | |
119 | /* Set our overridden fields */ | |
120 | customEventTimestamp = timestamp; | |
121 | customEventContent = null; | |
122 | customEventType = type; | |
6151d86c PT |
123 | } |
124 | ||
bd54d363 AM |
125 | // ------------------------------------------------------------------------ |
126 | // Overridden getters | |
127 | // ------------------------------------------------------------------------ | |
128 | ||
6151d86c PT |
129 | @Override |
130 | public ITmfTimestamp getTimestamp() { | |
131 | if (fData != null) { | |
132 | processData(); | |
133 | } | |
bd54d363 | 134 | return customEventTimestamp; |
6151d86c PT |
135 | } |
136 | ||
bd54d363 AM |
137 | @Override |
138 | public ITmfEventField getContent() { | |
139 | return customEventContent; | |
140 | } | |
141 | ||
142 | @Override | |
143 | public ITmfEventType getType() { | |
144 | return customEventType; | |
145 | } | |
146 | ||
147 | // ------------------------------------------------------------------------ | |
148 | // Setters | |
149 | // ------------------------------------------------------------------------ | |
150 | ||
151 | /** | |
152 | * Set this event's timestamp | |
153 | * | |
154 | * @param timestamp | |
155 | * The new timestamp | |
156 | */ | |
157 | protected void setTimestamp(ITmfTimestamp timestamp) { | |
158 | customEventTimestamp = timestamp; | |
159 | } | |
160 | ||
161 | /** | |
162 | * Set this event's content | |
163 | * | |
164 | * @param content | |
165 | * The new content | |
166 | */ | |
167 | protected void setContent(ITmfEventField content) { | |
168 | customEventContent = content; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Set this event's type | |
173 | * | |
174 | * @param type | |
175 | * The new type | |
176 | */ | |
177 | protected void setType(ITmfEventType type) { | |
178 | customEventType = type; | |
179 | } | |
180 | ||
181 | // ------------------------------------------------------------------------ | |
182 | // Other operations | |
183 | // ------------------------------------------------------------------------ | |
184 | ||
baafe54c AM |
185 | /** |
186 | * Get the contents of an event table cell for this event's row. | |
187 | * | |
188 | * @param index | |
189 | * The ID/index of the field to display. This corresponds to the | |
190 | * index in the event content. | |
191 | * @return The String to display in the cell | |
192 | * @since 3.1 | |
193 | */ | |
194 | public String getEventString(int index) { | |
195 | if (fData != null) { | |
196 | processData(); | |
197 | } | |
198 | if (index < 0 || index >= fColumnData.length) { | |
199 | return ""; //$NON-NLS-1$ | |
200 | } | |
201 | ||
202 | return fColumnData[index].getValue().toString(); | |
203 | } | |
204 | ||
a0a88f65 | 205 | /** |
cf37ad9f AM |
206 | * Get the contents of the row in the events table corresponding to this |
207 | * event. The order of the elements corresponds to the order of the columns. | |
208 | * | |
209 | * @return The event row entries | |
baafe54c AM |
210 | * @deprecated This should not be used, since this isn't related to the |
211 | * order of columns in the view anymore. You should use | |
212 | * {@link #getEventString(int)} | |
a0a88f65 | 213 | */ |
baafe54c | 214 | @Deprecated |
cf37ad9f | 215 | public String[] getEventStrings() { |
6151d86c PT |
216 | if (fData != null) { |
217 | processData(); | |
218 | } | |
cf37ad9f AM |
219 | String[] entries = new String[fColumnData.length]; |
220 | for (int i = 0; i < fColumnData.length; i++) { | |
221 | entries[i] = fColumnData[i].getValue().toString(); | |
222 | } | |
223 | return entries; | |
6151d86c PT |
224 | } |
225 | ||
226 | private void processData() { | |
738beb68 PT |
227 | String timestampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP); |
228 | String timestampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY); | |
229 | TmfTimestamp timestamp = null; | |
230 | if (timestampInputFormat != null && timestampString != null) { | |
231 | TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timestampInputFormat); | |
6151d86c | 232 | try { |
738beb68 | 233 | long time = timestampFormat.parseValue(timestampString); |
6b44794a | 234 | timestamp = new TmfNanoTimestamp(getTrace().getTimestampTransform().transform(time)); |
738beb68 | 235 | setTimestamp(timestamp); |
6151d86c PT |
236 | } catch (ParseException e) { |
237 | setTimestamp(TmfTimestamp.ZERO); | |
238 | } | |
239 | } else { | |
240 | setTimestamp(TmfTimestamp.ZERO); | |
241 | } | |
242 | ||
243 | int i = 0; | |
244 | fColumnData = new TmfEventField[fDefinition.outputs.size()]; | |
245 | for (OutputColumn outputColumn : fDefinition.outputs) { | |
246 | String value = fData.get(outputColumn.name); | |
738beb68 PT |
247 | if (outputColumn.name.equals(CustomTraceDefinition.TAG_TIMESTAMP) && timestamp != null) { |
248 | TmfTimestampFormat timestampFormat = new TmfTimestampFormat(fDefinition.timeStampOutputFormat); | |
249 | fColumnData[i++] = new TmfEventField(outputColumn.name, timestampFormat.format(timestamp.getValue()), null); | |
6151d86c | 250 | } else { |
214cc822 | 251 | fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$ |
6151d86c PT |
252 | } |
253 | } | |
80349bf7 AM |
254 | CustomEventContent curContent = (CustomEventContent) getContent(); |
255 | setContent(new CustomEventContent(curContent.getName(), curContent.getValue(), fColumnData)); | |
6151d86c PT |
256 | fData = null; |
257 | } | |
258 | ||
6151d86c PT |
259 | @Override |
260 | public int hashCode() { | |
261 | final int prime = 31; | |
262 | int result = super.hashCode(); | |
263 | result = prime * result + ((fDefinition == null) ? 0 : fDefinition.hashCode()); | |
bd54d363 AM |
264 | result = prime * result + ((customEventTimestamp == null) ? 0 : customEventTimestamp.hashCode()); |
265 | result = prime * result + ((customEventContent == null) ? 0 : customEventContent.hashCode()); | |
266 | result = prime * result + ((customEventType == null) ? 0 : customEventType.hashCode()); | |
6151d86c PT |
267 | return result; |
268 | } | |
269 | ||
6151d86c PT |
270 | @Override |
271 | public boolean equals(Object obj) { | |
272 | if (this == obj) { | |
273 | return true; | |
274 | } | |
275 | if (!super.equals(obj)) { | |
276 | return false; | |
277 | } | |
278 | if (!(obj instanceof CustomEvent)) { | |
279 | return false; | |
280 | } | |
281 | CustomEvent other = (CustomEvent) obj; | |
282 | if (fDefinition == null) { | |
283 | if (other.fDefinition != null) { | |
284 | return false; | |
285 | } | |
286 | } else if (!fDefinition.equals(other.fDefinition)) { | |
287 | return false; | |
288 | } | |
bd54d363 AM |
289 | |
290 | if (customEventTimestamp == null) { | |
291 | if (other.customEventTimestamp != null) { | |
292 | return false; | |
293 | } | |
294 | } else if (!customEventTimestamp.equals(other.customEventTimestamp)) { | |
295 | return false; | |
296 | } | |
297 | ||
298 | if (customEventContent == null) { | |
299 | if (other.customEventContent != null) { | |
300 | return false; | |
301 | } | |
302 | } else if (!customEventContent.equals(other.customEventContent)) { | |
303 | return false; | |
304 | } | |
305 | ||
306 | if (customEventType == null) { | |
307 | if (other.customEventType != null) { | |
308 | return false; | |
309 | } | |
310 | } else if (!customEventType.equals(other.customEventType)) { | |
311 | return false; | |
312 | } | |
313 | ||
6151d86c PT |
314 | return true; |
315 | } | |
316 | ||
317 | } |