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