1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.equalsNullable
;
17 import java
.text
.ParseException
;
18 import java
.util
.HashMap
;
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
;
35 * Base event for custom text parsers.
37 * @author Patrick Tassé
39 public class CustomEvent
extends TmfEvent
{
41 /** Input format key */
42 protected static final String TIMESTAMP_INPUT_FORMAT_KEY
= "CE_TS_I_F"; //$NON-NLS-1$
45 protected static final String NO_MESSAGE
= ""; //$NON-NLS-1$
47 /** Replacement for the super-class' timestamp field */
48 private @NonNull ITmfTimestamp customEventTimestamp
;
50 /** Replacement for the super-class' content field */
51 private ITmfEventField customEventContent
;
53 /** Replacement for the super-class' type field */
54 private ITmfEventType customEventType
;
56 /** The trace to which this event belongs */
57 protected CustomTraceDefinition fDefinition
;
59 /** The payload data of this event, <field name, value> */
60 protected Map
<String
, String
> fData
;
62 private TmfEventField
[] fColumnData
;
68 * The trace definition to which this event belongs
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
;
78 * Build a new CustomEvent from an existing TmfEvent.
81 * The trace definition to which this event belongs
83 * The TmfEvent to copy
85 public CustomEvent(CustomTraceDefinition definition
, @NonNull TmfEvent other
) {
87 fDefinition
= definition
;
88 fData
= new HashMap
<>();
90 /* Set our overridden fields */
91 customEventTimestamp
= other
.getTimestamp();
92 customEventContent
= other
.getContent();
93 customEventType
= other
.getType();
100 * Trace definition of this event
102 * Parent trace object
104 * Timestamp of this event
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
<>();
115 /* Set our overridden fields */
116 if (timestamp
== null) {
117 customEventTimestamp
= TmfTimestamp
.ZERO
;
119 customEventTimestamp
= timestamp
;
121 customEventContent
= null;
122 customEventType
= type
;
125 // ------------------------------------------------------------------------
126 // Overridden getters
127 // ------------------------------------------------------------------------
130 public ITmfTimestamp
getTimestamp() {
134 return customEventTimestamp
;
138 public ITmfEventField
getContent() {
142 return customEventContent
;
146 public ITmfEventType
getType() {
147 return customEventType
;
150 // ------------------------------------------------------------------------
152 // ------------------------------------------------------------------------
155 * Set this event's timestamp
160 protected void setTimestamp(@NonNull ITmfTimestamp timestamp
) {
161 customEventTimestamp
= timestamp
;
165 * Set this event's content
170 protected void setContent(ITmfEventField content
) {
171 customEventContent
= content
;
175 * Get this event's content value.
177 * This does not process the payload data and is therefore safe to call in
178 * the middle of parsing an event.
180 * @return the event's content value.
182 Object
getContentValue() {
183 return customEventContent
.getValue();
187 * Set this event's type
192 protected void setType(ITmfEventType type
) {
193 customEventType
= type
;
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
201 * Get the contents of an event table cell for this event's row.
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
208 public String
getEventString(int index
) {
212 if (index
< 0 || index
>= fColumnData
.length
) {
213 return ""; //$NON-NLS-1$
216 return fColumnData
[index
].getValue().toString();
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
);
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
);
233 setTimestamp(TmfTimestamp
.ZERO
);
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);
244 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
247 setContent(new CustomEventContent(customEventContent
.getName(), customEventContent
.getValue(), fColumnData
));
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());
263 public boolean equals(Object obj
) {
267 if (!super.equals(obj
)) {
270 if (!(obj
instanceof CustomEvent
)) {
273 CustomEvent other
= (CustomEvent
) obj
;
274 if (!equalsNullable(fDefinition
, other
.fDefinition
)) {
278 if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
282 if (!equalsNullable(customEventContent
, other
.customEventContent
)) {
286 if (!equalsNullable(customEventType
, other
.customEventType
)) {