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
.util
.Arrays
;
17 import java
.util
.HashMap
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampFormat
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
32 * Base event for custom text parsers.
34 * @author Patrick Tassé
37 public class CustomEvent
extends TmfEvent
{
39 /** Input format key */
40 protected static final String TIMESTAMP_INPUT_FORMAT_KEY
= "CE_TS_I_F"; //$NON-NLS-1$
43 protected static final String NO_MESSAGE
= ""; //$NON-NLS-1$
45 /** Replacement for the super-class' timestamp field */
46 private ITmfTimestamp customEventTimestamp
;
48 /** Replacement for the super-class' content field */
49 private ITmfEventField customEventContent
;
51 /** Replacement for the super-class' type field */
52 private ITmfEventType customEventType
;
54 /** The trace to which this event belongs */
55 protected CustomTraceDefinition fDefinition
;
57 /** The payload data of this event, <field name, value> */
58 protected Map
<String
, String
> fData
;
60 private TmfEventField
[] fColumnData
;
66 * The trace definition to which this event belongs
68 public CustomEvent(CustomTraceDefinition definition
) {
69 fDefinition
= definition
;
70 fData
= new HashMap
<>();
74 * Build a new CustomEvent from an existing TmfEvent.
77 * The trace definition to which this event belongs
79 * The TmfEvent to copy
81 public CustomEvent(CustomTraceDefinition definition
, TmfEvent other
) {
83 fDefinition
= definition
;
84 fData
= new HashMap
<>();
86 /* Set our overridden fields */
87 customEventTimestamp
= other
.getTimestamp();
88 customEventContent
= other
.getContent();
89 customEventType
= other
.getType();
96 * Trace definition of this event
100 * Timestamp of this event
102 * Source of the event
108 public CustomEvent(CustomTraceDefinition definition
, ITmfTrace parentTrace
,
109 ITmfTimestamp timestamp
, String source
, TmfEventType type
,
111 /* Do not use upstream's fields for stuff we override */
112 super(parentTrace
, null, source
, null, null, reference
);
113 fDefinition
= definition
;
114 fData
= new HashMap
<>();
116 /* Set our overridden fields */
117 customEventTimestamp
= timestamp
;
118 customEventContent
= null;
119 customEventType
= type
;
122 // ------------------------------------------------------------------------
123 // Overridden getters
124 // ------------------------------------------------------------------------
127 public ITmfTimestamp
getTimestamp() {
131 return customEventTimestamp
;
135 public ITmfEventField
getContent() {
136 return customEventContent
;
140 public ITmfEventType
getType() {
141 return customEventType
;
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * Set this event's timestamp
154 protected void setTimestamp(ITmfTimestamp timestamp
) {
155 customEventTimestamp
= timestamp
;
159 * Set this event's content
164 protected void setContent(ITmfEventField content
) {
165 customEventContent
= content
;
169 * Set this event's type
174 protected void setType(ITmfEventType type
) {
175 customEventType
= type
;
178 // ------------------------------------------------------------------------
180 // ------------------------------------------------------------------------
183 * @return The event fields
185 public TmfEventField
[] extractItemFields() {
189 return Arrays
.copyOf(fColumnData
, fColumnData
.length
);
192 private void processData() {
193 String timestampString
= fData
.get(CustomTraceDefinition
.TAG_TIMESTAMP
);
194 String timestampInputFormat
= fData
.get(TIMESTAMP_INPUT_FORMAT_KEY
);
195 TmfTimestamp timestamp
= null;
196 if (timestampInputFormat
!= null && timestampString
!= null) {
197 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(timestampInputFormat
);
199 long time
= timestampFormat
.parseValue(timestampString
);
200 timestamp
= new TmfTimestamp(time
, ITmfTimestamp
.NANOSECOND_SCALE
);
201 setTimestamp(timestamp
);
202 } catch (ParseException e
) {
203 setTimestamp(TmfTimestamp
.ZERO
);
206 setTimestamp(TmfTimestamp
.ZERO
);
210 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
211 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
212 String value
= fData
.get(outputColumn
.name
);
213 if (outputColumn
.name
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && timestamp
!= null) {
214 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(fDefinition
.timeStampOutputFormat
);
215 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, timestampFormat
.format(timestamp
.getValue()), null);
217 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
220 CustomEventContent curContent
= (CustomEventContent
) getContent();
221 setContent(new CustomEventContent(curContent
.getName(), curContent
.getValue(), fColumnData
));
226 public int hashCode() {
227 final int prime
= 31;
228 int result
= super.hashCode();
229 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
230 result
= prime
* result
+ ((customEventTimestamp
== null) ?
0 : customEventTimestamp
.hashCode());
231 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
232 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
237 public boolean equals(Object obj
) {
241 if (!super.equals(obj
)) {
244 if (!(obj
instanceof CustomEvent
)) {
247 CustomEvent other
= (CustomEvent
) obj
;
248 if (fDefinition
== null) {
249 if (other
.fDefinition
!= null) {
252 } else if (!fDefinition
.equals(other
.fDefinition
)) {
256 if (customEventTimestamp
== null) {
257 if (other
.customEventTimestamp
!= null) {
260 } else if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
264 if (customEventContent
== null) {
265 if (other
.customEventContent
!= null) {
268 } else if (!customEventContent
.equals(other
.customEventContent
)) {
272 if (customEventType
== null) {
273 if (other
.customEventType
!= null) {
276 } else if (!customEventType
.equals(other
.customEventType
)) {