Remove all existing @since annotations
[deliverable/tracecompass.git] / 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.TmfNanoTimestamp;
30 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
31 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
33 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
34
35 /**
36 * Base event for custom text parsers.
37 *
38 * @author Patrick Tassé
39 */
40 public class CustomEvent extends TmfEvent {
41
42 /** Input format key */
43 protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$
44
45 /** Empty message */
46 protected static final String NO_MESSAGE = ""; //$NON-NLS-1$
47
48 /** Replacement for the super-class' timestamp field */
49 private @NonNull ITmfTimestamp customEventTimestamp;
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
57 /** The trace to which this event belongs */
58 protected CustomTraceDefinition fDefinition;
59
60 /** The payload data of this event, <field name, value> */
61 protected Map<String, String> fData;
62
63 private TmfEventField[] fColumnData;
64
65 /**
66 * Basic constructor.
67 *
68 * @param definition
69 * The trace definition to which this event belongs
70 */
71 public CustomEvent(CustomTraceDefinition definition) {
72 super(null, ITmfContext.UNKNOWN_RANK, null, null, null);
73 fDefinition = definition;
74 fData = new HashMap<>();
75 customEventTimestamp = TmfTimestamp.ZERO;
76 }
77
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 */
86 public CustomEvent(CustomTraceDefinition definition, @NonNull TmfEvent other) {
87 super(other);
88 fDefinition = definition;
89 fData = new HashMap<>();
90
91 /* Set our overridden fields */
92 customEventTimestamp = other.getTimestamp();
93 customEventContent = other.getContent();
94 customEventType = other.getType();
95 }
96
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
106 * @param type
107 * Event type
108 */
109 public CustomEvent(CustomTraceDefinition definition, ITmfTrace parentTrace,
110 ITmfTimestamp timestamp, TmfEventType type) {
111 /* Do not use upstream's fields for stuff we override */
112 super(parentTrace, ITmfContext.UNKNOWN_RANK, null, null, null);
113 fDefinition = definition;
114 fData = new HashMap<>();
115
116 /* Set our overridden fields */
117 if (timestamp == null) {
118 customEventTimestamp = TmfTimestamp.ZERO;
119 } else {
120 customEventTimestamp = timestamp;
121 }
122 customEventContent = null;
123 customEventType = type;
124 }
125
126 // ------------------------------------------------------------------------
127 // Overridden getters
128 // ------------------------------------------------------------------------
129
130 @Override
131 public ITmfTimestamp getTimestamp() {
132 if (fData != null) {
133 processData();
134 }
135 return customEventTimestamp;
136 }
137
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(@NonNull 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
186 /**
187 * Get the contents of an event table cell for this event's row.
188 *
189 * @param index
190 * The ID/index of the field to display. This corresponds to the
191 * index in the event content.
192 * @return The String to display in the cell
193 */
194 public String getEventString(int index) {
195 if (fData != null) {
196 processData();
197 }
198 if (index < 0 || index >= fColumnData.length) {
199 return ""; //$NON-NLS-1$
200 }
201
202 return fColumnData[index].getValue().toString();
203 }
204
205 private void processData() {
206 String timestampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP);
207 String timestampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY);
208 TmfTimestamp timestamp = null;
209 if (timestampInputFormat != null && timestampString != null) {
210 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timestampInputFormat);
211 try {
212 long time = timestampFormat.parseValue(timestampString);
213 timestamp = new TmfNanoTimestamp(getTrace().getTimestampTransform().transform(time));
214 setTimestamp(timestamp);
215 } catch (ParseException e) {
216 setTimestamp(TmfTimestamp.ZERO);
217 }
218 } else {
219 setTimestamp(TmfTimestamp.ZERO);
220 }
221
222 int i = 0;
223 fColumnData = new TmfEventField[fDefinition.outputs.size()];
224 for (OutputColumn outputColumn : fDefinition.outputs) {
225 String value = fData.get(outputColumn.name);
226 if (outputColumn.name.equals(CustomTraceDefinition.TAG_TIMESTAMP) && timestamp != null) {
227 TmfTimestampFormat timestampFormat = new TmfTimestampFormat(fDefinition.timeStampOutputFormat);
228 fColumnData[i++] = new TmfEventField(outputColumn.name, timestampFormat.format(timestamp.getValue()), null);
229 } else {
230 fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$
231 }
232 }
233 CustomEventContent curContent = (CustomEventContent) getContent();
234 setContent(new CustomEventContent(curContent.getName(), curContent.getValue(), fColumnData));
235 fData = null;
236 }
237
238 @Override
239 public int hashCode() {
240 final int prime = 31;
241 int result = super.hashCode();
242 result = prime * result + ((fDefinition == null) ? 0 : fDefinition.hashCode());
243 result = prime * result + customEventTimestamp.hashCode();
244 result = prime * result + ((customEventContent == null) ? 0 : customEventContent.hashCode());
245 result = prime * result + ((customEventType == null) ? 0 : customEventType.hashCode());
246 return result;
247 }
248
249 @Override
250 public boolean equals(Object obj) {
251 if (this == obj) {
252 return true;
253 }
254 if (!super.equals(obj)) {
255 return false;
256 }
257 if (!(obj instanceof CustomEvent)) {
258 return false;
259 }
260 CustomEvent other = (CustomEvent) obj;
261 if (!equalsNullable(fDefinition, other.fDefinition)) {
262 return false;
263 }
264
265 if (!customEventTimestamp.equals(other.customEventTimestamp)) {
266 return false;
267 }
268
269 if (!equalsNullable(customEventContent, other.customEventContent)) {
270 return false;
271 }
272
273 if (!equalsNullable(customEventType, other.customEventType)) {
274 return false;
275 }
276 return true;
277 }
278
279 }
This page took 0.050832 seconds and 5 git commands to generate.