1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 É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
.tracecompass
.internal
.tmf
.core
.synchronization
;
15 import java
.math
.BigDecimal
;
16 import java
.math
.MathContext
;
18 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
19 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
25 * Class implementing a linear timestamp transform, with a slope and/or offset
27 * f(t) = alpha*t + beta
29 * @author Geneviève Bastien
31 public class TmfTimestampTransformLinear
implements ITmfTimestampTransformInvertible
{
34 * Generated serial UID
36 private static final long serialVersionUID
= -4756608071358979461L;
39 * Respectively the slope and offset and this linear equation.
41 private final BigDecimal fAlpha
;
42 private final BigDecimal fBeta
;
44 private static final MathContext fMc
= MathContext
.DECIMAL128
;
49 public TmfTimestampTransformLinear() {
50 fAlpha
= BigDecimal
.ONE
;
51 fBeta
= BigDecimal
.ZERO
;
55 * Constructor with alpha and beta
58 * The slope of the linear transform
60 * The initial offset of the linear transform
62 public TmfTimestampTransformLinear(final double alpha
, final double beta
) {
63 fAlpha
= BigDecimal
.valueOf(alpha
);
64 fBeta
= BigDecimal
.valueOf(beta
);
68 * Constructor with alpha and beta in big decimal
71 * The slope of the linear transform
73 * The initial offset of the linear transform
75 public TmfTimestampTransformLinear(final BigDecimal fAlpha2
, final BigDecimal fBeta2
) {
76 if (fAlpha2
!= null) {
79 fAlpha
= BigDecimal
.ONE
;
84 fBeta
= BigDecimal
.ZERO
;
89 public ITmfTimestamp
transform(ITmfTimestamp timestamp
) {
90 BigDecimal newvalue
= BigDecimal
.valueOf(timestamp
.getValue()).multiply(fAlpha
, fMc
).add(fBeta
);
91 return new TmfTimestamp(timestamp
, newvalue
.longValue());
95 public long transform(long timestamp
) {
96 BigDecimal t
= BigDecimal
.valueOf(timestamp
).multiply(fAlpha
, fMc
).add(fBeta
);
101 public ITmfTimestampTransform
composeWith(ITmfTimestampTransform composeWith
) {
102 if (composeWith
.equals(TmfTimestampTransform
.IDENTITY
)) {
103 /* If composing with identity, just return this */
105 } else if (composeWith
instanceof TmfTimestampTransformLinear
) {
106 /* If composeWith is a linear transform, add the two together */
107 TmfTimestampTransformLinear ttl
= (TmfTimestampTransformLinear
) composeWith
;
108 BigDecimal newAlpha
= fAlpha
.multiply(ttl
.fAlpha
, fMc
);
109 BigDecimal newBeta
= fAlpha
.multiply(ttl
.fBeta
, fMc
).add(fBeta
);
110 /* Don't use the factory to make sure any further composition will
111 * be performed on the same object type */
112 return new TmfTimestampTransformLinear(newAlpha
, newBeta
);
115 * We do not know what to do with this kind of transform, just
123 public boolean equals(Object other
) {
124 boolean result
= false;
125 if (other
instanceof TmfTimestampTransformLinear
) {
126 TmfTimestampTransformLinear that
= (TmfTimestampTransformLinear
) other
;
127 result
= ((that
.fAlpha
.equals(fAlpha
)) && (that
.fBeta
.equals(fBeta
)));
133 public int hashCode() {
134 final int prime
= 31;
136 result
= (prime
* result
) + (fBeta
.multiply(fAlpha
).intValue());
141 public String
toString() {
142 return "TmfTimestampLinear [ slope = " + fAlpha
.toString() + //$NON-NLS-1$
143 ", offset = " + fBeta
.toString() + //$NON-NLS-1$
148 public ITmfTimestampTransform
inverse() {
149 return TimestampTransformFactory
.createLinear(NonNullUtils
.checkNotNull(BigDecimal
.ONE
.divide(fAlpha
, fMc
)), NonNullUtils
.checkNotNull(BigDecimal
.valueOf(-1).multiply(fBeta
).divide(fAlpha
, fMc
)));
153 * @return the slope alpha
155 public BigDecimal
getAlpha() {
160 * @return the offset beta
162 public BigDecimal
getBeta() {