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 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
;
151 public String
getName() {
155 return super.getName();
158 // ------------------------------------------------------------------------
160 // ------------------------------------------------------------------------
163 * Set this event's timestamp
168 protected void setTimestamp(@NonNull ITmfTimestamp timestamp
) {
169 customEventTimestamp
= timestamp
;
173 * Set this event's content
178 protected void setContent(ITmfEventField content
) {
179 customEventContent
= content
;
183 * Get this event's content value.
185 * This does not process the payload data and is therefore safe to call in
186 * the middle of parsing an event.
188 * @return the event's content value.
190 Object
getContentValue() {
191 return customEventContent
.getValue();
195 * Set this event's type
200 protected void setType(ITmfEventType type
) {
201 customEventType
= type
;
204 // ------------------------------------------------------------------------
206 // ------------------------------------------------------------------------
209 * Get the contents of an event table cell for this event's row.
212 * The ID/index of the field to display. This corresponds to the
213 * index in the event content.
214 * @return The String to display in the cell
216 public String
getEventString(int index
) {
220 if (index
< 0 || index
>= fColumnData
.length
) {
221 return ""; //$NON-NLS-1$
224 return fColumnData
[index
].getValue().toString();
227 private void processData() {
228 String timestampString
= fData
.get(CustomTraceDefinition
.TAG_TIMESTAMP
);
229 String timestampInputFormat
= fData
.get(TIMESTAMP_INPUT_FORMAT_KEY
);
230 ITmfTimestamp timestamp
= null;
231 if (timestampInputFormat
!= null && timestampString
!= null) {
232 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(timestampInputFormat
);
234 long time
= timestampFormat
.parseValue(timestampString
);
235 timestamp
= TmfTimestamp
.fromNanos(getTrace().getTimestampTransform().transform(time
));
236 setTimestamp(timestamp
);
237 } catch (ParseException e
) {
238 setTimestamp(TmfTimestamp
.ZERO
);
241 setTimestamp(TmfTimestamp
.ZERO
);
244 // Update the custom event type of this event if set
245 String eventName
= fData
.get(CustomTraceDefinition
.TAG_EVENT_TYPE
);
246 ITmfEventType type
= getType();
247 if (eventName
!= null && type
instanceof CustomEventType
) {
248 ((CustomEventType
) type
).setName(eventName
);
252 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
253 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
254 String value
= fData
.get(outputColumn
.name
);
255 if (outputColumn
.name
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && timestamp
!= null) {
256 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(fDefinition
.timeStampOutputFormat
);
257 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, timestampFormat
.format(timestamp
.getValue()), null);
259 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
262 setContent(new CustomEventContent(customEventContent
.getName(), customEventContent
.getValue(), fColumnData
));
267 public int hashCode() {
268 final int prime
= 31;
269 int result
= super.hashCode();
270 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
271 result
= prime
* result
+ customEventTimestamp
.hashCode();
272 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
273 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
278 public boolean equals(Object obj
) {
282 if (!super.equals(obj
)) {
285 if (!(obj
instanceof CustomEvent
)) {
288 CustomEvent other
= (CustomEvent
) obj
;
289 if (!equalsNullable(fDefinition
, other
.fDefinition
)) {
293 if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
297 if (!equalsNullable(customEventContent
, other
.customEventContent
)) {
301 if (!equalsNullable(customEventType
, other
.customEventType
)) {