tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / timestamp / TmfTimestamp.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 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 * Francois Chouinard - Initial API and implementation
11 * Thomas Gatterweh - Updated scaling / synchronization
12 * Francois Chouinard - Refactoring to align with TMF Event Model 1.0
13 * Francois Chouinard - Implement augmented interface
14 *******************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.core.timestamp;
17
18 /**
19 * A generic timestamp implementation. The timestamp is represented by the
20 * tuple { value, scale, precision }. By default, timestamps are scaled in
21 * seconds.
22 *
23 * @author Francois Chouinard
24 * @version 1.1
25 * @since 2.0
26 */
27 public class TmfTimestamp implements ITmfTimestamp {
28
29 // ------------------------------------------------------------------------
30 // Constants
31 // ------------------------------------------------------------------------
32
33 /**
34 * The beginning of time
35 */
36 public static final ITmfTimestamp BIG_BANG =
37 new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE, 0);
38
39 /**
40 * The end of time
41 */
42 public static final ITmfTimestamp BIG_CRUNCH =
43 new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE, 0);
44
45 /**
46 * A more practical definition of "beginning of time"
47 */
48 public static final ITmfTimestamp PROJECT_IS_FUNDED = BIG_BANG;
49
50 /**
51 * A more practical definition of "end of time"
52 */
53 public static final ITmfTimestamp PROJECT_IS_CANNED = BIG_CRUNCH;
54
55 /**
56 * Zero
57 */
58 public static final ITmfTimestamp ZERO =
59 new TmfTimestamp(0, 0, 0);
60
61 // ------------------------------------------------------------------------
62 // Attributes
63 // ------------------------------------------------------------------------
64
65 /**
66 * The timestamp raw value (mantissa)
67 */
68 private final long fValue;
69
70 /**
71 * The timestamp scale (magnitude)
72 */
73 private final int fScale;
74
75 /**
76 * The value precision (tolerance)
77 */
78 private final int fPrecision;
79
80 // ------------------------------------------------------------------------
81 // Constructors
82 // ------------------------------------------------------------------------
83
84 /**
85 * Default constructor
86 */
87 public TmfTimestamp() {
88 this(0, ITmfTimestamp.SECOND_SCALE, 0);
89 }
90
91 /**
92 * Simple constructor (scale = precision = 0)
93 *
94 * @param value the timestamp value
95 */
96 public TmfTimestamp(final long value) {
97 this(value, ITmfTimestamp.SECOND_SCALE, 0);
98 }
99
100 /**
101 * Simple constructor (precision = 0)
102 *
103 * @param value the timestamp value
104 * @param scale the timestamp scale
105 */
106 public TmfTimestamp(final long value, final int scale) {
107 this(value, scale, 0);
108 }
109
110 /**
111 * Full constructor
112 *
113 * @param value the timestamp value
114 * @param scale the timestamp scale
115 * @param precision the timestamp precision
116 */
117 public TmfTimestamp(final long value, final int scale, final int precision) {
118 fValue = value;
119 fScale = scale;
120 fPrecision = Math.abs(precision);
121 }
122
123 /**
124 * Copy constructor
125 *
126 * @param timestamp the timestamp to copy
127 */
128 public TmfTimestamp(final ITmfTimestamp timestamp) {
129 if (timestamp == null) {
130 throw new IllegalArgumentException();
131 }
132 fValue = timestamp.getValue();
133 fScale = timestamp.getScale();
134 fPrecision = timestamp.getPrecision();
135 }
136
137 // ------------------------------------------------------------------------
138 // ITmfTimestamp
139 // ------------------------------------------------------------------------
140
141 @Override
142 public long getValue() {
143 return fValue;
144 }
145
146 @Override
147 public int getScale() {
148 return fScale;
149 }
150
151 @Override
152 public int getPrecision() {
153 return fPrecision;
154 }
155
156 private static final long scalingFactors[] = new long[] {
157 1L,
158 10L,
159 100L,
160 1000L,
161 10000L,
162 100000L,
163 1000000L,
164 10000000L,
165 100000000L,
166 1000000000L,
167 10000000000L,
168 100000000000L,
169 1000000000000L,
170 10000000000000L,
171 100000000000000L,
172 1000000000000000L,
173 10000000000000000L,
174 100000000000000000L,
175 1000000000000000000L,
176 };
177
178 @Override
179 public ITmfTimestamp normalize(final long offset, final int scale) {
180
181 long value = fValue;
182 int precision = fPrecision;
183
184 // Handle the trivial case
185 if (fScale == scale && offset == 0) {
186 return this;
187 }
188
189 // In case of big bang and big crunch just return this (no need to normalize)
190 if (this.equals(BIG_BANG) || this.equals(BIG_CRUNCH)) {
191 return this;
192 }
193
194 // First, scale the timestamp
195 if (fScale != scale) {
196 final int scaleDiff = Math.abs(fScale - scale);
197 if (scaleDiff >= scalingFactors.length) {
198 throw new ArithmeticException("Scaling exception"); //$NON-NLS-1$
199 }
200
201 final long scalingFactor = scalingFactors[scaleDiff];
202 if (scale < fScale) {
203 value *= scalingFactor;
204 precision *= scalingFactor;
205 } else {
206 value /= scalingFactor;
207 precision /= scalingFactor;
208 }
209 }
210
211 // Then, apply the offset
212 if (offset < 0) {
213 value = (value < Long.MIN_VALUE - offset) ? Long.MIN_VALUE : value + offset;
214 } else {
215 value = (value > Long.MAX_VALUE - offset) ? Long.MAX_VALUE : value + offset;
216 }
217
218 return new TmfTimestamp(value, scale, precision);
219 }
220
221 @Override
222 public int compareTo(final ITmfTimestamp ts, final boolean withinPrecision) {
223
224 // Check the corner cases (we can't use equals() because it uses compareTo()...)
225 if (ts == null) {
226 return 1;
227 }
228 if (this == ts || (fValue == ts.getValue() && fScale == ts.getScale())) {
229 return 0;
230 }
231 if ((fValue == BIG_BANG.getValue() && fScale == BIG_BANG.getScale()) || (ts.getValue() == BIG_CRUNCH.getValue() && ts.getScale() == BIG_CRUNCH.getScale())) {
232 return -1;
233 }
234 if ((fValue == BIG_CRUNCH.getValue() && fScale == BIG_CRUNCH.getScale()) || (ts.getValue() == BIG_BANG.getValue() && ts.getScale() == BIG_BANG.getScale())) {
235 return 1;
236 }
237
238 try {
239 final ITmfTimestamp nts = ts.normalize(0, fScale);
240 final long delta = fValue - nts.getValue();
241 if ((delta == 0) || (withinPrecision && (Math.abs(delta) <= (fPrecision + nts.getPrecision())))) {
242 return 0;
243 }
244 return (delta > 0) ? 1 : -1;
245 }
246 catch (final ArithmeticException e) {
247 // Scaling error. We can figure it out nonetheless.
248
249 // First, look at the sign of the mantissa
250 final long value = ts.getValue();
251 if (fValue == 0 && value == 0) {
252 return 0;
253 }
254 if (fValue < 0 && value >= 0) {
255 return -1;
256 }
257 if (fValue >= 0 && value < 0) {
258 return 1;
259 }
260
261 // Otherwise, just compare the scales
262 final int scale = ts.getScale();
263 return (fScale > scale) ? (fValue >= 0) ? 1 : -1 : (fValue >= 0) ? -1 : 1;
264 }
265 }
266
267 @Override
268 public ITmfTimestamp getDelta(final ITmfTimestamp ts) {
269 final ITmfTimestamp nts = ts.normalize(0, fScale);
270 final long value = fValue - nts.getValue();
271 return new TmfTimestampDelta(value, fScale, fPrecision + nts.getPrecision());
272 }
273
274 @Override
275 public boolean intersects(TmfTimeRange range) {
276 if (this.compareTo(range.getStartTime()) >= 0 &&
277 this.compareTo(range.getEndTime()) <= 0) {
278 return true;
279 }
280 return false;
281 }
282
283 // ------------------------------------------------------------------------
284 // Comparable
285 // ------------------------------------------------------------------------
286
287 @Override
288 public int compareTo(final ITmfTimestamp ts) {
289 return compareTo(ts, false);
290 }
291
292 // ------------------------------------------------------------------------
293 // Object
294 // ------------------------------------------------------------------------
295
296 @Override
297 public int hashCode() {
298 final int prime = 31;
299 int result = 1;
300 result = prime * result + (int) (fValue ^ (fValue >>> 32));
301 result = prime * result + fScale;
302 result = prime * result + fPrecision;
303 return result;
304 }
305
306 @Override
307 public boolean equals(final Object other) {
308 if (this == other) {
309 return true;
310 }
311 if (other == null) {
312 return false;
313 }
314 if (!(other instanceof TmfTimestamp)) {
315 return false;
316 }
317 final TmfTimestamp ts = (TmfTimestamp) other;
318 return compareTo(ts, false) == 0;
319 }
320
321 @Override
322 public String toString() {
323 return toString(TmfTimestampFormat.getDefaulTimeFormat());
324 }
325
326 /**
327 * @since 2.0
328 */
329 @Override
330 public String toString(final TmfTimestampFormat format) {
331 try {
332 ITmfTimestamp ts = normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
333 return format.format(ts.getValue());
334 }
335 catch (ArithmeticException e) {
336 return format.format(0);
337 }
338 }
339
340 }
This page took 0.038917 seconds and 5 git commands to generate.