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