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-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, 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.rad", SI_DERIVED);
60          RADIAN = SI;
61          DEGREE = new AngleUnit("AngleUnit.deg", SI_ACCEPTED, RADIAN, Math.PI / 180.0);
62          ARCMINUTE = new AngleUnit("AngleUnit.arcmin", SI_ACCEPTED, DEGREE, 1.0 / 60.0);
63          ARCSECOND = new AngleUnit("AngleUnit.arcsec", SI_ACCEPTED, DEGREE, 1.0 / 3600.0);
64          GRAD = new AngleUnit("AngleUnit.grad", OTHER, RADIAN, 2.0 * Math.PI / 400.0);
65          CENTESIMAL_ARCMINUTE = new AngleUnit("AngleUnit.centesimal_arcmin", OTHER, GRAD, 1.0 / 100.0);
66          CENTESIMAL_ARCSECOND = new AngleUnit("AngleUnit.centesimal_arcsec", OTHER, GRAD, 1.0 / 10000.0);
67          PERCENT = new AngleUnit("AngleUnit.perc", OTHER, new GradeScale(0.01));
68  
69      }
70  
71      /**
72       * Build a standard unit.
73       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
74       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
75       */
76      private AngleUnit(final String abbreviationKey, final UnitSystem unitSystem)
77      {
78          super(abbreviationKey, unitSystem);
79      }
80  
81      /**
82       * Build a unit by converting it from another unit.
83       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
84       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
85       * @param referenceUnit AngleUnit; the unit to convert to
86       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
87       *            unit
88       */
89      private AngleUnit(final String abbreviationKey, final UnitSystem unitSystem, final AngleUnit referenceUnit,
90              final double scaleFactorToReferenceUnit)
91      {
92          super(abbreviationKey, unitSystem, new LinearScale(
93                  ((LinearScale) referenceUnit.getScale()).getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit));
94      }
95  
96      /**
97       * Build a user-defined unit with a conversion factor to another unit.
98       * @param name String; the long name of the unit
99       * @param abbreviation String; the abbreviation of the unit
100      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
101      * @param referenceUnit AngleUnit; the unit to convert to
102      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
103      *            unit
104      */
105     public AngleUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final AngleUnit referenceUnit,
106             final double scaleFactorToReferenceUnit)
107     {
108         super(name, abbreviation, unitSystem, new LinearScale(
109                 ((LinearScale) referenceUnit.getScale()).getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit));
110     }
111 
112     /**
113      * Build an angle-slope unit with its own scale.
114      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
115      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
116      * @param scale Scale; the scale to use
117      */
118     private AngleUnit(final String abbreviationKey, final UnitSystem unitSystem, final Scale scale)
119     {
120         super(abbreviationKey, unitSystem, scale);
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final AngleUnit getStandardUnit()
126     {
127         return RADIAN;
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public final String getSICoefficientsString()
133     {
134         return "1";
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public int hashCode()
140     {
141         final int prime = 31;
142         int result = super.hashCode();
143         result = prime * result + ((getScale() == null) ? 0 : getScale().hashCode());
144         return result;
145     }
146 
147     /** {@inheritDoc} */
148     @SuppressWarnings("checkstyle:needbraces")
149     @Override
150     public boolean equals(final Object obj)
151     {
152         if (this == obj)
153             return true;
154         if (!super.equals(obj))
155             return false;
156         if (getClass() != obj.getClass())
157             return false;
158         AngleUnit other = (AngleUnit) obj;
159         if (getScale() == null)
160         {
161             if (other.getScale() != null)
162                 return false;
163         }
164         else if (!getScale().equals(other.getScale()))
165             return false;
166         return true;
167     }
168 
169     /** {@inheritDoc} */
170     @SuppressWarnings("checkstyle:needbraces")
171     @Override
172     public boolean equalsIgnoreNaming(final Object obj)
173     {
174         if (this == obj)
175             return true;
176         if (getClass() != obj.getClass())
177             return false;
178         AngleUnit other = (AngleUnit) obj;
179         if (getScale() == null)
180         {
181             if (other.getScale() != null)
182                 return false;
183         }
184         else if (!getScale().equals(other.getScale()))
185             return false;
186         return true;
187     }
188 
189 }