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-2018 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: 2018-08-10 10:39:38 +0200 (Fri, 10 Aug 2018) $, @version $Revision: 287 $, 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 the key to the locale file for the long name of the unit
76       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
77       * @param 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 if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
87       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
88       *            otherwise the abbreviation itself
89       * @param unitSystem the unit system, e.g. SI or Imperial
90       * @param referenceUnit the unit to convert to
91       * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
92       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
93       */
94      private AngleUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
95              final AngleUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
96      {
97          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, new LinearScale(
98                  ((LinearScale) referenceUnit.getScale()).getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit),
99                  standardUnit);
100     }
101 
102     /**
103      * Build a user-defined unit with a conversion factor to another unit.
104      * @param name the long name of the unit
105      * @param abbreviation the abbreviation of the unit
106      * @param unitSystem the unit system, e.g. SI or Imperial
107      * @param referenceUnit the unit to convert to
108      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
109      */
110     public AngleUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final AngleUnit referenceUnit,
111             final double scaleFactorToReferenceUnit)
112     {
113         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
114     }
115 
116     /**
117      * Build an angle-slope unit with its own scale.
118      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
119      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
120      *            otherwise the abbreviation itself
121      * @param unitSystem the unit system, e.g. SI or Imperial
122      * @param scale the scale to use
123      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
124      */
125     private AngleUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
126             final Scale scale, final boolean standardUnit)
127     {
128         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, scale, standardUnit);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public final AngleUnit getStandardUnit()
134     {
135         return RADIAN;
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     public final String getSICoefficientsString()
141     {
142         return "1";
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     public int hashCode()
148     {
149         final int prime = 31;
150         int result = super.hashCode();
151         result = prime * result + ((getScale() == null) ? 0 : getScale().hashCode());
152         return result;
153     }
154 
155     /** {@inheritDoc} */
156     @SuppressWarnings("checkstyle:needbraces")
157     @Override
158     public boolean equals(final Object obj)
159     {
160         if (this == obj)
161             return true;
162         if (!super.equals(obj))
163             return false;
164         if (getClass() != obj.getClass())
165             return false;
166         AngleUnit other = (AngleUnit) obj;
167         if (getScale() == null)
168         {
169             if (other.getScale() != null)
170                 return false;
171         }
172         else if (!getScale().equals(other.getScale()))
173             return false;
174         return true;
175     }
176 
177     /** {@inheritDoc} */
178     @SuppressWarnings("checkstyle:needbraces")
179     @Override
180     public boolean equalsIgnoreNaming(final Object obj)
181     {
182         if (this == obj)
183             return true;
184         if (getClass() != obj.getClass())
185             return false;
186         AngleUnit other = (AngleUnit) obj;
187         if (getScale() == null)
188         {
189             if (other.getScale() != null)
190                 return false;
191         }
192         else if (!getScale().equals(other.getScale()))
193             return false;
194         return true;
195     }
196     
197     
198 }