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