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
.internal
.tmf
.ui
.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
.internal
.tmf
.ui
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
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é
37 public class CustomEvent
extends TmfEvent
{
39 /** Default timestamp scale for text-parser events */
40 public static final byte TIMESTAMP_SCALE
= -3;
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 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 fDefinition
= definition
;
73 fData
= new HashMap
<String
, String
>();
77 * Build a new CustomEvent from an existing TmfEvent.
80 * The trace definition to which this event belongs
82 * The TmfEvent to copy
84 public CustomEvent(CustomTraceDefinition definition
, TmfEvent other
) {
86 fDefinition
= definition
;
87 fData
= new HashMap
<String
, String
>();
89 /* Set our overridden fields */
90 customEventTimestamp
= other
.getTimestamp();
91 customEventContent
= other
.getContent();
92 customEventType
= other
.getType();
99 * Trace definition of this event
101 * Parent trace object
103 * Timestamp of this event
105 * Source of the event
111 public CustomEvent(CustomTraceDefinition definition
, ITmfTrace parentTrace
,
112 ITmfTimestamp timestamp
, String source
, TmfEventType type
,
114 /* Do not use upstream's fields for stuff we override */
115 super(parentTrace
, null, source
, null, null, reference
);
116 fDefinition
= definition
;
117 fData
= new HashMap
<String
, String
>();
119 /* Set our overridden fields */
120 customEventTimestamp
= timestamp
;
121 customEventContent
= null;
122 customEventType
= type
;
125 // ------------------------------------------------------------------------
126 // Overridden getters
127 // ------------------------------------------------------------------------
130 public ITmfTimestamp
getTimestamp() {
134 return customEventTimestamp
;
138 public ITmfEventField
getContent() {
139 return customEventContent
;
143 public ITmfEventType
getType() {
144 return customEventType
;
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
152 * Set this event's timestamp
157 protected void setTimestamp(ITmfTimestamp timestamp
) {
158 customEventTimestamp
= timestamp
;
162 * Set this event's content
167 protected void setContent(ITmfEventField content
) {
168 customEventContent
= content
;
172 * Set this event's type
177 protected void setType(ITmfEventType type
) {
178 customEventType
= type
;
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * @return The event fields
188 public TmfEventField
[] extractItemFields() {
192 return Arrays
.copyOf(fColumnData
, fColumnData
.length
);
195 private void processData() {
196 String timeStampString
= fData
.get(CustomTraceDefinition
.TAG_TIMESTAMP
);
197 String timeStampInputFormat
= fData
.get(TIMESTAMP_INPUT_FORMAT_KEY
);
199 if (timeStampInputFormat
!= null && timeStampString
!= null) {
200 SimpleDateFormat dateFormat
= new SimpleDateFormat(timeStampInputFormat
);
202 date
= dateFormat
.parse(timeStampString
);
203 setTimestamp(new TmfTimestamp(date
.getTime(), TIMESTAMP_SCALE
));
204 } catch (ParseException e
) {
205 setTimestamp(TmfTimestamp
.ZERO
);
208 setTimestamp(TmfTimestamp
.ZERO
);
212 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
213 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
214 String value
= fData
.get(outputColumn
.name
);
215 if (outputColumn
.name
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && date
!= null) {
216 SimpleDateFormat dateFormat
= new SimpleDateFormat(fDefinition
.timeStampOutputFormat
);
217 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, dateFormat
.format(date
));
219 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: "")); //$NON-NLS-1$
222 CustomEventContent curContent
= (CustomEventContent
) getContent();
223 setContent(new CustomEventContent(curContent
.getName(), curContent
.getValue(), fColumnData
));
228 public int hashCode() {
229 final int prime
= 31;
230 int result
= super.hashCode();
231 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
232 result
= prime
* result
+ ((customEventTimestamp
== null) ?
0 : customEventTimestamp
.hashCode());
233 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
234 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
239 public boolean equals(Object obj
) {
243 if (!super.equals(obj
)) {
246 if (!(obj
instanceof CustomEvent
)) {
249 CustomEvent other
= (CustomEvent
) obj
;
250 if (fDefinition
== null) {
251 if (other
.fDefinition
!= null) {
254 } else if (!fDefinition
.equals(other
.fDefinition
)) {
258 if (customEventTimestamp
== null) {
259 if (other
.customEventTimestamp
!= null) {
262 } else if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
266 if (customEventContent
== null) {
267 if (other
.customEventContent
!= null) {
270 } else if (!customEventContent
.equals(other
.customEventContent
)) {
274 if (customEventType
== null) {
275 if (other
.customEventType
!= null) {
278 } else if (!customEventType
.equals(other
.customEventType
)) {