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