tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;
14
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;
20 import java.util.Map;
21
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;
31
32 /**
33 * Base event for custom text parsers.
34 *
35 * @author Patrick Tassé
36 */
37 public class CustomEvent extends TmfEvent {
38
39 /** Default timestamp scale for text-parser events */
40 public static final byte TIMESTAMP_SCALE = -3;
41
42 /** Input format key */
43 protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$
44
45 /** Empty message */
46 protected static final String NO_MESSAGE = ""; //$NON-NLS-1$
47
48 /** Replacement for the super-class' timestamp field */
49 private ITmfTimestamp customEventTimestamp;
50
51 /** Replacement for the super-class' content field */
52 private ITmfEventField customEventContent;
53
54 /** Replacement for the super-class' type field */
55 private ITmfEventType customEventType;
56
57 /** The trace to which this event belongs */
58 protected CustomTraceDefinition fDefinition;
59
60 /** The payload data of this event, <field name, value> */
61 protected Map<String, String> fData;
62
63 private TmfEventField[] fColumnData;
64
65 /**
66 * Basic constructor.
67 *
68 * @param definition
69 * The trace definition to which this event belongs
70 */
71 public CustomEvent(CustomTraceDefinition definition) {
72 fDefinition = definition;
73 fData = new HashMap<>();
74 }
75
76 /**
77 * Build a new CustomEvent from an existing TmfEvent.
78 *
79 * @param definition
80 * The trace definition to which this event belongs
81 * @param other
82 * The TmfEvent to copy
83 */
84 public CustomEvent(CustomTraceDefinition definition, TmfEvent other) {
85 super(other);
86 fDefinition = definition;
87 fData = new HashMap<>();
88
89 /* Set our overridden fields */
90 customEventTimestamp = other.getTimestamp();
91 customEventContent = other.getContent();
92 customEventType = other.getType();
93 }
94
95 /**
96 * Full constructor
97 *
98 * @param definition
99 * Trace definition of this event
100 * @param parentTrace
101 * Parent trace object
102 * @param timestamp
103 * Timestamp of this event
104 * @param source
105 * Source of the event
106 * @param type
107 * Event type
108 * @param reference
109 * Event reference
110 */
111 public CustomEvent(CustomTraceDefinition definition, ITmfTrace parentTrace,
112 ITmfTimestamp timestamp, String source, TmfEventType type,
113 String reference) {
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<>();
118
119 /* Set our overridden fields */
120 customEventTimestamp = timestamp;
121 customEventContent = null;
122 customEventType = type;
123 }
124
125 // ------------------------------------------------------------------------
126 // Overridden getters
127 // ------------------------------------------------------------------------
128
129 @Override
130 public ITmfTimestamp getTimestamp() {
131 if (fData != null) {
132 processData();
133 }
134 return customEventTimestamp;
135 }
136
137 @Override
138 public ITmfEventField getContent() {
139 return customEventContent;
140 }
141
142 @Override
143 public ITmfEventType getType() {
144 return customEventType;
145 }
146
147 // ------------------------------------------------------------------------
148 // Setters
149 // ------------------------------------------------------------------------
150
151 /**
152 * Set this event's timestamp
153 *
154 * @param timestamp
155 * The new timestamp
156 */
157 protected void setTimestamp(ITmfTimestamp timestamp) {
158 customEventTimestamp = timestamp;
159 }
160
161 /**
162 * Set this event's content
163 *
164 * @param content
165 * The new content
166 */
167 protected void setContent(ITmfEventField content) {
168 customEventContent = content;
169 }
170
171 /**
172 * Set this event's type
173 *
174 * @param type
175 * The new type
176 */
177 protected void setType(ITmfEventType type) {
178 customEventType = type;
179 }
180
181 // ------------------------------------------------------------------------
182 // Other operations
183 // ------------------------------------------------------------------------
184
185 /**
186 * @return The event fields
187 */
188 public TmfEventField[] extractItemFields() {
189 if (fData != null) {
190 processData();
191 }
192 return Arrays.copyOf(fColumnData, fColumnData.length);
193 }
194
195 private void processData() {
196 String timeStampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP);
197 String timeStampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY);
198 Date date = null;
199 if (timeStampInputFormat != null && timeStampString != null) {
200 SimpleDateFormat dateFormat = new SimpleDateFormat(timeStampInputFormat);
201 try {
202 date = dateFormat.parse(timeStampString);
203 setTimestamp(new TmfTimestamp(date.getTime(), TIMESTAMP_SCALE));
204 } catch (ParseException e) {
205 setTimestamp(TmfTimestamp.ZERO);
206 }
207 } else {
208 setTimestamp(TmfTimestamp.ZERO);
209 }
210
211 int i = 0;
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), null);
218 } else {
219 fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : ""), null); //$NON-NLS-1$
220 }
221 }
222 CustomEventContent curContent = (CustomEventContent) getContent();
223 setContent(new CustomEventContent(curContent.getName(), curContent.getValue(), fColumnData));
224 fData = null;
225 }
226
227 @Override
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());
235 return result;
236 }
237
238 @Override
239 public boolean equals(Object obj) {
240 if (this == obj) {
241 return true;
242 }
243 if (!super.equals(obj)) {
244 return false;
245 }
246 if (!(obj instanceof CustomEvent)) {
247 return false;
248 }
249 CustomEvent other = (CustomEvent) obj;
250 if (fDefinition == null) {
251 if (other.fDefinition != null) {
252 return false;
253 }
254 } else if (!fDefinition.equals(other.fDefinition)) {
255 return false;
256 }
257
258 if (customEventTimestamp == null) {
259 if (other.customEventTimestamp != null) {
260 return false;
261 }
262 } else if (!customEventTimestamp.equals(other.customEventTimestamp)) {
263 return false;
264 }
265
266 if (customEventContent == null) {
267 if (other.customEventContent != null) {
268 return false;
269 }
270 } else if (!customEventContent.equals(other.customEventContent)) {
271 return false;
272 }
273
274 if (customEventType == null) {
275 if (other.customEventType != null) {
276 return false;
277 }
278 } else if (!customEventType.equals(other.customEventType)) {
279 return false;
280 }
281
282 return true;
283 }
284
285 }
This page took 0.076894 seconds and 5 git commands to generate.