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-2019 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 /** */
19 private static final long serialVersionUID = 1L;
20
21 /** The offset that has to be taken into account for conversions, multiplied by the conversionFactorToStandardUnit. */
22 private final double offsetToStandardUnit;
23
24 /**
25 * Construct a Scale for linear transformations with an offset, e.g. for Temperature.
26 * @param conversionFactorToStandardUnit double; the conversion factor by which this number has to be multiplied to convert
27 * it to the standard (e.g., SI) unit.
28 * @param offsetToStandardUnit the offset that has to be taken into account for conversions; when converting to a standard
29 * unit, the offset is applied first.
30 */
31 public OffsetLinearScale(final double conversionFactorToStandardUnit, final double offsetToStandardUnit)
32 {
33 super(conversionFactorToStandardUnit);
34 this.offsetToStandardUnit = offsetToStandardUnit;
35 }
36
37 /** {@inheritDoc} */
38 @Override
39 public final double toStandardUnit(final double value)
40 {
41 return (value + this.offsetToStandardUnit) * getConversionFactorToStandardUnit();
42 }
43
44 /** {@inheritDoc} */
45 @Override
46 public final double fromStandardUnit(final double value)
47 {
48 return (value / getConversionFactorToStandardUnit()) - this.offsetToStandardUnit;
49 }
50
51 /**
52 * @return offsetToStandardUnit
53 */
54 public final double getOffsetToStandardUnit()
55 {
56 return this.offsetToStandardUnit;
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 public final boolean isBaseSIScale()
62 {
63 return getConversionFactorToStandardUnit() == 1.0 && this.offsetToStandardUnit == 0.0;
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public int hashCode()
69 {
70 final int prime = 31;
71 int result = super.hashCode();
72 long temp;
73 temp = Double.doubleToLongBits(this.offsetToStandardUnit);
74 result = prime * result + (int) (temp ^ (temp >>> 32));
75 return result;
76 }
77
78 /** {@inheritDoc} */
79 @SuppressWarnings("checkstyle:needbraces")
80 @Override
81 public boolean equals(final Object obj)
82 {
83 if (this == obj)
84 return true;
85 if (!super.equals(obj))
86 return false;
87 if (getClass() != obj.getClass())
88 return false;
89 OffsetLinearScale other = (OffsetLinearScale) obj;
90 if (Double.doubleToLongBits(this.offsetToStandardUnit) != Double.doubleToLongBits(other.offsetToStandardUnit))
91 return false;
92 return true;
93 }
94
95 }