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