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