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