AngleUnit.java

  1. package org.djunits.unit;

  2. import static org.djunits.unit.unitsystem.UnitSystem.OTHER;
  3. import static org.djunits.unit.unitsystem.UnitSystem.SI_ACCEPTED;
  4. import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;

  5. import org.djunits.unit.scale.GradeScale;
  6. import org.djunits.unit.scale.LinearScale;
  7. import org.djunits.unit.scale.Scale;
  8. import org.djunits.unit.unitsystem.UnitSystem;

  9. /**
  10.  * Standard angle unit. Several conversion factors have been taken from
  11.  * <a href="http://en.wikipedia.org/wiki/Conversion_of_units">http://en.wikipedia.org/wiki/Conversion_of_units</a>.
  12.  * <p>
  13.  * Note that the Angle is <b>counter</b>clockwise.
  14.  * <p>
  15.  * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  16.  * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
  17.  * <p>
  18.  * $LastChangedDate: 2019-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, by $Author: averbraeck $,
  19.  * initial version May 15, 2014 <br>
  20.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  21.  */
  22. public class AngleUnit extends Unit<AngleUnit>
  23. {
  24.     /** */
  25.     private static final long serialVersionUID = 20140607L;

  26.     /** The SI unit for angle is radian. */
  27.     public static final AngleUnit SI;

  28.     /** radian. */
  29.     public static final AngleUnit RADIAN;

  30.     /** percent (non-linear, 100% is 45 degrees; 90 degrees is infinite). */
  31.     public static final AngleUnit PERCENT;

  32.     /** degree. */
  33.     public static final AngleUnit DEGREE;

  34.     /** arcminute. */
  35.     public static final AngleUnit ARCMINUTE;

  36.     /** arcsecond. */
  37.     public static final AngleUnit ARCSECOND;

  38.     /** grad. */
  39.     public static final AngleUnit GRAD;

  40.     /** centesimal arcminute. */
  41.     public static final AngleUnit CENTESIMAL_ARCMINUTE;

  42.     /** centesimal arcsecond. */
  43.     public static final AngleUnit CENTESIMAL_ARCSECOND;

  44.     static
  45.     {
  46.         SI = new AngleUnit("AngleUnit.rad", SI_DERIVED);
  47.         RADIAN = SI;
  48.         DEGREE = new AngleUnit("AngleUnit.deg", SI_ACCEPTED, RADIAN, Math.PI / 180.0);
  49.         ARCMINUTE = new AngleUnit("AngleUnit.arcmin", SI_ACCEPTED, DEGREE, 1.0 / 60.0);
  50.         ARCSECOND = new AngleUnit("AngleUnit.arcsec", SI_ACCEPTED, DEGREE, 1.0 / 3600.0);
  51.         GRAD = new AngleUnit("AngleUnit.grad", OTHER, RADIAN, 2.0 * Math.PI / 400.0);
  52.         CENTESIMAL_ARCMINUTE = new AngleUnit("AngleUnit.centesimal_arcmin", OTHER, GRAD, 1.0 / 100.0);
  53.         CENTESIMAL_ARCSECOND = new AngleUnit("AngleUnit.centesimal_arcsec", OTHER, GRAD, 1.0 / 10000.0);
  54.         PERCENT = new AngleUnit("AngleUnit.perc", OTHER, new GradeScale(0.01));

  55.     }

  56.     /**
  57.      * Build a standard unit.
  58.      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
  59.      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
  60.      */
  61.     private AngleUnit(final String abbreviationKey, final UnitSystem unitSystem)
  62.     {
  63.         super(abbreviationKey, unitSystem);
  64.     }

  65.     /**
  66.      * Build a unit by converting it from another unit.
  67.      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
  68.      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
  69.      * @param referenceUnit AngleUnit; the unit to convert to
  70.      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
  71.      *            unit
  72.      */
  73.     private AngleUnit(final String abbreviationKey, final UnitSystem unitSystem, final AngleUnit referenceUnit,
  74.             final double scaleFactorToReferenceUnit)
  75.     {
  76.         super(abbreviationKey, unitSystem, new LinearScale(
  77.                 ((LinearScale) referenceUnit.getScale()).getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit));
  78.     }

  79.     /**
  80.      * Build a user-defined unit with a conversion factor to another unit.
  81.      * @param name String; the long name of the unit
  82.      * @param abbreviation String; the abbreviation of the unit
  83.      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
  84.      * @param referenceUnit AngleUnit; the unit to convert to
  85.      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
  86.      *            unit
  87.      */
  88.     public AngleUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final AngleUnit referenceUnit,
  89.             final double scaleFactorToReferenceUnit)
  90.     {
  91.         super(name, abbreviation, unitSystem, new LinearScale(
  92.                 ((LinearScale) referenceUnit.getScale()).getConversionFactorToStandardUnit() * scaleFactorToReferenceUnit));
  93.     }

  94.     /**
  95.      * Build an angle-slope unit with its own scale.
  96.      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
  97.      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
  98.      * @param scale Scale; the scale to use
  99.      */
  100.     private AngleUnit(final String abbreviationKey, final UnitSystem unitSystem, final Scale scale)
  101.     {
  102.         super(abbreviationKey, unitSystem, scale);
  103.     }

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     public final AngleUnit getStandardUnit()
  107.     {
  108.         return RADIAN;
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public final String getSICoefficientsString()
  113.     {
  114.         return "1";
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public int hashCode()
  119.     {
  120.         final int prime = 31;
  121.         int result = super.hashCode();
  122.         result = prime * result + ((getScale() == null) ? 0 : getScale().hashCode());
  123.         return result;
  124.     }

  125.     /** {@inheritDoc} */
  126.     @SuppressWarnings("checkstyle:needbraces")
  127.     @Override
  128.     public boolean equals(final Object obj)
  129.     {
  130.         if (this == obj)
  131.             return true;
  132.         if (!super.equals(obj))
  133.             return false;
  134.         if (getClass() != obj.getClass())
  135.             return false;
  136.         AngleUnit other = (AngleUnit) obj;
  137.         if (getScale() == null)
  138.         {
  139.             if (other.getScale() != null)
  140.                 return false;
  141.         }
  142.         else if (!getScale().equals(other.getScale()))
  143.             return false;
  144.         return true;
  145.     }

  146.     /** {@inheritDoc} */
  147.     @SuppressWarnings("checkstyle:needbraces")
  148.     @Override
  149.     public boolean equalsIgnoreNaming(final Object obj)
  150.     {
  151.         if (this == obj)
  152.             return true;
  153.         if (getClass() != obj.getClass())
  154.             return false;
  155.         AngleUnit other = (AngleUnit) obj;
  156.         if (getScale() == null)
  157.         {
  158.             if (other.getScale() != null)
  159.                 return false;
  160.         }
  161.         else if (!getScale().equals(other.getScale()))
  162.             return false;
  163.         return true;
  164.     }

  165. }