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