Commit | Line | Data |
---|---|---|
b0f9e44d MK |
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 | ||
ce2388e0 FC |
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 | ||
b0f9e44d | 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
ce2388e0 FC |
19 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
20 | ||
b1baa808 MK |
21 | /** |
22 | */ | |
929acb4f | 23 | public class CtfTmfTimestamp extends TmfTimestamp { |
ce2388e0 | 24 | |
b1baa808 MK |
25 | /** |
26 | */ | |
b0f9e44d | 27 | public enum TimestampType { |
9ac2eb62 MK |
28 | /** |
29 | * yyyy/mm/dd hh:mm:ss.nnnnnnnnnn | |
30 | */ | |
31 | FULL_DATE, | |
32 | /** | |
33 | * hh:mm:ss.nnnnnnnnnn | |
34 | */ | |
35 | DAY, | |
36 | /** | |
37 | * nnnnnnnnnnnnnnnnnnnnn ns | |
38 | */ | |
39 | NANOS, | |
40 | /** | |
41 | * ssssssssss.nnnnnnnnnn s | |
42 | */ | |
43 | SECONDS | |
b0f9e44d MK |
44 | } |
45 | ||
46 | private TimestampType type; | |
ce2388e0 | 47 | |
b1baa808 MK |
48 | /** |
49 | * Constructor for CtfTmfTimestamp. | |
50 | * @param timestamp long | |
51 | */ | |
b0f9e44d | 52 | public CtfTmfTimestamp(long timestamp) { |
b9e37ffd | 53 | setValue(timestamp, -9, 0); |
b0f9e44d | 54 | type = TimestampType.DAY; |
ce2388e0 FC |
55 | } |
56 | ||
b1baa808 MK |
57 | /** |
58 | * Method setType. | |
59 | * @param value TimestampType | |
60 | */ | |
b0f9e44d MK |
61 | public void setType(TimestampType value) { |
62 | type = value; | |
63 | } | |
64 | ||
b1baa808 MK |
65 | /** |
66 | * Method getType. | |
67 | * @return TimestampType | |
68 | */ | |
b0f9e44d MK |
69 | public TimestampType getType() { |
70 | return type; | |
ce2388e0 FC |
71 | } |
72 | ||
b0f9e44d MK |
73 | /* |
74 | * (non-Javadoc) | |
75 | * | |
76 | * @see | |
77 | * org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse | |
78 | * .linuxtools.tmf.core.event.ITmfTimestamp) | |
ce2388e0 | 79 | */ |
b1baa808 MK |
80 | /** |
81 | * Method getDelta. | |
82 | * @param ts ITmfTimestamp | |
83 | * @return ITmfTimestamp | |
84 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp#getDelta(ITmfTimestamp) | |
85 | */ | |
ce2388e0 | 86 | @Override |
b0f9e44d MK |
87 | public ITmfTimestamp getDelta(ITmfTimestamp ts) { |
88 | TmfTimestamp parent = (TmfTimestamp) super.getDelta(ts); | |
89 | long value = parent.getValue(); | |
90 | long exp = parent.getScale(); | |
91 | long diff = exp + 9; | |
92 | for (int i = 0; i < diff; i++) { | |
93 | value *= 10; | |
ce2388e0 | 94 | } |
b0f9e44d MK |
95 | CtfTmfTimestamp retVal = new CtfTmfTimestamp(value); |
96 | if (value > 100000000) { | |
97 | retVal.type = TimestampType.SECONDS; | |
98 | } else { | |
99 | retVal.type = TimestampType.NANOS; | |
ce2388e0 | 100 | } |
b0f9e44d | 101 | return retVal; |
ce2388e0 FC |
102 | } |
103 | ||
104 | /* | |
105 | * (non-Javadoc) | |
106 | * | |
107 | * @see java.lang.Object#toString() | |
108 | */ | |
109 | @Override | |
110 | public String toString() { | |
b0f9e44d MK |
111 | switch (type) { |
112 | case DAY: { | |
113 | return dateToString(); | |
114 | } | |
115 | case FULL_DATE: { | |
116 | return toFullDateString(); | |
117 | } | |
118 | case NANOS: { | |
119 | return nanoToString(); | |
120 | } | |
121 | case SECONDS:{ | |
122 | return secondsToString(); | |
123 | } | |
124 | } | |
125 | return super.toString(); | |
126 | } | |
127 | ||
b1baa808 MK |
128 | /** |
129 | * Method secondsToString. | |
130 | * @return String | |
131 | */ | |
b0f9e44d MK |
132 | private String secondsToString() { |
133 | double timestamp = getValue(); | |
134 | timestamp /= 1000000000; | |
135 | StringBuilder retVal = new StringBuilder(); | |
136 | retVal.append(timestamp); | |
137 | retVal.append(" s"); //$NON-NLS-1$ | |
138 | return retVal.toString(); | |
139 | } | |
140 | ||
b1baa808 MK |
141 | /** |
142 | * Method nanoToString. | |
143 | * @return String | |
144 | */ | |
b0f9e44d MK |
145 | private String nanoToString() { |
146 | final long timestamp = getValue(); | |
147 | StringBuilder retVal = new StringBuilder(); | |
148 | retVal.append(timestamp); | |
149 | retVal.append(" ns"); //$NON-NLS-1$ | |
150 | return retVal.toString(); | |
151 | } | |
152 | ||
b1baa808 MK |
153 | /** |
154 | * Method dateToString. | |
155 | * @return String | |
156 | */ | |
b0f9e44d | 157 | private String dateToString() { |
b9e37ffd | 158 | final long timestamp = getValue(); |
ce2388e0 FC |
159 | final Date d = new Date(timestamp / 1000000); |
160 | final DateFormat df = new SimpleDateFormat("HH:mm:ss."); //$NON-NLS-1$ | |
161 | final long nanos = (timestamp % 1000000000); | |
162 | StringBuilder output = new StringBuilder(); | |
163 | output.append(df.format(d)); | |
164 | output.append(String.format("%09d", nanos)); //$NON-NLS-1$ | |
165 | return output.toString(); | |
166 | } | |
167 | ||
b1baa808 MK |
168 | /** |
169 | * Method toFullDateString. | |
170 | * @return String | |
171 | */ | |
b0f9e44d | 172 | private String toFullDateString() { |
b9e37ffd | 173 | final long timestamp = getValue(); |
ce2388e0 FC |
174 | final Date d = new Date(timestamp / 1000000); |
175 | final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$ | |
176 | final long nanos = (timestamp % 1000000000); | |
177 | StringBuilder output = new StringBuilder(); | |
178 | output.append(df.format(d)); | |
179 | output.append(String.format("%09d", nanos)); //$NON-NLS-1$ | |
180 | return output.toString(); | |
181 | } | |
182 | ||
b1baa808 MK |
183 | /* (non-Javadoc) |
184 | * @see java.lang.Object#hashCode() | |
185 | */ | |
186 | @Override | |
187 | public int hashCode() { | |
188 | final int prime = 31; | |
81c8e6f7 MK |
189 | int result = super.hashCode() * prime; |
190 | result += ((type == null) ? 0 : type.toString().hashCode()); | |
b1baa808 MK |
191 | return result; |
192 | } | |
193 | ||
194 | /* (non-Javadoc) | |
195 | * @see java.lang.Object#equals(java.lang.Object) | |
196 | */ | |
197 | @Override | |
198 | public boolean equals(Object obj) { | |
199 | if (this == obj) { | |
200 | return true; | |
201 | } | |
202 | if (!super.equals(obj)) { | |
203 | return false; | |
204 | } | |
205 | if (!(obj instanceof CtfTmfTimestamp)) { | |
206 | return false; | |
207 | } | |
208 | CtfTmfTimestamp other = (CtfTmfTimestamp) obj; | |
209 | if (type != other.type) { | |
210 | return false; | |
211 | } | |
212 | return true; | |
213 | } | |
214 | ||
ce2388e0 | 215 | } |