1 package org.djunits.unit.scale;
2
3 /**
4 * A Scale for linear transformations with an offset that has to be applied first when converting to the standard (SI) unit,
5 * before the scaling takes place, e.g. for Temperature. As an example, transform from Degrees Fahrenheit to Kelvin (SI). The
6 * conversion is K = (F + 459.67) × 5⁄9, and F = K × 9⁄5 − 459.67.
7 * <p>
8 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10 * </p>
11 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
12 * initial version Oct 11, 2015 <br>
13 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
15 */
16 public class OffsetLinearScale extends LinearScale implements Scale
17 {
18 /** The offset that has to be taken into account for conversions, multiplied by the conversionFactorToStandardUnit. */
19 private final double offsetToStandardUnit;
20
21 /**
22 * Construct a Scale for linear transformations with an offset, e.g. for Temperature.
23 * @param conversionFactorToStandardUnit the conversion factor by which this number has to be multiplied to convert it to
24 * the standard (e.g., SI) unit.
25 * @param offsetToStandardUnit the offset that has to be taken into account for conversions; when converting to a standard
26 * unit, the offset is applied first.
27 */
28 public OffsetLinearScale(final double conversionFactorToStandardUnit, final double offsetToStandardUnit)
29 {
30 super(conversionFactorToStandardUnit);
31 this.offsetToStandardUnit = offsetToStandardUnit;
32 }
33
34 /** {@inheritDoc} */
35 @Override
36 public final double toStandardUnit(final double value)
37 {
38 return (value + this.offsetToStandardUnit) * getConversionFactorToStandardUnit();
39 }
40
41 /** {@inheritDoc} */
42 @Override
43 public final double fromStandardUnit(final double value)
44 {
45 return (value / getConversionFactorToStandardUnit()) - this.offsetToStandardUnit;
46 }
47
48 /**
49 * @return offsetToStandardUnit
50 */
51 public final double getOffsetToStandardUnit()
52 {
53 return this.offsetToStandardUnit;
54 }
55
56 /** {@inheritDoc} */
57 @Override
58 public final boolean isBaseSIScale()
59 {
60 return getConversionFactorToStandardUnit() == 1.0 && this.offsetToStandardUnit == 0.0;
61 }
62
63 /** {@inheritDoc} */
64 @Override
65 public int hashCode()
66 {
67 final int prime = 31;
68 int result = super.hashCode();
69 long temp;
70 temp = Double.doubleToLongBits(this.offsetToStandardUnit);
71 result = prime * result + (int) (temp ^ (temp >>> 32));
72 return result;
73 }
74
75 /** {@inheritDoc} */
76 @SuppressWarnings("checkstyle:needbraces")
77 @Override
78 public boolean equals(final Object obj)
79 {
80 if (this == obj)
81 return true;
82 if (!super.equals(obj))
83 return false;
84 if (getClass() != obj.getClass())
85 return false;
86 OffsetLinearScale other = (OffsetLinearScale) obj;
87 if (Double.doubleToLongBits(this.offsetToStandardUnit) != Double.doubleToLongBits(other.offsetToStandardUnit))
88 return false;
89 return true;
90 }
91
92 }