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