tmf: Don't use ITmfEventField in TmfEventsTable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / parsers / custom / CustomEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
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
13 package org.eclipse.linuxtools.tmf.core.parsers.custom;
14
15 import java.text.ParseException;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
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;
24 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn;
25 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29
30 /**
31 * Base event for custom text parsers.
32 *
33 * @author Patrick Tassé
34 * @since 3.0
35 */
36 public class CustomEvent extends TmfEvent {
37
38 /** Input format key */
39 protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$
40
41 /** Empty message */
42 protected static final String NO_MESSAGE = ""; //$NON-NLS-1$
43
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
53 /** The trace to which this event belongs */
54 protected CustomTraceDefinition fDefinition;
55
56 /** The payload data of this event, <field name, value> */
57 protected Map<String, String> fData;
58
59 private TmfEventField[] fColumnData;
60
61 /**
62 * Basic constructor.
63 *
64 * @param definition
65 * The trace definition to which this event belongs
66 */
67 public CustomEvent(CustomTraceDefinition definition) {
68 fDefinition = definition;
69 fData = new HashMap<>();
70 }
71
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 */
80 public CustomEvent(CustomTraceDefinition definition, TmfEvent other) {
81 super(other);
82 fDefinition = definition;
83 fData = new HashMap<>();
84
85 /* Set our overridden fields */
86 customEventTimestamp = other.getTimestamp();
87 customEventContent = other.getContent();
88 customEventType = other.getType();
89 }
90
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) {
110 /* Do not use upstream's fields for stuff we override */
111 super(parentTrace, null, source, null, null, reference);
112 fDefinition = definition;
113 fData = new HashMap<>();
114
115 /* Set our overridden fields */
116 customEventTimestamp = timestamp;
117 customEventContent = null;
118 customEventType = type;
119 }
120
121 // ------------------------------------------------------------------------
122 // Overridden getters
123 // ------------------------------------------------------------------------
124
125 @Override
126 public ITmfTimestamp getTimestamp() {
127 if (fData != null) {
128 processData();
129 }
130 return customEventTimestamp;
131 }
132
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
181 /**
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
186 */
187 public String[] getEventStrings() {
188 if (fData != null) {
189 processData();
190 }
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;
196 }
197
198 private void processData() {
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);
204 try {
205 long time = timestampFormat.parseValue(timestampString);
206 timestamp = new TmfTimestamp(time, ITmfTimestamp.NANOSECOND_SCALE);
207 setTimestamp(timestamp);
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);
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);
222 } else {
223 fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$
224 }
225 }
226 CustomEventContent curContent = (CustomEventContent) getContent();
227 setContent(new CustomEventContent(curContent.getName(), curContent.getValue(), fColumnData));
228 fData = null;
229 }
230
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());
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());
239 return result;
240 }
241
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 }
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
286 return true;
287 }
288
289 }
This page took 0.04121 seconds and 6 git commands to generate.