View Javadoc
1   package org.djunits.unit.scale;
2   
3   import org.djunits.Throw;
4   import org.djunits.unit.util.UnitRuntimeException;
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-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim 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      /** {@inheritDoc} */
40      @SuppressWarnings("checkstyle:designforextension")
41      @Override
42      public double toStandardUnit(final double value)
43      {
44          return value * this.conversionFactorToStandardUnit;
45      }
46  
47      /** {@inheritDoc} */
48      @SuppressWarnings("checkstyle:designforextension")
49      @Override
50      public double fromStandardUnit(final double value)
51      {
52          return value / this.conversionFactorToStandardUnit;
53      }
54  
55      /**
56       * Retrieve the factor for conversion to the standard unit.
57       * @return double; the factor for conversion to the standard unit
58       */
59      public final double getConversionFactorToStandardUnit()
60      {
61          return this.conversionFactorToStandardUnit;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public boolean isBaseSIScale()
67      {
68          return this.conversionFactorToStandardUnit == 1.0;
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public int hashCode()
74      {
75          final int prime = 31;
76          int result = 1;
77          long temp;
78          temp = Double.doubleToLongBits(this.conversionFactorToStandardUnit);
79          result = prime * result + (int) (temp ^ (temp >>> 32));
80          return result;
81      }
82  
83      /** {@inheritDoc} */
84      @SuppressWarnings("checkstyle:needbraces")
85      @Override
86      public boolean equals(final Object obj)
87      {
88          if (this == obj)
89              return true;
90          if (obj == null)
91              return false;
92          if (getClass() != obj.getClass())
93              return false;
94          LinearScale./../../org/djunits/unit/scale/LinearScale.html#LinearScale">LinearScale other = (LinearScale) obj;
95          if (Double.doubleToLongBits(this.conversionFactorToStandardUnit) != Double
96                  .doubleToLongBits(other.conversionFactorToStandardUnit))
97              return false;
98          return true;
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public String toString()
104     {
105         return "LinearScale [conversionFactorToStandardUnit=" + this.conversionFactorToStandardUnit + "]";
106     }
107 
108 }