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
.linuxtools
.tmf
.core
.parsers
.custom
;
15 import java
.text
.ParseException
;
16 import java
.util
.HashMap
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampFormat
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
31 * Base event for custom text parsers.
33 * @author Patrick Tassé
36 public class CustomEvent
extends TmfEvent
{
38 /** Input format key */
39 protected static final String TIMESTAMP_INPUT_FORMAT_KEY
= "CE_TS_I_F"; //$NON-NLS-1$
42 protected static final String NO_MESSAGE
= ""; //$NON-NLS-1$
44 /** Replacement for the super-class' timestamp field */
45 private ITmfTimestamp customEventTimestamp
;
47 /** Replacement for the super-class' content field */
48 private ITmfEventField customEventContent
;
50 /** Replacement for the super-class' type field */
51 private ITmfEventType customEventType
;
53 /** The trace to which this event belongs */
54 protected CustomTraceDefinition fDefinition
;
56 /** The payload data of this event, <field name, value> */
57 protected Map
<String
, String
> fData
;
59 private TmfEventField
[] fColumnData
;
65 * The trace definition to which this event belongs
67 public CustomEvent(CustomTraceDefinition definition
) {
68 fDefinition
= definition
;
69 fData
= new HashMap
<>();
73 * Build a new CustomEvent from an existing TmfEvent.
76 * The trace definition to which this event belongs
78 * The TmfEvent to copy
80 public CustomEvent(CustomTraceDefinition definition
, TmfEvent other
) {
82 fDefinition
= definition
;
83 fData
= new HashMap
<>();
85 /* Set our overridden fields */
86 customEventTimestamp
= other
.getTimestamp();
87 customEventContent
= other
.getContent();
88 customEventType
= other
.getType();
95 * Trace definition of this event
99 * Timestamp of this event
101 * Source of the event
107 public CustomEvent(CustomTraceDefinition definition
, ITmfTrace parentTrace
,
108 ITmfTimestamp timestamp
, String source
, TmfEventType type
,
110 /* Do not use upstream's fields for stuff we override */
111 super(parentTrace
, null, source
, null, null, reference
);
112 fDefinition
= definition
;
113 fData
= new HashMap
<>();
115 /* Set our overridden fields */
116 customEventTimestamp
= timestamp
;
117 customEventContent
= null;
118 customEventType
= type
;
121 // ------------------------------------------------------------------------
122 // Overridden getters
123 // ------------------------------------------------------------------------
126 public ITmfTimestamp
getTimestamp() {
130 return customEventTimestamp
;
134 public ITmfEventField
getContent() {
135 return customEventContent
;
139 public ITmfEventType
getType() {
140 return customEventType
;
143 // ------------------------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * Set this event's timestamp
153 protected void setTimestamp(ITmfTimestamp timestamp
) {
154 customEventTimestamp
= timestamp
;
158 * Set this event's content
163 protected void setContent(ITmfEventField content
) {
164 customEventContent
= content
;
168 * Set this event's type
173 protected void setType(ITmfEventType type
) {
174 customEventType
= type
;
177 // ------------------------------------------------------------------------
179 // ------------------------------------------------------------------------
182 * Get the contents of the row in the events table corresponding to this
183 * event. The order of the elements corresponds to the order of the columns.
185 * @return The event row entries
187 public String
[] getEventStrings() {
191 String
[] entries
= new String
[fColumnData
.length
];
192 for (int i
= 0; i
< fColumnData
.length
; i
++) {
193 entries
[i
] = fColumnData
[i
].getValue().toString();
198 private void processData() {
199 String timestampString
= fData
.get(CustomTraceDefinition
.TAG_TIMESTAMP
);
200 String timestampInputFormat
= fData
.get(TIMESTAMP_INPUT_FORMAT_KEY
);
201 TmfTimestamp timestamp
= null;
202 if (timestampInputFormat
!= null && timestampString
!= null) {
203 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(timestampInputFormat
);
205 long time
= timestampFormat
.parseValue(timestampString
);
206 timestamp
= new TmfTimestamp(time
, ITmfTimestamp
.NANOSECOND_SCALE
);
207 setTimestamp(timestamp
);
208 } catch (ParseException e
) {
209 setTimestamp(TmfTimestamp
.ZERO
);
212 setTimestamp(TmfTimestamp
.ZERO
);
216 fColumnData
= new TmfEventField
[fDefinition
.outputs
.size()];
217 for (OutputColumn outputColumn
: fDefinition
.outputs
) {
218 String value
= fData
.get(outputColumn
.name
);
219 if (outputColumn
.name
.equals(CustomTraceDefinition
.TAG_TIMESTAMP
) && timestamp
!= null) {
220 TmfTimestampFormat timestampFormat
= new TmfTimestampFormat(fDefinition
.timeStampOutputFormat
);
221 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, timestampFormat
.format(timestamp
.getValue()), null);
223 fColumnData
[i
++] = new TmfEventField(outputColumn
.name
, (value
!= null ? value
: ""), null); //$NON-NLS-1$
226 CustomEventContent curContent
= (CustomEventContent
) getContent();
227 setContent(new CustomEventContent(curContent
.getName(), curContent
.getValue(), fColumnData
));
232 public int hashCode() {
233 final int prime
= 31;
234 int result
= super.hashCode();
235 result
= prime
* result
+ ((fDefinition
== null) ?
0 : fDefinition
.hashCode());
236 result
= prime
* result
+ ((customEventTimestamp
== null) ?
0 : customEventTimestamp
.hashCode());
237 result
= prime
* result
+ ((customEventContent
== null) ?
0 : customEventContent
.hashCode());
238 result
= prime
* result
+ ((customEventType
== null) ?
0 : customEventType
.hashCode());
243 public boolean equals(Object obj
) {
247 if (!super.equals(obj
)) {
250 if (!(obj
instanceof CustomEvent
)) {
253 CustomEvent other
= (CustomEvent
) obj
;
254 if (fDefinition
== null) {
255 if (other
.fDefinition
!= null) {
258 } else if (!fDefinition
.equals(other
.fDefinition
)) {
262 if (customEventTimestamp
== null) {
263 if (other
.customEventTimestamp
!= null) {
266 } else if (!customEventTimestamp
.equals(other
.customEventTimestamp
)) {
270 if (customEventContent
== null) {
271 if (other
.customEventContent
!= null) {
274 } else if (!customEventContent
.equals(other
.customEventContent
)) {
278 if (customEventType
== null) {
279 if (other
.customEventType
!= null) {
282 } else if (!customEventType
.equals(other
.customEventType
)) {