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
.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
{
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 * Compare with another timestamp
262 * @param other the other timestamp
263 * @param withinPrecision indicates if precision is to be take into consideration
264 * @return -1: this timestamp is lower (i.e. anterior)
265 * 0: timestamps are equal (within precision if requested)
266 * 1: this timestamp is higher (i.e. posterior)
268 public int compareTo(final TmfTimestamp other
, boolean withinPrecision
) {
270 // If values have the same time scale, perform the comparison
271 if (fScale
== other
.fScale
) {
273 return compareWithinPrecision(this.fValue
, this.fPrecision
, other
.fValue
, other
.fPrecision
);
275 return compareNoPrecision(this.fValue
, other
.fValue
);
278 // If values have different time scales, adjust to the finest one and
279 // then compare. If the scaling difference is too large, revert to
280 // some heuristics. Hopefully, nobody will try to compare galactic and
281 // quantic clock events...
282 int scaleDiff
= Math
.abs(fScale
- other
.fScale
);
284 if (scaleDiff
< scalingFactors
.length
) {
285 factor
= scalingFactors
[scaleDiff
];
286 limit
= scalingLimits
[scaleDiff
];
289 limit
= 0; // !!! 0 can always be scaled!!!
292 if (fScale
< other
.fScale
) {
293 // this has finer scale, so other should be scaled
295 if (other
.fValue
> limit
|| other
.fValue
< -limit
296 || other
.fPrecision
> limit
297 || other
.fPrecision
< -limit
)
298 return other
.fValue
> 0 ?
-1 : +1; // other exceeds scaling limit
300 return compareWithinPrecision(this.fValue
, this.fPrecision
,
301 other
.fValue
* factor
, other
.fPrecision
* factor
);
302 else if (other
.fValue
> limit
|| other
.fValue
< -limit
)
303 return other
.fValue
> 0 ?
-1 : +1; // other exceeds scaling limit
305 return compareNoPrecision(this.fValue
, other
.fValue
* factor
);
307 // other has finer scale, so this should be scaled
309 if (this.fValue
> limit
|| this.fValue
< -limit
310 || this.fPrecision
> limit
|| this.fPrecision
< -limit
)
311 return this.fValue
> 0 ?
+1 : -1; // we exceed scaling limit
313 return compareWithinPrecision(this.fValue
* factor
,
314 this.fPrecision
* factor
, other
.fValue
,
316 else if (this.fValue
> limit
|| this.fValue
< -limit
)
317 return this.fValue
> 0 ?
+1 : -1; // we exceed scaling limit
319 return compareNoPrecision(this.fValue
* factor
, other
.fValue
);
323 private static int compareNoPrecision(long thisValue
, long otherValue
) {
324 return (thisValue
== otherValue
) ?
0 : (thisValue
< otherValue
) ?
-1 : 1;
327 private static int compareWithinPrecision(long thisValue
, long thisPrecision
, long otherValue
, long otherPrecision
) {
328 if ((thisValue
+ thisPrecision
) < (otherValue
- otherPrecision
))
330 if ((thisValue
- thisPrecision
) > (otherValue
+ otherPrecision
))
335 // ------------------------------------------------------------------------
337 // ------------------------------------------------------------------------
340 public int hashCode() {
342 result
= 37 * result
+ (int) (fValue ^
(fValue
>>> 32));
343 result
= 37 * result
+ fScale
;
344 result
= 37 * result
+ (int) (fPrecision ^
(fPrecision
>>> 32));
349 public boolean equals(Object other
) {
350 if (!(other
instanceof TmfTimestamp
))
352 TmfTimestamp o
= (TmfTimestamp
) other
;
353 return compareTo(o
, false) == 0;
357 @SuppressWarnings("nls")
358 public String
toString() {
359 return "[TmfTimestamp(" + fValue
+ "," + fScale
+ "," + fPrecision
+ ")]";
363 public TmfTimestamp
clone() {
364 TmfTimestamp clone
= null;
366 clone
= (TmfTimestamp
) super.clone();
367 clone
.fValue
= fValue
;
368 clone
.fScale
= fScale
;
369 clone
.fPrecision
= fPrecision
;
370 } catch (CloneNotSupportedException e
) {