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 correcponding 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       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
39       *            name itself
40       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
41       *            unit, otherwise the abbreviation itself
42       * @param unitSystem the unit system, e.g. SI or Imperial; often the unit system for an absolute unit will be OTHER
43       * @param scaleFactorToReferenceUnit double; multiply by this number to convert to the standard unit
44       * @param offsetToReferenceUnit double; the offset to the reference unit to add to convert to the standard (e.g., BASE) unit
45       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
46       *            unit
47       * @param relativeUnit RU; the corresponding relative unit belonging to this absolute unit
48       */
49      protected AbsoluteLinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
50              final UnitSystem unitSystem, final double scaleFactorToReferenceUnit, final double offsetToReferenceUnit,
51              final boolean standardUnit, final RU relativeUnit)
52      {
53          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem,
54                  new OffsetLinearScale(scaleFactorToReferenceUnit, offsetToReferenceUnit), standardUnit);
55          this.relativeUnit = relativeUnit;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final OffsetLinearScale getScale()
61      {
62          return (OffsetLinearScale) super.getScale();
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final double getOffset()
68      {
69          return getScale().getOffsetToStandardUnit();
70      }
71  
72      /**
73       * Return the corresponding relative unit that belongs to this absolute unit.
74       * @return the relative unit that belongs to this absolute unit
75       */
76      public final RU getRelativeUnit()
77      {
78          return this.relativeUnit;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public int hashCode()
84      {
85          final int prime = 31;
86          int result = super.hashCode();
87          long temp;
88          temp = Double.doubleToLongBits(this.getOffset());
89          result = prime * result + (int) (temp ^ (temp >>> 32));
90          return result;
91      }
92  
93      /** {@inheritDoc} */
94      @SuppressWarnings("checkstyle:needbraces")
95      @Override
96      public boolean equals(final Object obj)
97      {
98          if (this == obj)
99              return true;
100         if (!super.equals(obj))
101             return false;
102         if (getClass() != obj.getClass())
103             return false;
104         AbsoluteLinearUnit<?, ?> other = (AbsoluteLinearUnit<?, ?>) obj;
105         if (Double.doubleToLongBits(this.getOffset()) != Double.doubleToLongBits(other.getOffset()))
106             return false;
107         return true;
108     }
109 
110     /** {@inheritDoc} */
111     @SuppressWarnings("checkstyle:needbraces")
112     @Override
113     public boolean equalsIgnoreNaming(final Object obj)
114     {
115         if (this == obj)
116             return true;
117         if (!super.equals(obj))
118             return false;
119         if (getClass() != obj.getClass())
120             return false;
121         AbsoluteLinearUnit<?, ?> other = (AbsoluteLinearUnit<?, ?>) obj;
122         if (Double.doubleToLongBits(getOffset()) != Double.doubleToLongBits(other.getOffset()))
123             return false;
124         return true;
125     }
126 
127 }