Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / synchronization / TmfTimestampTransformLinear.java
1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.core.synchronization;
14
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17
18 import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
19 import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory;
20 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
21 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
22
23 /**
24 * Class implementing a linear timestamp transform, with a slope and/or offset
25 *
26 * f(t) = alpha*t + beta
27 *
28 * @author Geneviève Bastien
29 * @since 3.0
30 */
31 public class TmfTimestampTransformLinear implements ITmfTimestampTransformInvertible {
32
33 /**
34 * Generated serial UID
35 */
36 private static final long serialVersionUID = -4756608071358979461L;
37
38 /**
39 * Respectively the slope and offset and this linear equation.
40 */
41 private final BigDecimal fAlpha;
42 private final BigDecimal fBeta;
43
44 private static final MathContext fMc = MathContext.DECIMAL128;
45
46 /**
47 * Default constructor
48 */
49 public TmfTimestampTransformLinear() {
50 fAlpha = BigDecimal.ONE;
51 fBeta = BigDecimal.ZERO;
52 }
53
54 /**
55 * Constructor with alpha and beta
56 *
57 * @param alpha
58 * The slope of the linear transform
59 * @param beta
60 * The initial offset of the linear transform
61 */
62 public TmfTimestampTransformLinear(final double alpha, final double beta) {
63 fAlpha = BigDecimal.valueOf(alpha);
64 fBeta = BigDecimal.valueOf(beta);
65 }
66
67 /**
68 * Constructor with alpha and beta in big decimal
69 *
70 * @param fAlpha2
71 * The slope of the linear transform
72 * @param fBeta2
73 * The initial offset of the linear transform
74 */
75 public TmfTimestampTransformLinear(final BigDecimal fAlpha2, final BigDecimal fBeta2) {
76 if (fAlpha2 != null) {
77 fAlpha = fAlpha2;
78 } else {
79 fAlpha = BigDecimal.ONE;
80 }
81 if (fBeta2 != null) {
82 fBeta = fBeta2;
83 } else {
84 fBeta = BigDecimal.ZERO;
85 }
86 }
87
88 @Override
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());
92 }
93
94 @Override
95 public long transform(long timestamp) {
96 BigDecimal t = BigDecimal.valueOf(timestamp).multiply(fAlpha, fMc).add(fBeta);
97 return t.longValue();
98 }
99
100 @Override
101 public ITmfTimestampTransform composeWith(ITmfTimestampTransform composeWith) {
102 if (composeWith.equals(TmfTimestampTransform.IDENTITY)) {
103 /* If composing with identity, just return this */
104 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 return TimestampTransformFactory.createLinear(newAlpha, newBeta);
111 } else {
112 /*
113 * We do not know what to do with this kind of transform, just
114 * return this
115 */
116 return this;
117 }
118 }
119
120 @Override
121 public boolean equals(Object other) {
122 boolean result = false;
123 if (other instanceof TmfTimestampTransformLinear) {
124 TmfTimestampTransformLinear that = (TmfTimestampTransformLinear) other;
125 result = ((that.fAlpha.equals(fAlpha)) && (that.fBeta.equals(fBeta)));
126 }
127 return result;
128 }
129
130 @Override
131 public int hashCode() {
132 final int prime = 31;
133 int result = 1;
134 result = (prime * result) + (fBeta.multiply(fAlpha).intValue());
135 return result;
136 }
137
138 @Override
139 public String toString() {
140 return "TmfTimestampLinear [ slope = " + fAlpha.toString() + //$NON-NLS-1$
141 ", offset = " + fBeta.toString() + //$NON-NLS-1$
142 " ]"; //$NON-NLS-1$
143 }
144
145 @Override
146 public ITmfTimestampTransform inverse() {
147 return TimestampTransformFactory.createLinear(BigDecimal.ONE.divide(fAlpha, fMc), BigDecimal.valueOf(-1).multiply(fBeta).divide(fAlpha, fMc));
148 }
149
150 }
This page took 0.037337 seconds and 5 git commands to generate.