View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.scale.LinearScale;
4   import org.djunits.unit.scale.Scale;
5   import org.djunits.unit.scale.StandardScale;
6   import org.djunits.unit.unitsystem.UnitSystem;
7   
8   /**
9    * A linear unit with easy-access constructor with a linear factor, and access to the linear factor. <br>
10   * A linear unit is a unit that is linearly related to the SI standard unit. E.g. Mile is linearly related to meter (the SI unit
11   * for length). Unlike temperature in degrees Celsius which is <strong>not</strong> linearly related to the Kelvin (the SI unit
12   * for temperature).
13   * <p>
14   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
18   * initial version Oct 11, 2015 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @param <U> the linear Unit.
22   */
23  public abstract class LinearUnit<U extends LinearUnit<U>> extends Unit<U>
24  {
25      /** */
26      private static final long serialVersionUID = 20151011L;
27  
28      /**
29       * Build a standard linear unit and create the fields for a unit. If the parameter standardUnit is true, it is a standard
30       * unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined unit where
31       * the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if
32       * standardUnit is false a UnitException is thrown as a RunTimeException.
33       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
34       *            name itself
35       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
36       *            unit, otherwise the abbreviation itself
37       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
38       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
39       *            unit
40       */
41      public LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
42              final boolean standardUnit)
43      {
44          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, standardUnit);
45      }
46  
47      /**
48       * Build a unit with a linear conversion factor to another unit. If the parameter standardUnit is true, it is a standard
49       * unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined unit where
50       * the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if
51       * standardUnit is false a UnitException is thrown as a RunTimeException.
52       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
53       *            name itself
54       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
55       *            unit, otherwise the abbreviation itself
56       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
57       * @param referenceUnit U; the unit to convert to
58       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
59       *            unit
60       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
61       *            unit
62       */
63      protected LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
64              final U referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
65      {
66          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem,
67                  referenceUnit == null ? StandardScale.SCALE
68                          : new LinearScale(
69                                  referenceUnit.getScale().getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit),
70                  standardUnit);
71      }
72  
73      /**
74       * Build a unit with a specific conversion scale to/from the standard unit. If the parameter standardUnit is true, it is a
75       * standard unit where name is the nameKey, and abbreviation is the abbreviationKey; if false, this unit is a user-defined
76       * unit where the localization files do not have an entry. If standardUnit is true, a UnitException is silently ignored; if
77       * standardUnit is false a UnitException is thrown as a RunTimeException.
78       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
79       *            name itself
80       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
81       *            unit, otherwise the abbreviation itself
82       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
83       * @param scale Scale; the conversion scale to use for this unit
84       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
85       *            unit
86       */
87      protected LinearUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
88              final Scale scale, final boolean standardUnit)
89      {
90          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, scale, standardUnit);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      @SuppressWarnings("checkstyle:designforextension")
96      public LinearScale getScale()
97      {
98          return (LinearScale) super.getScale();
99      }
100 
101     /**
102      * @return the conversion factor to the standard unit (e.g., the SI unit)
103      */
104     public final double getScaleFactor()
105     {
106         return getScale().getConversionFactorToStandardUnit();
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public int hashCode()
112     {
113         final int prime = 31;
114         int result = super.hashCode();
115         long temp;
116         temp = Double.doubleToLongBits(this.getScaleFactor());
117         result = prime * result + (int) (temp ^ (temp >>> 32));
118         return result;
119     }
120 
121     /** {@inheritDoc} */
122     @SuppressWarnings("checkstyle:needbraces")
123     @Override
124     public boolean equals(final Object obj)
125     {
126         if (this == obj)
127             return true;
128         if (!super.equals(obj))
129             return false;
130         if (getClass() != obj.getClass())
131             return false;
132         LinearUnit<?> other = (LinearUnit<?>) obj;
133         if (Double.doubleToLongBits(this.getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor()))
134             return false;
135         return true;
136     }
137 
138     /** {@inheritDoc} */
139     @SuppressWarnings("checkstyle:needbraces")
140     @Override
141     public boolean equalsIgnoreNaming(final Object obj)
142     {
143         if (this == obj)
144             return true;
145         if (getClass() != obj.getClass())
146             return false;
147         LinearUnit<?> other = (LinearUnit<?>) obj;
148         if (Double.doubleToLongBits(getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor()))
149             return false;
150         return true;
151     }
152 
153 }