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
.tracecompass
.tmf
.core
.synchronization
;
15 import java
.math
.BigDecimal
;
16 import java
.math
.MathContext
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
19 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
22 * Class implementing a linear timestamp transform, with a slope and/or offset
24 * f(t) = alpha*t + beta
26 * @author Geneviève Bastien
28 * @deprecated This class has been moved to internal. Use one of
29 * {@link TimestampTransformFactory} methods to create the timestamp
30 * transform. It will return an optimized transform for the
31 * parameters given. To have a linear transform, use methods
32 * <code>createLinear</code> from the factory.
35 public class TmfTimestampTransformLinear
implements ITmfTimestampTransform
{
38 * Generated serial UID
40 private static final long serialVersionUID
= -4756608071358979461L;
43 * Respectively the slope and offset and this linear equation.
45 private final BigDecimal fAlpha
;
46 private final BigDecimal fBeta
;
48 private static final MathContext fMc
= MathContext
.DECIMAL128
;
53 public TmfTimestampTransformLinear() {
54 fAlpha
= BigDecimal
.ONE
;
55 fBeta
= BigDecimal
.ZERO
;
59 * Constructor with alpha and beta
62 * The slope of the linear transform
64 * The initial offset of the linear transform
66 public TmfTimestampTransformLinear(final double alpha
, final double beta
) {
67 fAlpha
= BigDecimal
.valueOf(alpha
);
68 fBeta
= BigDecimal
.valueOf(beta
);
72 * Constructor with alpha and beta in big decimal
75 * The slope of the linear transform
77 * The initial offset of the linear transform
79 public TmfTimestampTransformLinear(final BigDecimal fAlpha2
, final BigDecimal fBeta2
) {
80 if (fAlpha2
!= null) {
83 fAlpha
= BigDecimal
.ONE
;
88 fBeta
= BigDecimal
.ZERO
;
93 public ITmfTimestamp
transform(ITmfTimestamp timestamp
) {
94 BigDecimal newvalue
= BigDecimal
.valueOf(timestamp
.getValue()).multiply(fAlpha
, fMc
).add(fBeta
);
95 return new TmfTimestamp(timestamp
, newvalue
.longValue());
99 public long transform(long timestamp
) {
100 BigDecimal t
= BigDecimal
.valueOf(timestamp
).multiply(fAlpha
, fMc
).add(fBeta
);
101 return t
.longValue();
105 public ITmfTimestampTransform
composeWith(ITmfTimestampTransform composeWith
) {
106 if (composeWith
.equals(TmfTimestampTransform
.IDENTITY
)) {
107 /* If composing with identity, just return this */
109 } else if (composeWith
instanceof TmfTimestampTransformLinear
) {
110 /* If composeWith is a linear transform, add the two together */
111 TmfTimestampTransformLinear ttl
= (TmfTimestampTransformLinear
) composeWith
;
112 BigDecimal newAlpha
= fAlpha
.multiply(ttl
.fAlpha
, fMc
);
113 BigDecimal newBeta
= fAlpha
.multiply(ttl
.fBeta
, fMc
).add(fBeta
);
114 return new TmfTimestampTransformLinear(newAlpha
, newBeta
);
117 * We do not know what to do with this kind of transform, just
125 public boolean equals(Object other
) {
126 boolean result
= false;
127 if (other
instanceof TmfTimestampTransformLinear
) {
128 TmfTimestampTransformLinear that
= (TmfTimestampTransformLinear
) other
;
129 result
= ((that
.fAlpha
.equals(fAlpha
)) && (that
.fBeta
.equals(fBeta
)));
135 public int hashCode() {
136 final int prime
= 31;
138 result
= (prime
* result
) + (fBeta
.multiply(fAlpha
).intValue());
143 public String
toString() {
144 return "TmfTimestampLinear [ alpha = " + fAlpha
.toString() + //$NON-NLS-1$
145 ", beta = " + fBeta
.toString() + //$NON-NLS-1$