1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.synchronization
;
15 import java
.io
.Serializable
;
16 import java
.math
.BigDecimal
;
17 import java
.math
.MathContext
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
23 * Class implementing a linear timestamp transform, with a slope and/or offset
25 * f(t) = alpha*t + beta
27 * @author Geneviève Bastien
30 public class TmfTimestampTransformLinear
implements ITmfTimestampTransform
, Serializable
{
33 * Generated serial UID
35 private static final long serialVersionUID
= -4756608071358979461L;
38 * Respectively the slope and offset and this linear equation.
40 * FIXME: Maybe doubles will be enough, for the whole synchronization
41 * package as well, I think BigDecimal is a remnant of past trials and
44 private final BigDecimal fAlpha
;
45 private final BigDecimal fBeta
;
47 private static final MathContext fMc
= MathContext
.DECIMAL128
;
52 public TmfTimestampTransformLinear() {
53 fAlpha
= BigDecimal
.ONE
;
54 fBeta
= BigDecimal
.ZERO
;
58 * Constructor with alpha and beta
61 * The slope of the linear transform
63 * The initial offset of the linear transform
65 public TmfTimestampTransformLinear(final double alpha
, final double beta
) {
66 fAlpha
= BigDecimal
.valueOf(alpha
);
67 fBeta
= BigDecimal
.valueOf(beta
);
71 * Constructor with alpha and beta in big decimal
74 * The slope of the linear transform
76 * The initial offset of the linear transform
78 public TmfTimestampTransformLinear(final BigDecimal fAlpha2
, final BigDecimal fBeta2
) {
79 if (fAlpha2
!= null) {
82 fAlpha
= BigDecimal
.ONE
;
87 fBeta
= BigDecimal
.ZERO
;
92 public ITmfTimestamp
transform(ITmfTimestamp timestamp
) {
93 BigDecimal newvalue
= BigDecimal
.valueOf(timestamp
.getValue()).multiply(fAlpha
, fMc
).add(fBeta
);
94 return new TmfTimestamp(timestamp
, newvalue
.longValue());
98 public long transform(long timestamp
) {
99 BigDecimal t
= BigDecimal
.valueOf(timestamp
).multiply(fAlpha
, fMc
).add(fBeta
);
100 return t
.longValue();
104 public ITmfTimestampTransform
composeWith(ITmfTimestampTransform composeWith
) {
105 if (composeWith
.equals(TmfTimestampTransform
.IDENTITY
)) {
106 /* If composing with identity, just return this */
108 } else if (composeWith
instanceof TmfTimestampTransformLinear
) {
109 /* If composeWith is a linear transform, add the two together */
110 TmfTimestampTransformLinear ttl
= (TmfTimestampTransformLinear
) composeWith
;
111 BigDecimal newAlpha
= fAlpha
.multiply(ttl
.fAlpha
, fMc
);
112 BigDecimal newBeta
= fAlpha
.multiply(ttl
.fBeta
, fMc
).add(fBeta
);
113 return new TmfTimestampTransformLinear(newAlpha
, newBeta
);
116 * We do not know what to do with this kind of transform, just
124 public boolean equals(Object other
) {
125 boolean result
= false;
126 if (other
instanceof TmfTimestampTransformLinear
) {
127 TmfTimestampTransformLinear that
= (TmfTimestampTransformLinear
) other
;
128 result
= ((that
.fAlpha
.equals(fAlpha
)) && (that
.fBeta
.equals(fBeta
)));
134 public int hashCode() {
135 final int prime
= 31;
137 result
= (prime
* result
) + (fBeta
.multiply(fAlpha
).intValue());
142 public String
toString() {
143 return "TmfTimestampLinear [ alpha = " + fAlpha
.toString() + //$NON-NLS-1$
144 ", beta = " + fBeta
.toString() + //$NON-NLS-1$