View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.scale.OffsetLinearScale;
4   import org.djunits.unit.unitsystem.UnitSystem;
5   
6   /**
7    * An absolute unit is a unit that needs to be defined with a "distance" to the reference absolute unit. E.g., for Time, an
8    * absolute reference unit has to be chosen such as an Epoch (e.g., 0001-01-01 AD at 00:00), where other TimeUnits can be
9    * defined relative to that reference unit (e.g., POSIX time, 1-1-1970 at 00:00, which is 719162 days after the reference time).
10   * <p>
11   * An absolute unit is always a unit with an OffsetLinearScale. The offset defines how far away the origin of that absolute unit
12   * is compared to the base unit. The linear scale indicates in what default "steps" compared to the reference relative scale the
13   * absolute scale is used. For temperature, this is very clear. Degrees Celcius is shifted 273.15 compared to the origin of the
14   * Kelvin scale, and the linear unit is 1 (relatively, 1 Kelvin is one degree Celcius). For Degree Fahrenheit, the linear scale
15   * is 5/9, and the offset is 459.67. For Direction, North can be chosen as 0, and radians (SI) as the default angle. But one
16   * could also define a NORTH_DEGREES absolute direction unit, or a WEST_RADIANS one. Similar choices can be made for time and
17   * position.
18   * <p>
19   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
21   * </p>
22   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
23   * initial version Apr 27, 2017 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @param <AU> the absolute unit.
26   * @param <RU> the corresponding relative unit
27   */
28  public abstract class AbsoluteLinearUnit<AU extends LinearUnit<AU>, RU extends Unit<RU>> extends LinearUnit<AU>
29          implements AbsoluteUnit
30  {
31      /** */
32      private static final long serialVersionUID = 20170427L;
33  
34      /** the corresponding relative unit belonging to this absolute unit. */
35      private final RU relativeUnit;
36  
37      /**
38       * Build a standard unit with an absolute linear scale and create the fields for that unit.
39       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
40       * @param unitSystem the unit system, e.g. SI or Imperial; often the unit system for an absolute unit will be OTHER
41       * @param scaleFactorToReferenceUnit double; multiply by this number to convert to the standard unit
42       * @param offsetToReferenceUnit double; the offset to the reference unit to add to convert to the standard (e.g., BASE) unit
43       * @param relativeUnit RU; the corresponding relative unit belonging to this absolute unit
44       */
45      protected AbsoluteLinearUnit(final String abbreviationKey, final UnitSystem unitSystem,
46              final double scaleFactorToReferenceUnit, final double offsetToReferenceUnit, final RU relativeUnit)
47      {
48          super(abbreviationKey, unitSystem, new OffsetLinearScale(scaleFactorToReferenceUnit, offsetToReferenceUnit));
49          this.relativeUnit = relativeUnit;
50      }
51  
52      /**
53       * Build a user-defined unit with an absolute linear scale and create the fields for that unit.
54       * @param name String; the unit name
55       * @param abbreviation String; the unit abbreviation
56       * @param unitSystem the unit system, e.g. SI or Imperial; often the unit system for an absolute unit will be OTHER
57       * @param scaleFactorToReferenceUnit double; multiply by this number to convert to the standard unit
58       * @param offsetToReferenceUnit double; the offset to the reference unit to add to convert to the standard (e.g., BASE) unit
59       * @param relativeUnit RU; the corresponding relative unit belonging to this absolute unit
60       */
61      protected AbsoluteLinearUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
62              final double scaleFactorToReferenceUnit, final double offsetToReferenceUnit, final RU relativeUnit)
63      {
64          super(name, abbreviation, unitSystem, new OffsetLinearScale(scaleFactorToReferenceUnit, offsetToReferenceUnit));
65          this.relativeUnit = relativeUnit;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final OffsetLinearScale getScale()
71      {
72          return (OffsetLinearScale) super.getScale();
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final double getOffset()
78      {
79          return getScale().getOffsetToStandardUnit();
80      }
81  
82      /**
83       * Return the corresponding relative unit that belongs to this absolute unit.
84       * @return the relative unit that belongs to this absolute unit
85       */
86      public final RU getRelativeUnit()
87      {
88          return this.relativeUnit;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public int hashCode()
94      {
95          final int prime = 31;
96          int result = super.hashCode();
97          long temp;
98          temp = Double.doubleToLongBits(this.getOffset());
99          result = prime * result + (int) (temp ^ (temp >>> 32));
100         return result;
101     }
102 
103     /** {@inheritDoc} */
104     @SuppressWarnings("checkstyle:needbraces")
105     @Override
106     public boolean equals(final Object obj)
107     {
108         if (this == obj)
109             return true;
110         if (!super.equals(obj))
111             return false;
112         if (getClass() != obj.getClass())
113             return false;
114         AbsoluteLinearUnit<?, ?> other = (AbsoluteLinearUnit<?, ?>) obj;
115         if (Double.doubleToLongBits(this.getOffset()) != Double.doubleToLongBits(other.getOffset()))
116             return false;
117         return true;
118     }
119 
120     /** {@inheritDoc} */
121     @SuppressWarnings("checkstyle:needbraces")
122     @Override
123     public boolean equalsIgnoreNaming(final Object obj)
124     {
125         if (this == obj)
126             return true;
127         if (!super.equals(obj))
128             return false;
129         if (getClass() != obj.getClass())
130             return false;
131         AbsoluteLinearUnit<?, ?> other = (AbsoluteLinearUnit<?, ?>) obj;
132         if (Double.doubleToLongBits(getOffset()) != Double.doubleToLongBits(other.getOffset()))
133             return false;
134         return true;
135     }
136 
137 }