View Javadoc
1   package org.djunits.unit.scale;
2   
3   /**
4    * A Scale for linear transformations not involving a zero-offset, e.g. for Length, Time, Area. <br>
5    * A linear scale is a scale that is linearly relates a unit to the underlying SI standard unit. E.g. Mile is linearly related
6    * to meter (the SI unit for length) and the conversion is zero-based (0 miles equals 0 meter). Unlike temperature in degrees
7    * Celsius which is <strong>not</strong> linearly related to the Kelvin (the SI unit for temperature) because the conversion is
8    * not zero-based (0&deg;C is 273.15K).
9    * <p>
10   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12   * </p>
13   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
14   * initial version Oct 11, 2015 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public class LinearScale implements Scale
19  {
20      /** multiply by this number to convert to the standard (e.g., SI) unit. */
21      private final double conversionFactorToStandardUnit;
22  
23      /**
24       * Construct a Scale for linear transformations.
25       * @param conversionFactorToStandardUnit double; the conversion factor by which this number has to be multiplied to convert
26       *            it to the standard (e.g., SI) unit.
27       */
28      public LinearScale(final double conversionFactorToStandardUnit)
29      {
30          super();
31          this.conversionFactorToStandardUnit = conversionFactorToStandardUnit;
32      }
33  
34      /** {@inheritDoc} */
35      @SuppressWarnings("checkstyle:designforextension")
36      @Override
37      public double toStandardUnit(final double value)
38      {
39          return value * this.conversionFactorToStandardUnit;
40      }
41  
42      /** {@inheritDoc} */
43      @SuppressWarnings("checkstyle:designforextension")
44      @Override
45      public double fromStandardUnit(final double value)
46      {
47          return value / this.conversionFactorToStandardUnit;
48      }
49  
50      /**
51       * @return conversionFactorToStandardUnit
52       */
53      public final double getConversionFactorToStandardUnit()
54      {
55          return this.conversionFactorToStandardUnit;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      @SuppressWarnings("checkstyle:designforextension")
61      public boolean isBaseSIScale()
62      {
63          return this.conversionFactorToStandardUnit == 1.0;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public int hashCode()
69      {
70          final int prime = 31;
71          int result = 1;
72          long temp;
73          temp = Double.doubleToLongBits(this.conversionFactorToStandardUnit);
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 (obj == null)
86              return false;
87          if (getClass() != obj.getClass())
88              return false;
89          LinearScale other = (LinearScale) obj;
90          if (Double.doubleToLongBits(this.conversionFactorToStandardUnit) != Double
91                  .doubleToLongBits(other.conversionFactorToStandardUnit))
92              return false;
93          return true;
94      }
95  
96  }