analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / internal / tmf / core / synchronization / TmfTimestampTransformLinear.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 É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.tracecompass.internal.tmf.core.synchronization;
14
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17
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;
23
24 /**
25 * Class implementing a linear timestamp transform, with a slope and/or offset
26 *
27 * f(t) = alpha*t + beta
28 *
29 * @author Geneviève Bastien
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 /* 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);
113 } else {
114 /*
115 * We do not know what to do with this kind of transform, just
116 * return this
117 */
118 return this;
119 }
120 }
121
122 @Override
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)));
128 }
129 return result;
130 }
131
132 @Override
133 public int hashCode() {
134 final int prime = 31;
135 int result = 1;
136 result = (prime * result) + (fBeta.multiply(fAlpha).intValue());
137 return result;
138 }
139
140 @Override
141 public String toString() {
142 return "TmfTimestampLinear [ slope = " + fAlpha.toString() + //$NON-NLS-1$
143 ", offset = " + fBeta.toString() + //$NON-NLS-1$
144 " ]"; //$NON-NLS-1$
145 }
146
147 @Override
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)));
150 }
151
152 /**
153 * @return the slope alpha
154 */
155 public BigDecimal getAlpha() {
156 return fAlpha;
157 }
158
159 /**
160 * @return the offset beta
161 */
162 public BigDecimal getBeta() {
163 return fBeta;
164 }
165 }
This page took 0.036599 seconds and 5 git commands to generate.