1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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
.linuxtools
.tmf
.core
.parsers
.custom
;
15 import java
.text
.ParseException
;
16 import java
.text
.SimpleDateFormat
;
17 import java
.util
.Arrays
;
18 import java
.util
.Date
;
19 import java
.util
.HashMap
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 * Base event for custom text parsers.
35 * @author Patrick Tassé
38 public class CustomEvent
extends TmfEvent
{
40 /** Default timestamp scale for text-parser events */
41 public static final byte TIMESTAMP_SCALE
= -3;
43 /** Input format key */
44 protected static final String TIMESTAMP_INPUT_FORMAT_KEY
= "CE_TS_I_F"; //$NON-NLS-1$
47 protected static final String NO_MESSAGE
= ""; //$NON-NLS-1$
49 /** Replacement for the super-class' timestamp field */
50 private ITmfTimestamp customEventTimestamp
;
52 /** Replacement for the super-class' content field */
53 private ITmfEventField customEventContent
;
55 /** Replacement for the super-class' type field */
56 private ITmfEventType customEventType
;
58 /** The trace to which this event belongs */
59 protected CustomTraceDefinition fDefinition
;
61 /** The payload data of this event, <field name, value> */
62 protected Map
<String
, String
> fData
;
64 private TmfEventField
[] fColumnData
;
70 * The trace definition to which this event belongs
72 public CustomEvent(CustomTraceDefinition definition
) {
73 fDefinition
= definition
;
74 fData
= new HashMap
<>();
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
, 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
106 * Source of the event
112 public CustomEvent(CustomTraceDefinition definition
, ITmfTrace parentTrace
,
113 ITmfTimestamp timestamp
, String source
, TmfEventType type
,
115 /* Do not use upstream's fields for stuff we override */
116 super(parentTrace
, null, source
, null, null, reference
);
117 fDefinition
= definition
;
118 fData
= new HashMap
<>();
120 /* Set our overridden fields */
121 customEventTimestamp
= timestamp
;
122 customEventContent
= null;
123 customEventType
= type
;
126 // ------------------------------------------------------------------------
127 // Overridden getters
128 // ------------------------------------------------------------------------
131 public ITmfTimestamp
getTimestamp() {
135 return customEventTimestamp
;
139 public ITmfEventField
getContent() {
140 return customEventContent
;
144 public ITmfEventType
getType() {
145 return customEventType
;
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Set this event's timestamp
158 protected void setTimestamp(ITmfTimestamp timestamp
) {
159 customEventTimestamp
= timestamp
;
163 * Set this event's content
168 protected void setContent(ITmfEventField content
) {
169 customEventContent
= content
;
173 * Set this event's type
178 protected void setType(ITmfEventType type
) {
179 customEventType
= type
;
182 // ------------------------------------------------------------------------
184 // ------------------------------------------------------------------------
187 * @return The event fields
189 public TmfEventField
[] extractItemFields() {
193 return Arrays
.copyOf(fColumnData
, fColumnData
.length
);
196 private void processData() {
197 String timeStampString
= fData
.get(CustomTraceDefinition
.TAG_TIMESTAMP
);
198 String timeStampInputFormat
= fData
.get(TIMESTAMP_INPUT_FORMAT_KEY
);
200 if (timeStampInputFormat
!= null && timeStampString
!= null) {
201 SimpleDateFormat dateFormat
= new SimpleDateFormat(timeStampInputFormat
);
203 date
= dateFormat
.parse(timeStampString
);
204 setTimestamp(new TmfTimestamp(date
.getTime(), TIMESTAMP_SCALE
));
205 } catch (ParseException e
) {
206 setTimestamp(TmfTimestamp
.ZERO
);
209 setTimestamp(TmfTimestamp
.ZERO
);
213 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
214 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
215 String value
= fData
.get(outputColumn
.name
);
216 if (outputColumn
.name
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && date
!= null) {
217 SimpleDateFormat dateFormat
= new SimpleDateFormat(fDefinition
.timeStampOutputFormat
);
218 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, dateFormat
.format(date
), null);
220 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
223 CustomEventContent curContent
= (CustomEventContent
) getContent();
224 setContent(new CustomEventContent(curContent
.getName(), curContent
.getValue(), fColumnData
));
229 public int hashCode() {
230 final int prime
= 31;
231 int result
= super.hashCode();
232 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
233 result
= prime
* result
+ ((customEventTimestamp
== null) ?
0 : customEventTimestamp
.hashCode());
234 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
235 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
240 public boolean equals(Object obj
) {
244 if (!super.equals(obj
)) {
247 if (!(obj
instanceof CustomEvent
)) {
250 CustomEvent other
= (CustomEvent
) obj
;
251 if (fDefinition
== null) {
252 if (other
.fDefinition
!= null) {
255 } else if (!fDefinition
.equals(other
.fDefinition
)) {
259 if (customEventTimestamp
== null) {
260 if (other
.customEventTimestamp
!= null) {
263 } else if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
267 if (customEventContent
== null) {
268 if (other
.customEventContent
!= null) {
271 } else if (!customEventContent
.equals(other
.customEventContent
)) {
275 if (customEventType
== null) {
276 if (other
.customEventType
!= null) {
279 } else if (!customEventType
.equals(other
.customEventType
)) {