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
.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
;
36 * Base event for custom text parsers.
38 * @author Patrick Tassé
40 public class CustomEvent
extends TmfEvent
{
42 /** Input format key */
43 protected static final String TIMESTAMP_INPUT_FORMAT_KEY
= "CE_TS_I_F"; //$NON-NLS-1$
46 protected static final String NO_MESSAGE
= ""; //$NON-NLS-1$
48 /** Replacement for the super-class' timestamp field */
49 private @NonNull ITmfTimestamp customEventTimestamp
;
51 /** Replacement for the super-class' content field */
52 private ITmfEventField customEventContent
;
54 /** Replacement for the super-class' type field */
55 private ITmfEventType customEventType
;
57 /** The trace to which this event belongs */
58 protected CustomTraceDefinition fDefinition
;
60 /** The payload data of this event, <field name, value> */
61 protected Map
<String
, String
> fData
;
63 private TmfEventField
[] fColumnData
;
69 * The trace definition to which this event belongs
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
;
79 * Build a new CustomEvent from an existing TmfEvent.
82 * The trace definition to which this event belongs
84 * The TmfEvent to copy
86 public CustomEvent(CustomTraceDefinition definition
, @NonNull TmfEvent other
) {
88 fDefinition
= definition
;
89 fData
= new HashMap
<>();
91 /* Set our overridden fields */
92 customEventTimestamp
= other
.getTimestamp();
93 customEventContent
= other
.getContent();
94 customEventType
= other
.getType();
101 * Trace definition of this event
103 * Parent trace object
105 * Timestamp of this event
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
<>();
116 /* Set our overridden fields */
117 if (timestamp
== null) {
118 customEventTimestamp
= TmfTimestamp
.ZERO
;
120 customEventTimestamp
= timestamp
;
122 customEventContent
= null;
123 customEventType
= type
;
126 // ------------------------------------------------------------------------
127 // Overridden getters
128 // ------------------------------------------------------------------------
131 public ITmfTimestamp
getTimestamp() {
135 return customEventTimestamp
;
139 public ITmfEventField
getContent() {
143 return customEventContent
;
147 public ITmfEventType
getType() {
148 return customEventType
;
151 // ------------------------------------------------------------------------
153 // ------------------------------------------------------------------------
156 * Set this event's timestamp
161 protected void setTimestamp(@NonNull ITmfTimestamp timestamp
) {
162 customEventTimestamp
= timestamp
;
166 * Set this event's content
171 protected void setContent(ITmfEventField content
) {
172 customEventContent
= content
;
176 * Get this event's content value.
178 * This does not process the payload data and is therefore safe to call in
179 * the middle of parsing an event.
181 * @return the event's content value.
183 Object
getContentValue() {
184 return customEventContent
.getValue();
188 * Set this event's type
193 protected void setType(ITmfEventType type
) {
194 customEventType
= type
;
197 // ------------------------------------------------------------------------
199 // ------------------------------------------------------------------------
202 * Get the contents of an event table cell for this event's row.
205 * The ID/index of the field to display. This corresponds to the
206 * index in the event content.
207 * @return The String to display in the cell
209 public String
getEventString(int index
) {
213 if (index
< 0 || index
>= fColumnData
.length
) {
214 return ""; //$NON-NLS-1$
217 return fColumnData
[index
].getValue().toString();
220 private void processData() {
221 String timestampString
= fData
.get(CustomTraceDefinition
.TAG_TIMESTAMP
);
222 String timestampInputFormat
= fData
.get(TIMESTAMP_INPUT_FORMAT_KEY
);
223 TmfTimestamp timestamp
= null;
224 if (timestampInputFormat
!= null && timestampString
!= null) {
225 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(timestampInputFormat
);
227 long time
= timestampFormat
.parseValue(timestampString
);
228 timestamp
= new TmfNanoTimestamp(getTrace().getTimestampTransform().transform(time
));
229 setTimestamp(timestamp
);
230 } catch (ParseException e
) {
231 setTimestamp(TmfTimestamp
.ZERO
);
234 setTimestamp(TmfTimestamp
.ZERO
);
238 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
239 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
240 String value
= fData
.get(outputColumn
.name
);
241 if (outputColumn
.name
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && timestamp
!= null) {
242 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(fDefinition
.timeStampOutputFormat
);
243 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, timestampFormat
.format(timestamp
.getValue()), null);
245 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
248 setContent(new CustomEventContent(customEventContent
.getName(), customEventContent
.getValue(), fColumnData
));
253 public int hashCode() {
254 final int prime
= 31;
255 int result
= super.hashCode();
256 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
257 result
= prime
* result
+ customEventTimestamp
.hashCode();
258 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
259 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
264 public boolean equals(Object obj
) {
268 if (!super.equals(obj
)) {
271 if (!(obj
instanceof CustomEvent
)) {
274 CustomEvent other
= (CustomEvent
) obj
;
275 if (!equalsNullable(fDefinition
, other
.fDefinition
)) {
279 if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
283 if (!equalsNullable(customEventContent
, other
.customEventContent
)) {
287 if (!equalsNullable(customEventType
, other
.customEventType
)) {