Simplify TmfEvent constructors and update javadoc
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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.ITmfTimestamp;
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.event.TmfTimestamp;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29
30 public class CustomEvent extends TmfEvent {
31
32 protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$
33 protected static final String NO_MESSAGE = ""; //$NON-NLS-1$
34 public static final byte TIMESTAMP_SCALE = -3;
35
36 protected CustomTraceDefinition fDefinition;
37 protected Map<String, String> fData;
38 private TmfEventField[] fColumnData;
39
40 public CustomEvent(CustomTraceDefinition definition) {
41 fDefinition = definition;
42 fData = new HashMap<String, String>();
43 }
44
45 public CustomEvent(CustomTraceDefinition definition, TmfEvent other) {
46 super(other);
47 fDefinition = definition;
48 fData = new HashMap<String, String>();
49 }
50
51 public CustomEvent(CustomTraceDefinition definition, ITmfTrace<?> parentTrace, ITmfTimestamp timestamp, String source, TmfEventType type, String reference) {
52 super(parentTrace, timestamp, source, type, null, reference);
53 fDefinition = definition;
54 fData = new HashMap<String, String>();
55 }
56
57 @Override
58 public ITmfTimestamp getTimestamp() {
59 if (fData != null) processData();
60 return super.getTimestamp();
61 }
62
63 public TmfEventField[] extractItemFields() {
64 if (fData != null) processData();
65 return Arrays.copyOf(fColumnData, fColumnData.length);
66 }
67
68 private void processData() {
69 String timeStampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP);
70 String timeStampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY);
71 Date date = null;
72 if (timeStampInputFormat != null && timeStampString != null) {
73 SimpleDateFormat dateFormat = new SimpleDateFormat(timeStampInputFormat);
74 try {
75 date = dateFormat.parse(timeStampString);
76 setTimestamp(new TmfTimestamp(date.getTime(), TIMESTAMP_SCALE));
77 } catch (ParseException e) {
78 setTimestamp(TmfTimestamp.ZERO);
79 }
80 } else {
81 setTimestamp(TmfTimestamp.ZERO);
82 }
83
84 int i = 0;
85 fColumnData = new TmfEventField[fDefinition.outputs.size()];
86 for (OutputColumn outputColumn : fDefinition.outputs) {
87 String value = fData.get(outputColumn.name);
88 if (outputColumn.name.equals(CustomTraceDefinition.TAG_TIMESTAMP) && date != null) {
89 SimpleDateFormat dateFormat = new SimpleDateFormat(fDefinition.timeStampOutputFormat);
90 fColumnData[i++] = new TmfEventField(outputColumn.name, dateFormat.format(date));
91 } else {
92 fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : "")); //$NON-NLS-1$
93 }
94 }
95 fData = null;
96 }
97
98 /* (non-Javadoc)
99 * @see java.lang.Object#hashCode()
100 */
101 @Override
102 public int hashCode() {
103 final int prime = 31;
104 int result = super.hashCode();
105 result = prime * result + ((fDefinition == null) ? 0 : fDefinition.hashCode());
106 return result;
107 }
108
109 /* (non-Javadoc)
110 * @see java.lang.Object#equals(java.lang.Object)
111 */
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 }
117 if (!super.equals(obj)) {
118 return false;
119 }
120 if (!(obj instanceof CustomEvent)) {
121 return false;
122 }
123 CustomEvent other = (CustomEvent) obj;
124 if (fDefinition == null) {
125 if (other.fDefinition != null) {
126 return false;
127 }
128 } else if (!fDefinition.equals(other.fDefinition)) {
129 return false;
130 }
131 return true;
132 }
133
134 }
This page took 0.034486 seconds and 5 git commands to generate.