1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
;
15 import java
.text
.ParseException
;
16 import java
.util
.HashMap
;
18 import java
.util
.Objects
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.Tag
;
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
;
35 * Base event for custom text parsers.
37 * @author Patrick Tassé
39 public class CustomEvent
extends TmfEvent
{
41 /** Payload data map key
44 /** Timestamp input format */
45 TIMESTAMP_INPUT_FORMAT
49 * @deprecated Use {@link Key#TIMESTAMP_INPUT_FORMAT} instead. */
51 protected static final String TIMESTAMP_INPUT_FORMAT_KEY
= "CE_TS_I_F"; //$NON-NLS-1$
54 protected static final String NO_MESSAGE
= ""; //$NON-NLS-1$
56 /** Replacement for the super-class' timestamp field */
57 private @NonNull ITmfTimestamp customEventTimestamp
;
59 /** Replacement for the super-class' content field */
60 private ITmfEventField customEventContent
;
62 /** Replacement for the super-class' type field */
63 private ITmfEventType customEventType
;
65 /** The trace to which this event belongs */
66 protected CustomTraceDefinition fDefinition
;
69 * The payload data of this event, where the key is one of: the {@link Tag},
70 * the field name string if the tag is {@link Tag#OTHER}, or
71 * {@link Key#TIMESTAMP_INPUT_FORMAT}.
73 protected Map
<Object
, String
> fData
;
75 private TmfEventField
[] fColumnData
;
81 * The trace definition to which this event belongs
83 public CustomEvent(CustomTraceDefinition definition
) {
84 super(null, ITmfContext
.UNKNOWN_RANK
, null, null, null);
85 fDefinition
= definition
;
86 fData
= new HashMap
<>();
87 customEventTimestamp
= TmfTimestamp
.ZERO
;
91 * Build a new CustomEvent from an existing TmfEvent.
94 * The trace definition to which this event belongs
96 * The TmfEvent to copy
98 public CustomEvent(CustomTraceDefinition definition
, @NonNull TmfEvent other
) {
100 fDefinition
= definition
;
101 fData
= new HashMap
<>();
103 /* Set our overridden fields */
104 customEventTimestamp
= other
.getTimestamp();
105 customEventContent
= other
.getContent();
106 customEventType
= other
.getType();
113 * Trace definition of this event
115 * Parent trace object
117 * Timestamp of this event
121 public CustomEvent(CustomTraceDefinition definition
, ITmfTrace parentTrace
,
122 ITmfTimestamp timestamp
, TmfEventType type
) {
123 /* Do not use upstream's fields for stuff we override */
124 super(parentTrace
, ITmfContext
.UNKNOWN_RANK
, null, null, null);
125 fDefinition
= definition
;
126 fData
= new HashMap
<>();
128 /* Set our overridden fields */
129 if (timestamp
== null) {
130 customEventTimestamp
= TmfTimestamp
.ZERO
;
132 customEventTimestamp
= timestamp
;
134 customEventContent
= null;
135 customEventType
= type
;
138 // ------------------------------------------------------------------------
139 // Overridden getters
140 // ------------------------------------------------------------------------
143 public ITmfTimestamp
getTimestamp() {
147 return customEventTimestamp
;
151 public ITmfEventField
getContent() {
155 return customEventContent
;
159 public ITmfEventType
getType() {
160 return customEventType
;
164 public String
getName() {
168 return super.getName();
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
176 * Set this event's timestamp
181 protected void setTimestamp(@NonNull ITmfTimestamp timestamp
) {
182 customEventTimestamp
= timestamp
;
186 * Set this event's content
191 protected void setContent(ITmfEventField content
) {
192 customEventContent
= content
;
196 * Get this event's content value.
198 * This does not process the payload data and is therefore safe to call in
199 * the middle of parsing an event.
201 * @return the event's content value.
203 Object
getContentValue() {
204 return customEventContent
.getValue();
208 * Set this event's type
213 protected void setType(ITmfEventType type
) {
214 customEventType
= type
;
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
222 * Get the contents of an event table cell for this event's row.
225 * The ID/index of the field to display. This corresponds to the
226 * index in the event content.
227 * @return The String to display in the cell
229 public String
getEventString(int index
) {
233 if (index
< 0 || index
>= fColumnData
.length
) {
234 return ""; //$NON-NLS-1$
237 return fColumnData
[index
].getValue().toString();
240 private void processData() {
241 String timestampString
= fData
.get(Tag
.TIMESTAMP
);
242 String timestampInputFormat
= fData
.get(Key
.TIMESTAMP_INPUT_FORMAT
);
243 ITmfTimestamp timestamp
= null;
244 if (timestampInputFormat
!= null && timestampString
!= null) {
245 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(timestampInputFormat
);
247 long time
= timestampFormat
.parseValue(timestampString
);
248 timestamp
= TmfTimestamp
.fromNanos(getTrace().getTimestampTransform().transform(time
));
249 setTimestamp(timestamp
);
250 } catch (ParseException e
) {
251 setTimestamp(TmfTimestamp
.ZERO
);
254 setTimestamp(TmfTimestamp
.ZERO
);
257 // Update the custom event type of this event if set
258 String eventName
= fData
.get(Tag
.EVENT_TYPE
);
259 ITmfEventType type
= getType();
260 if (eventName
!= null && type
instanceof CustomEventType
) {
261 ((CustomEventType
) type
).setName(eventName
);
265 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
266 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
267 Object key
= (outputColumn
.tag
.equals(Tag
.OTHER
) ? outputColumn
.name
: outputColumn
.tag
);
268 String value
= fData
.get(key
);
269 if (outputColumn
.tag
.equals(Tag
.TIMESTAMP
) && timestamp
!= null) {
270 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(fDefinition
.timeStampOutputFormat
);
271 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, timestampFormat
.format(timestamp
.getValue()), null);
273 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
276 setContent(new CustomEventContent(customEventContent
.getName(), customEventContent
.getValue(), fColumnData
));
281 public int hashCode() {
282 final int prime
= 31;
283 int result
= super.hashCode();
284 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
285 result
= prime
* result
+ customEventTimestamp
.hashCode();
286 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
287 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
292 public boolean equals(Object obj
) {
296 if (!super.equals(obj
)) {
299 if (!(obj
instanceof CustomEvent
)) {
302 CustomEvent other
= (CustomEvent
) obj
;
303 if (!Objects
.equals(fDefinition
, other
.fDefinition
)) {
307 if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
311 if (!Objects
.equals(customEventContent
, other
.customEventContent
)) {
315 if (!Objects
.equals(customEventType
, other
.customEventType
)) {