View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.OTHER;
4   import static org.djunits.unit.unitsystem.UnitSystem.SI_ACCEPTED;
5   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
6   
7   import org.djunits.unit.scale.GradeScale;
8   import org.djunits.unit.scale.LinearScale;
9   import org.djunits.unit.scale.Scale;
10  import org.djunits.unit.unitsystem.UnitSystem;
11  
12  /**
13   * Standard angle unit. Several conversion factors have been taken from
14   * <a href="http://en.wikipedia.org/wiki/Conversion_of_units">http://en.wikipedia.org/wiki/Conversion_of_units</a>.
15   * <p>
16   * Note that the Angle is <b>counter</b>clockwise.
17   * <p>
18   * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
20   * <p>
21   * $LastChangedDate: 2019-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 324 $, by $Author: averbraeck $,
22   * initial version May 15, 2014 <br>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   */
25  public class AngleUnit extends Unit<AngleUnit>
26  {
27      /** */
28      private static final long serialVersionUID = 20140607L;
29  
30      /** The SI unit for angle is radian. */
31      public static final AngleUnit SI;
32  
33      /** radian. */
34      public static final AngleUnit RADIAN;
35  
36      /** percent (non-linear, 100% is 45 degrees; 90 degrees is infinite). */
37      public static final AngleUnit PERCENT;
38  
39      /** degree. */
40      public static final AngleUnit DEGREE;
41  
42      /** arcminute. */
43      public static final AngleUnit ARCMINUTE;
44  
45      /** arcsecond. */
46      public static final AngleUnit ARCSECOND;
47  
48      /** grad. */
49      public static final AngleUnit GRAD;
50  
51      /** centesimal arcminute. */
52      public static final AngleUnit CENTESIMAL_ARCMINUTE;
53  
54      /** centesimal arcsecond. */
55      public static final AngleUnit CENTESIMAL_ARCSECOND;
56  
57      static
58      {
59          SI = new AngleUnit("AngleUnit.radian", "AngleUnit.rad", SI_DERIVED);
60          RADIAN = SI;
61          DEGREE = new AngleUnit("AngleUnit.degree", "AngleUnit.deg", SI_ACCEPTED, RADIAN, Math.PI / 180.0, true);
62          ARCMINUTE = new AngleUnit("AngleUnit.arcminute", "AngleUnit.arcmin", SI_ACCEPTED, DEGREE, 1.0 / 60.0, true);
63          ARCSECOND = new AngleUnit("AngleUnit.arcsecond", "AngleUnit.arcsec", SI_ACCEPTED, DEGREE, 1.0 / 3600.0, true);
64          GRAD = new AngleUnit("AngleUnit.gradian", "AngleUnit.grad", OTHER, RADIAN, 2.0 * Math.PI / 400.0, true);
65          CENTESIMAL_ARCMINUTE =
66                  new AngleUnit("AngleUnit.centesimal_arcminute", "AngleUnit.centesimal_arcmin", OTHER, GRAD, 1.0 / 100.0, true);
67          CENTESIMAL_ARCSECOND = new AngleUnit("AngleUnit.centesimal_arcsecond", "AngleUnit.centesimal_arcsec", OTHER, GRAD,
68                  1.0 / 10000.0, true);
69          PERCENT = new AngleUnit("AngleUnit.percent", "AngleUnit.perc", OTHER, new GradeScale(0.01), true);
70  
71      }
72  
73      /**
74       * Build a standard unit.
75       * @param nameKey String; the key to the locale file for the long name of the unit
76       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
77       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
78       */
79      private AngleUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem)
80      {
81          super(nameKey, abbreviationKey, unitSystem, true);
82      }
83  
84      /**
85       * Build a unit by converting it from another unit.
86       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
87       *            name itself
88       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
89       *            unit, otherwise the abbreviation itself
90       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
91       * @param referenceUnit AngleUnit; the unit to convert to
92       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
93       *            unit
94       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
95       *            unit
96       */
97      private AngleUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
98              final AngleUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
99      {
100         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, new LinearScale(
101                 ((LinearScale) referenceUnit.getScale()).getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit),
102                 standardUnit);
103     }
104 
105     /**
106      * Build a user-defined unit with a conversion factor to another unit.
107      * @param name String; the long name of the unit
108      * @param abbreviation String; the abbreviation of the unit
109      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
110      * @param referenceUnit AngleUnit; the unit to convert to
111      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
112      *            unit
113      */
114     public AngleUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final AngleUnit referenceUnit,
115             final double scaleFactorToReferenceUnit)
116     {
117         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
118     }
119 
120     /**
121      * Build an angle-slope unit with its own scale.
122      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
123      *            name itself
124      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
125      *            unit, otherwise the abbreviation itself
126      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
127      * @param scale Scale; the scale to use
128      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
129      *            unit
130      */
131     private AngleUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
132             final Scale scale, final boolean standardUnit)
133     {
134         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, scale, standardUnit);
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public final AngleUnit getStandardUnit()
140     {
141         return RADIAN;
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public final String getSICoefficientsString()
147     {
148         return "1";
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public int hashCode()
154     {
155         final int prime = 31;
156         int result = super.hashCode();
157         result = prime * result + ((getScale() == null) ? 0 : getScale().hashCode());
158         return result;
159     }
160 
161     /** {@inheritDoc} */
162     @SuppressWarnings("checkstyle:needbraces")
163     @Override
164     public boolean equals(final Object obj)
165     {
166         if (this == obj)
167             return true;
168         if (!super.equals(obj))
169             return false;
170         if (getClass() != obj.getClass())
171             return false;
172         AngleUnit other = (AngleUnit) obj;
173         if (getScale() == null)
174         {
175             if (other.getScale() != null)
176                 return false;
177         }
178         else if (!getScale().equals(other.getScale()))
179             return false;
180         return true;
181     }
182 
183     /** {@inheritDoc} */
184     @SuppressWarnings("checkstyle:needbraces")
185     @Override
186     public boolean equalsIgnoreNaming(final Object obj)
187     {
188         if (this == obj)
189             return true;
190         if (getClass() != obj.getClass())
191             return false;
192         AngleUnit other = (AngleUnit) obj;
193         if (getScale() == null)
194         {
195             if (other.getScale() != null)
196                 return false;
197         }
198         else if (!getScale().equals(other.getScale()))
199             return false;
200         return true;
201     }
202 
203 }