ctf: Fix lost events in a more elegant way
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTimestamp.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17
18 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
19 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
20
21 /**
22 * The CTF adapter for the TMF timestamp
23 *
24 * @version 1.0
25 * @author Matthew khouzam
26 */
27 public class CtfTmfTimestamp extends TmfTimestamp {
28
29 /**
30 */
31 public enum TimestampType {
32 /**
33 * yyyy/mm/dd hh:mm:ss.nnnnnnnnnn
34 */
35 FULL_DATE,
36 /**
37 * hh:mm:ss.nnnnnnnnnn
38 */
39 DAY,
40 /**
41 * nnnnnnnnnnnnnnnnnnnnn ns
42 */
43 NANOS,
44 /**
45 * ssssssssss.nnnnnnnnnn s
46 */
47 SECONDS
48 }
49
50 private TimestampType type;
51
52 /**
53 * Constructor for CtfTmfTimestamp.
54 * @param timestamp long
55 */
56 public CtfTmfTimestamp(long timestamp) {
57 setValue(timestamp, -9, 0);
58 type = TimestampType.DAY;
59 }
60
61 /**
62 * Method setType.
63 * @param value TimestampType
64 */
65 public void setType(TimestampType value) {
66 type = value;
67 }
68
69 /**
70 * Method getType.
71 * @return TimestampType
72 */
73 public TimestampType getType() {
74 return type;
75 }
76
77 /*
78 * (non-Javadoc)
79 *
80 * @see
81 * org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse
82 * .linuxtools.tmf.core.event.ITmfTimestamp)
83 */
84 /**
85 * Method getDelta.
86 * @param ts ITmfTimestamp
87 * @return ITmfTimestamp
88 * @see org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp#getDelta(ITmfTimestamp)
89 */
90 @Override
91 public ITmfTimestamp getDelta(ITmfTimestamp ts) {
92 TmfTimestamp parent = (TmfTimestamp) super.getDelta(ts);
93 long value = parent.getValue();
94 long exp = parent.getScale();
95 long diff = exp + 9;
96 for (int i = 0; i < diff; i++) {
97 value *= 10;
98 }
99 CtfTmfTimestamp retVal = new CtfTmfTimestamp(value);
100 if (value > 100000000) {
101 retVal.type = TimestampType.SECONDS;
102 } else {
103 retVal.type = TimestampType.NANOS;
104 }
105 return retVal;
106 }
107
108 /*
109 * (non-Javadoc)
110 *
111 * @see java.lang.Object#toString()
112 */
113 @Override
114 public String toString() {
115 switch (type) {
116 case DAY: {
117 return dateToString();
118 }
119 case FULL_DATE: {
120 return toFullDateString();
121 }
122 case NANOS: {
123 return nanoToString();
124 }
125 case SECONDS:{
126 return secondsToString();
127 }
128 default:
129 break;
130 }
131 return super.toString();
132 }
133
134 /**
135 * Method secondsToString.
136 * @return String
137 */
138 private String secondsToString() {
139 double timestamp = getValue();
140 timestamp /= 1000000000;
141 StringBuilder retVal = new StringBuilder();
142 retVal.append(timestamp);
143 retVal.append(" s"); //$NON-NLS-1$
144 return retVal.toString();
145 }
146
147 /**
148 * Method nanoToString.
149 * @return String
150 */
151 private String nanoToString() {
152 final long timestamp = getValue();
153 StringBuilder retVal = new StringBuilder();
154 retVal.append(timestamp);
155 retVal.append(" ns"); //$NON-NLS-1$
156 return retVal.toString();
157 }
158
159 /**
160 * Method dateToString.
161 * @return String
162 */
163 private String dateToString() {
164 final long timestamp = getValue();
165 final Date d = new Date(timestamp / 1000000);
166 final DateFormat df = new SimpleDateFormat("HH:mm:ss."); //$NON-NLS-1$
167 final long nanos = (timestamp % 1000000000);
168 StringBuilder output = new StringBuilder();
169 output.append(df.format(d));
170 output.append(String.format("%09d", nanos)); //$NON-NLS-1$
171 return output.toString();
172 }
173
174 /**
175 * Method toFullDateString.
176 * @return String
177 */
178 private String toFullDateString() {
179 final long timestamp = getValue();
180 final Date d = new Date(timestamp / 1000000);
181 final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
182 final long nanos = (timestamp % 1000000000);
183 StringBuilder output = new StringBuilder();
184 output.append(df.format(d));
185 output.append(String.format("%09d", nanos)); //$NON-NLS-1$
186 return output.toString();
187 }
188
189 /* (non-Javadoc)
190 * @see java.lang.Object#hashCode()
191 */
192 @Override
193 public int hashCode() {
194 final int prime = 31;
195 int result = super.hashCode() * prime;
196 result += ((type == null) ? 0 : type.toString().hashCode());
197 return result;
198 }
199
200 /* (non-Javadoc)
201 * @see java.lang.Object#equals(java.lang.Object)
202 */
203 @Override
204 public boolean equals(Object obj) {
205 if (this == obj) {
206 return true;
207 }
208 if (!super.equals(obj)) {
209 return false;
210 }
211 if (!(obj instanceof CtfTmfTimestamp)) {
212 return false;
213 }
214 CtfTmfTimestamp other = (CtfTmfTimestamp) obj;
215 if (type != other.type) {
216 return false;
217 }
218 return true;
219 }
220
221 }
This page took 0.035094 seconds and 5 git commands to generate.