Tmf: Trace synchronization using network events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / 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.tmf.core.synchronization;
14
15 import java.io.Serializable;
16 import java.math.BigDecimal;
17 import java.math.MathContext;
18
19 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
20 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
21
22 /**
23 * Class implementing a linear timestamp transform, with a slope and/or offset
24 *
25 * f(t) = alpha*t + beta
26 *
27 * @author Geneviève Bastien
28 * @since 3.0
29 */
30 public class TmfTimestampTransformLinear implements ITmfTimestampTransform, Serializable {
31
32 /**
33 * Generated serial UID
34 */
35 private static final long serialVersionUID = -4756608071358979461L;
36
37 /**
38 * Respectively the slope and offset and this linear equation.
39 *
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
42 * errors
43 */
44 private final BigDecimal fAlpha;
45 private final BigDecimal fBeta;
46
47 private static final MathContext fMc = MathContext.DECIMAL128;
48
49 /**
50 * Default constructor
51 */
52 public TmfTimestampTransformLinear() {
53 fAlpha = BigDecimal.ONE;
54 fBeta = BigDecimal.ZERO;
55 }
56
57 /**
58 * Constructor with alpha and beta
59 *
60 * @param alpha
61 * The slope of the linear transform
62 * @param beta
63 * The initial offset of the linear transform
64 */
65 public TmfTimestampTransformLinear(final double alpha, final double beta) {
66 fAlpha = BigDecimal.valueOf(alpha);
67 fBeta = BigDecimal.valueOf(beta);
68 }
69
70 /**
71 * Constructor with alpha and beta in big decimal
72 *
73 * @param fAlpha2
74 * The slope of the linear transform
75 * @param fBeta2
76 * The initial offset of the linear transform
77 */
78 public TmfTimestampTransformLinear(final BigDecimal fAlpha2, final BigDecimal fBeta2) {
79 if (fAlpha2 != null) {
80 fAlpha = fAlpha2;
81 } else {
82 fAlpha = BigDecimal.ONE;
83 }
84 if (fBeta2 != null) {
85 fBeta = fBeta2;
86 } else {
87 fBeta = BigDecimal.ZERO;
88 }
89 }
90
91 @Override
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());
95 }
96
97 @Override
98 public long transform(long timestamp) {
99 BigDecimal t = BigDecimal.valueOf(timestamp).multiply(fAlpha, fMc).add(fBeta);
100 return t.longValue();
101 }
102
103 @Override
104 public ITmfTimestampTransform composeWith(ITmfTimestampTransform composeWith) {
105 if (composeWith.equals(TmfTimestampTransform.IDENTITY)) {
106 /* If composing with identity, just return this */
107 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);
114 } else {
115 /*
116 * We do not know what to do with this kind of transform, just
117 * return this
118 */
119 return this;
120 }
121 }
122
123 @Override
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)));
129 }
130 return result;
131 }
132
133 @Override
134 public int hashCode() {
135 final int prime = 31;
136 int result = 1;
137 result = (prime * result) + (fBeta.multiply(fAlpha).intValue());
138 return result;
139 }
140
141 @Override
142 public String toString() {
143 return "TmfTimestampLinear [ alpha = " + fAlpha.toString() + //$NON-NLS-1$
144 ", beta = " + fBeta.toString() + //$NON-NLS-1$
145 " ]"; //$NON-NLS-1$
146 }
147
148 }
This page took 0.033419 seconds and 5 git commands to generate.