View Javadoc
1   package org.djunits.unit.scale;
2   
3   import org.djunits.unit.util.UnitRuntimeException;
4   import org.djutils.exceptions.Throw;
5   
6   /**
7    * A Scale for linear transformations not involving a zero-offset, e.g. for Length, Time, Area. <br>
8    * A linear scale is a scale that is linearly relates a unit to the underlying SI standard unit. E.g. Mile is linearly related
9    * to meter (the SI unit for length) and the conversion is zero-based (0 miles equals 0 meter). Unlike temperature in degrees
10   * Celsius which is <strong>not</strong> linearly related to the Kelvin (the SI unit for temperature) because the conversion is
11   * not zero-based (0&deg;C is 273.15K).
12   * <p>
13   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
15   * </p>
16   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
18   */
19  public class LinearScale implements Scale
20  {
21      /** */
22      private static final long serialVersionUID = 20151011L;
23  
24      /** multiply by this number to convert to the standard (e.g., SI) unit. */
25      private final double conversionFactorToStandardUnit;
26  
27      /**
28       * Construct a Scale for linear transformations.
29       * @param conversionFactorToStandardUnit double; the conversion factor by which this number has to be multiplied to convert
30       *            it to the standard (e.g., SI) unit.
31       */
32      public LinearScale(final double conversionFactorToStandardUnit)
33      {
34          Throw.when(conversionFactorToStandardUnit == 0.0, UnitRuntimeException.class,
35                  "conversion factor for linear scale cannnot be 0");
36          this.conversionFactorToStandardUnit = conversionFactorToStandardUnit;
37      }
38  
39      @SuppressWarnings("checkstyle:designforextension")
40      @Override
41      public double toStandardUnit(final double value)
42      {
43          return value * this.conversionFactorToStandardUnit;
44      }
45  
46      @SuppressWarnings("checkstyle:designforextension")
47      @Override
48      public double fromStandardUnit(final double value)
49      {
50          return value / this.conversionFactorToStandardUnit;
51      }
52  
53      /**
54       * Retrieve the factor for conversion to the standard unit.
55       * @return double; the factor for conversion to the standard unit
56       */
57      public final double getConversionFactorToStandardUnit()
58      {
59          return this.conversionFactorToStandardUnit;
60      }
61  
62      @Override
63      public boolean isBaseSIScale()
64      {
65          return this.conversionFactorToStandardUnit == 1.0;
66      }
67  
68      @Override
69      public int hashCode()
70      {
71          final int prime = 31;
72          int result = 1;
73          long temp;
74          temp = Double.doubleToLongBits(this.conversionFactorToStandardUnit);
75          result = prime * result + (int) (temp ^ (temp >>> 32));
76          return result;
77      }
78  
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      @Override
97      public String toString()
98      {
99          return "LinearScale [conversionFactorToStandardUnit=" + this.conversionFactorToStandardUnit + "]";
100     }
101 
102 }