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 unit with a linear scale and create the fields for that unit. For a standard unit, a UnitException is
30       * silently ignored.
31       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
32       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
33       */
34      public LinearUnit(final String abbreviationKey, final UnitSystem unitSystem)
35      {
36          super(abbreviationKey, unitSystem);
37      }
38  
39      /**
40       * Build a standard unit with a linear conversion factor to another unit.
41       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
42       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
43       * @param referenceUnit U; the unit to convert to
44       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
45       *            unit
46       */
47      protected LinearUnit(final String abbreviationKey, final UnitSystem unitSystem, final U referenceUnit,
48              final double scaleFactorToReferenceUnit)
49      {
50          super(abbreviationKey, unitSystem, referenceUnit == null ? StandardScale.SCALE
51                  : new LinearScale(referenceUnit.getScale().getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit));
52      }
53  
54      /**
55       * Build a standard unit with a specific conversion scale to/from the standard unit.
56       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
57       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
58       * @param scale Scale; the conversion scale to use for this unit
59       */
60      protected LinearUnit(final String abbreviationKey, final UnitSystem unitSystem, final Scale scale)
61      {
62          super(abbreviationKey, unitSystem, scale);
63      }
64  
65      /**
66       * Build a user-defined unit with a linear scale and create the fields for that unit.
67       * @param name String; the name of the unit
68       * @param abbreviation String; the abbreviation of the unit
69       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
70       */
71      public LinearUnit(final String name, final String abbreviation, final UnitSystem unitSystem)
72      {
73          super(name, abbreviation, unitSystem);
74      }
75  
76      /**
77       * Build a user-defined unit with a linear conversion factor to another unit.
78       * @param name String; the name of the unit
79       * @param abbreviation String; the abbreviation of the unit
80       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
81       * @param referenceUnit U; the unit to convert to
82       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
83       *            unit
84       */
85      protected LinearUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final U referenceUnit,
86              final double scaleFactorToReferenceUnit)
87      {
88          super(name, abbreviation, unitSystem, referenceUnit == null ? StandardScale.SCALE
89                  : new LinearScale(referenceUnit.getScale().getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit));
90      }
91  
92      /**
93       * Build a user-defined unit with a specific conversion scale to/from the standard unit.
94       * @param name String; the name of the unit
95       * @param abbreviation String; the abbreviation of the unit
96       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
97       * @param scale Scale; the conversion scale to use for this unit
98       */
99      protected LinearUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final Scale scale)
100     {
101         super(name, abbreviation, unitSystem, scale);
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     @SuppressWarnings("checkstyle:designforextension")
107     public LinearScale getScale()
108     {
109         return (LinearScale) super.getScale();
110     }
111 
112     /**
113      * @return the conversion factor to the standard unit (e.g., the SI unit)
114      */
115     public final double getScaleFactor()
116     {
117         return getScale().getConversionFactorToStandardUnit();
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public int hashCode()
123     {
124         final int prime = 31;
125         int result = super.hashCode();
126         long temp;
127         temp = Double.doubleToLongBits(this.getScaleFactor());
128         result = prime * result + (int) (temp ^ (temp >>> 32));
129         return result;
130     }
131 
132     /** {@inheritDoc} */
133     @SuppressWarnings("checkstyle:needbraces")
134     @Override
135     public boolean equals(final Object obj)
136     {
137         if (this == obj)
138             return true;
139         if (!super.equals(obj))
140             return false;
141         if (getClass() != obj.getClass())
142             return false;
143         LinearUnit<?> other = (LinearUnit<?>) obj;
144         if (Double.doubleToLongBits(this.getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor()))
145             return false;
146         return true;
147     }
148 
149     /** {@inheritDoc} */
150     @SuppressWarnings("checkstyle:needbraces")
151     @Override
152     public boolean equalsIgnoreNaming(final Object obj)
153     {
154         if (this == obj)
155             return true;
156         if (getClass() != obj.getClass())
157             return false;
158         LinearUnit<?> other = (LinearUnit<?>) obj;
159         if (Double.doubleToLongBits(getScaleFactor()) != Double.doubleToLongBits(other.getScaleFactor()))
160             return false;
161         return true;
162     }
163 
164 }