1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Thomas Gatterweh - Updated scaling / synchronization
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.event
;
17 * <b><u>TmfTimestamp</u></b>
19 * The fundamental time reference in the TMF.
21 * It provides a generic timestamp implementation in its most basic form:
23 * <li>timestamp = [value] * 10**[scale] +/- [precision]
27 * <li>[value] is an unstructured integer value
28 * <li>[scale] is the magnitude of the value wrt some application-specific
29 * base unit (e.g. the second)
30 * <li>[precision] indicates the error on the value (useful for comparing
31 * timestamps in different scales). Default: 0.
36 * To allow synchronization of timestamps from different reference clocks,
37 * there is a possibility to "adjust" the timestamp by changing its scale
38 * (traces of different time scale) and/or by adding an offset to its value
39 * (clock drift between traces).
41 * Notice that the adjusted timestamp value could be negative e.g. for events
42 * that occurred before t0 wrt the reference clock.
44 public class TmfTimestamp
implements Cloneable
, Comparable
<TmfTimestamp
> {
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 protected long fValue
; // The timestamp raw value
51 protected byte fScale
; // The time scale
52 protected long fPrecision
; // The value precision (tolerance)
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 // The beginning and end of time
59 public static final TmfTimestamp BigBang
= new TmfTimestamp(Long
.MIN_VALUE
, Byte
.MAX_VALUE
, 0);
60 public static final TmfTimestamp BigCrunch
= new TmfTimestamp(Long
.MAX_VALUE
, Byte
.MAX_VALUE
, 0);
61 public static final TmfTimestamp Zero
= new TmfTimestamp(0, (byte) 0, 0);
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
70 public TmfTimestamp() {
75 * Simple constructor with value only
77 public TmfTimestamp(long value
) {
78 this(value
, (byte) 0, 0);
82 * Simple constructor with value and scale
87 public TmfTimestamp(long value
, byte scale
) {
88 this(value
, scale
, 0);
92 * Constructor with value, scale and precision
98 public TmfTimestamp(long value
, byte scale
, long precision
) {
101 fPrecision
= Math
.abs(precision
);
109 public TmfTimestamp(TmfTimestamp other
) {
111 throw new IllegalArgumentException();
112 fValue
= other
.fValue
;
113 fScale
= other
.fScale
;
114 fPrecision
= other
.fPrecision
;
117 // ------------------------------------------------------------------------
119 // ------------------------------------------------------------------------
122 * @return the timestamp value
124 public long getValue() {
129 * @return the timestamp scale
131 public byte getScale() {
136 * @return the timestamp value precision
138 public long getPrecision() {
142 // ------------------------------------------------------------------------
144 // ------------------------------------------------------------------------
147 * Return a shifted and scaled timestamp.
149 * Limitation: The scaling is limited to MAX_SCALING orders of magnitude.
150 * The main reason is that the 64 bits value starts to lose any significance
151 * meaning beyond that scale difference and it's not even worth the trouble
152 * to switch to BigDecimal arithmetics.
154 * @param offset the shift value (in the same scale as newScale)
155 * @param newScale the new timestamp scale
156 * @return the synchronized timestamp in the new scale
157 * @throws ArithmeticException
159 public TmfTimestamp
synchronize(long offset
, byte newScale
) throws ArithmeticException
{
161 long newValue
= fValue
;
162 long newPrecision
= fPrecision
;
164 // Handle the easy case
165 if (fScale
== newScale
&& offset
== 0)
168 // Determine the scaling factor
169 if (fScale
!= newScale
) {
170 int scaleDiff
= Math
.abs(fScale
- newScale
);
171 // Let's try to be realistic...
172 if (scaleDiff
>= scalingFactors
.length
) {
173 throw new ArithmeticException("Scaling exception"); //$NON-NLS-1$
175 // Adjust the timestamp
176 long scalingFactor
= scalingFactors
[scaleDiff
];
177 if (newScale
< fScale
) {
178 newValue
*= scalingFactor
;
179 newPrecision
*= scalingFactor
;
181 newValue
/= scalingFactor
;
182 newPrecision
/= scalingFactor
;
187 newValue
= (newValue
< Long
.MIN_VALUE
- offset
) ? Long
.MIN_VALUE
: newValue
+ offset
;
189 newValue
= (newValue
> Long
.MAX_VALUE
- offset
) ? Long
.MAX_VALUE
: newValue
+ offset
;
192 return new TmfTimestamp(newValue
, newScale
, newPrecision
);
195 private static final long scalingFactors
[] = new long[] {
214 1000000000000000000L,
217 private static final long scalingLimits
[] = new long[] {
219 Long
.MAX_VALUE
/ 10L,
220 Long
.MAX_VALUE
/ 100L,
221 Long
.MAX_VALUE
/ 1000L,
222 Long
.MAX_VALUE
/ 10000L,
223 Long
.MAX_VALUE
/ 100000L,
224 Long
.MAX_VALUE
/ 1000000L,
225 Long
.MAX_VALUE
/ 10000000L,
226 Long
.MAX_VALUE
/ 100000000L,
227 Long
.MAX_VALUE
/ 1000000000L,
228 Long
.MAX_VALUE
/ 10000000000L,
229 Long
.MAX_VALUE
/ 100000000000L,
230 Long
.MAX_VALUE
/ 1000000000000L,
231 Long
.MAX_VALUE
/ 10000000000000L,
232 Long
.MAX_VALUE
/ 100000000000000L,
233 Long
.MAX_VALUE
/ 1000000000000000L,
234 Long
.MAX_VALUE
/ 10000000000000000L,
235 Long
.MAX_VALUE
/ 100000000000000000L,
236 Long
.MAX_VALUE
/ 1000000000000000000L,
239 public static long getScalingFactor(byte scale
)
241 return scalingFactors
[scale
];
245 * Compute the adjustment, in the reference scale, needed to synchronize
246 * this timestamp with a reference timestamp.
248 * @param reference the reference timestamp to synchronize with
249 * @param scale the scale of the adjustment
250 * @return the adjustment term in the reference time scale
251 * @throws ArithmeticException
253 public long getAdjustment(TmfTimestamp reference
, byte scale
) throws ArithmeticException
{
254 TmfTimestamp ts1
= synchronize(0, scale
);
255 TmfTimestamp ts2
= reference
.synchronize(0, scale
);
256 return ts2
.fValue
- ts1
.fValue
;
260 * Compute the delta between two timestamps (adjusted to scale of current timestamp).
262 * @param reference the reference timestamp to synchronize with
263 * @return the delta timestamp
264 * @throws ArithmeticException
266 public TmfTimestamp
getDelta(TmfTimestamp other
) throws ArithmeticException
{
267 TmfTimestamp newSecond
= other
;
268 if ((fScale
!= other
.fScale
) || (fPrecision
!= other
.fPrecision
)) {
269 newSecond
= other
.synchronize(0, fScale
);
271 return new TmfTimestamp(fValue
- newSecond
.fValue
,
273 newSecond
.fPrecision
> fPrecision ? newSecond
.fPrecision
: fPrecision
);
277 * Compare with another timestamp
279 * @param other the other timestamp
280 * @param withinPrecision indicates if precision is to be take into consideration
281 * @return -1: this timestamp is lower (i.e. anterior)
282 * 0: timestamps are equal (within precision if requested)
283 * 1: this timestamp is higher (i.e. posterior)
285 public int compareTo(final TmfTimestamp other
, boolean withinPrecision
) {
287 // If values have the same time scale, perform the comparison
288 if (fScale
== other
.fScale
) {
290 return compareWithinPrecision(this.fValue
, this.fPrecision
, other
.fValue
, other
.fPrecision
);
292 return compareNoPrecision(this.fValue
, other
.fValue
);
295 // If values have different time scales, adjust to the finest one and
296 // then compare. If the scaling difference is too large, revert to
297 // some heuristics. Hopefully, nobody will try to compare galactic and
298 // quantic clock events...
299 int scaleDiff
= Math
.abs(fScale
- other
.fScale
);
301 if (scaleDiff
< scalingFactors
.length
) {
302 factor
= scalingFactors
[scaleDiff
];
303 limit
= scalingLimits
[scaleDiff
];
306 limit
= 0; // !!! 0 can always be scaled!!!
309 if (fScale
< other
.fScale
) {
310 // this has finer scale, so other should be scaled
312 if (other
.fValue
> limit
|| other
.fValue
< -limit
313 || other
.fPrecision
> limit
314 || other
.fPrecision
< -limit
)
315 return other
.fValue
> 0 ?
-1 : +1; // other exceeds scaling limit
317 return compareWithinPrecision(this.fValue
, this.fPrecision
,
318 other
.fValue
* factor
, other
.fPrecision
* factor
);
319 else if (other
.fValue
> limit
|| other
.fValue
< -limit
)
320 return other
.fValue
> 0 ?
-1 : +1; // other exceeds scaling limit
322 return compareNoPrecision(this.fValue
, other
.fValue
* factor
);
324 // other has finer scale, so this should be scaled
326 if (this.fValue
> limit
|| this.fValue
< -limit
327 || this.fPrecision
> limit
|| this.fPrecision
< -limit
)
328 return this.fValue
> 0 ?
+1 : -1; // we exceed scaling limit
330 return compareWithinPrecision(this.fValue
* factor
,
331 this.fPrecision
* factor
, other
.fValue
,
333 else if (this.fValue
> limit
|| this.fValue
< -limit
)
334 return this.fValue
> 0 ?
+1 : -1; // we exceed scaling limit
336 return compareNoPrecision(this.fValue
* factor
, other
.fValue
);
340 private static int compareNoPrecision(long thisValue
, long otherValue
) {
341 return (thisValue
== otherValue
) ?
0 : (thisValue
< otherValue
) ?
-1 : 1;
344 private static int compareWithinPrecision(long thisValue
, long thisPrecision
, long otherValue
, long otherPrecision
) {
345 if ((thisValue
+ thisPrecision
) < (otherValue
- otherPrecision
))
347 if ((thisValue
- thisPrecision
) > (otherValue
+ otherPrecision
))
352 // ------------------------------------------------------------------------
354 // ------------------------------------------------------------------------
357 public int hashCode() {
359 result
= 37 * result
+ (int) (fValue ^
(fValue
>>> 32));
360 result
= 37 * result
+ fScale
;
361 result
= 37 * result
+ (int) (fPrecision ^
(fPrecision
>>> 32));
366 public boolean equals(Object other
) {
367 if (!(other
instanceof TmfTimestamp
))
369 TmfTimestamp o
= (TmfTimestamp
) other
;
370 return compareTo(o
, false) == 0;
374 @SuppressWarnings("nls")
375 public String
toString() {
376 return "[TmfTimestamp(" + fValue
+ "," + fScale
+ "," + fPrecision
+ ")]";
380 public TmfTimestamp
clone() {
381 TmfTimestamp clone
= null;
383 clone
= (TmfTimestamp
) super.clone();
384 clone
.fValue
= fValue
;
385 clone
.fScale
= fScale
;
386 clone
.fPrecision
= fPrecision
;
387 } catch (CloneNotSupportedException e
) {
393 public int compareTo(TmfTimestamp o
) {
394 return compareTo(o
, false);