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