1 /*******************************************************************************
2 * Copyright (c) 2009 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.event
;
16 * <b><u>TmfTimestamp</u></b>
18 * The fundamental time reference in the TMF.
20 * It provides a generic timestamp implementation in its most basic form:
22 * <li>an unstructured integer value
23 * <li>a time scale corresponding to the magnitude of the value wrt some
24 * application-specific base unit (e.g. the second)
25 * <li>a precision to indicate the error on the value (useful for comparing
26 * timestamps in different scales). Default: 0.
28 * To allow synchronization of timestamps from different reference clocks, there
29 * is a possibility to "adjust" the timestamp both by changing its scale (traces
30 * of different scale) and by adding an offset to its value (clock drift between
33 * Note that the adjusted timestamp value could be negative e.g. for events that
34 * occurred before t0 of the reference clock.
37 public class TmfTimestamp
{
39 // ========================================================================
41 // ========================================================================
43 protected final long fValue
; // The timestamp value
44 protected final byte fScale
; // The time scale
45 protected final long fPrecision
; // The value precision (tolerance)
47 // ========================================================================
49 // ========================================================================
51 // The beginning and end of time
52 public static final TmfTimestamp BigBang
= new TmfTimestamp(Long
.MIN_VALUE
, Byte
.MAX_VALUE
, 0);
53 public static final TmfTimestamp BigCrunch
= new TmfTimestamp(Long
.MAX_VALUE
, Byte
.MAX_VALUE
, 0);
55 // ========================================================================
57 // ========================================================================
60 * Default constructor.
62 public TmfTimestamp() {
69 public TmfTimestamp(long value
) {
70 this(value
, (byte) 0, 0);
74 * Simple constructor with default error value
79 public TmfTimestamp(long value
, byte scale
) {
80 this(value
, scale
, 0);
84 * Constructor with measurement error.
90 public TmfTimestamp(long value
, byte scale
, long precision
) {
93 fPrecision
= Math
.abs(precision
);
101 public TmfTimestamp(TmfTimestamp other
) {
102 this(other
.fValue
, other
.fScale
, other
.fPrecision
);
105 // ========================================================================
107 // ========================================================================
110 * @return The timestamp value
112 public long getValue() {
117 * @return The timestamp scale
119 public byte getScale() {
124 * @return The timestamp value precision
126 public long getPrecision() {
130 // ========================================================================
132 // ========================================================================
135 * Return a shifted and scaled timestamp.
137 * Limitation: The scaling is limited to MAX_SCALING orders of magnitude.
138 * The main reason is that the 64 bits value starts to lose any significance
139 * meaning beyond that scale difference and it's not even worth the trouble
140 * to switch to BigDecimal arithmetics.
143 * - the shift value (in the same scale as newScale)
146 * @return The synchronized timestamp
150 * A java <code>long</code> has a maximum of 19 significant digits.
151 * (-9,223,372,036,854,775,808 .. +9,223,372,036,854,775,807)
153 * It is therefore useless to try to synchronize 2 timestamps whose
154 * difference in scale exceeds that value.
156 private static int MAX_SCALING
= 19;
158 public TmfTimestamp
synchronize(long offset
, byte newScale
) throws ArithmeticException
{
159 long newValue
= fValue
;
160 long newPrecision
= fPrecision
;
162 // Determine the scaling factor
163 if (fScale
!= newScale
) {
164 int scaleDiff
= Math
.abs(fScale
- newScale
);
165 // Let's try to be realistic...
166 if (scaleDiff
> MAX_SCALING
) {
167 throw new ArithmeticException("Scaling exception");
170 long scalingFactor
= 1;
171 for (int i
= 0; i
< scaleDiff
; i
++) {
174 if (newScale
< fScale
) {
175 newValue
*= scalingFactor
;
176 newPrecision
*= scalingFactor
;
178 newValue
/= scalingFactor
;
179 newPrecision
/= scalingFactor
;
183 return new TmfTimestamp(newValue
+ offset
, newScale
, newPrecision
);
187 * Compute the adjustment, in the reference scale, needed to synchronize
188 * this timestamp with a reference timestamp.
191 * - the reference timestamp to synchronize with
192 * @return The adjustment term in the reference time scale
193 * @throws TmfNumericalException
195 public long getAdjustment(TmfTimestamp reference
) throws ArithmeticException
{
196 TmfTimestamp ts
= synchronize(0, reference
.fScale
);
197 return reference
.fValue
- ts
.fValue
;
201 * Compare with another timestamp
204 * - the other timestamp
205 * @param withinPrecision
206 * - indicates if precision is to be take into consideration
207 * @return <li>-1: this timestamp is lower <li>0: timestamps are equal
208 * (within precision if requested) <li>1: this timestamp is higher
209 * @throws TmfNumericalException
211 public int compareTo(final TmfTimestamp other
, boolean withinPrecision
) {
213 // If values have the same time scale, perform the comparison
214 if (fScale
== other
.fScale
) {
215 if (withinPrecision
) {
216 if ((fValue
+ fPrecision
) < (other
.fValue
- other
.fPrecision
))
218 if ((fValue
- fPrecision
) > (other
.fValue
+ other
.fPrecision
))
222 return (fValue
== other
.fValue
) ?
0 : (fValue
< other
.fValue
) ?
-1
226 // If values have different time scales, adjust to the finest one and
227 // then compare. If the scaling difference is too large, revert to
228 // some heuristics. Hopefully, nobody will try to compare galactic and
229 // quantic clock events...
230 byte newScale
= (fScale
< other
.fScale
) ? fScale
: other
.fScale
;
232 TmfTimestamp ts1
= this.synchronize(0, newScale
);
233 TmfTimestamp ts2
= other
.synchronize(0, newScale
);
234 return ts1
.compareTo(ts2
, withinPrecision
);
235 } catch (ArithmeticException e
) {
236 if ((fValue
== 0) || (other
.fValue
== 0)) {
237 return (fValue
== other
.fValue
) ?
0
238 : (fValue
< other
.fValue
) ?
-1 : 1;
240 if ((fValue
> 0) && (other
.fValue
> 0)) {
241 return (fScale
< other
.fScale
) ?
-1 : 1;
243 if ((fValue
< 0) && (other
.fValue
< 0)) {
244 return (fScale
> other
.fScale
) ?
-1 : 1;
246 return (fValue
< 0) ?
-1 : 1;
253 * @see java.lang.Object#equals(java.lang.Object)
256 public boolean equals(Object other
) {
257 if (other
instanceof TmfTimestamp
)
258 return compareTo((TmfTimestamp
) other
, false) == 0;
259 return super.equals(other
);
263 * @see java.lang.Object#toString()
266 public String
toString() {
267 return "[TmfTimestamp:" + fValue
+ "," + fScale
+ "," + fPrecision
+ "]";
This page took 0.037322 seconds and 6 git commands to generate.