View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.IMPERIAL;
4   import static org.djunits.unit.unitsystem.UnitSystem.SI_BASE;
5   
6   import org.djunits.unit.unitsystem.UnitSystem;
7   
8   /**
9    * Standard length units. Several conversion factors have been taken from <a
10   * href="http://en.wikipedia.org/wiki/Conversion_of_units">http://en.wikipedia.org/wiki/Conversion_of_units</a>.
11   * <p>
12   * Copyright (c) 2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
14   * <p>
15   * $LastChangedDate: 2015-10-04 20:48:33 +0200 (Sun, 04 Oct 2015) $, @version $Revision: 87 $, by $Author: averbraeck $, initial
16   * version May 15, 2014 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class LengthUnit extends Unit<LengthUnit>
20  {
21      /** */
22      private static final long serialVersionUID = 20140607L;
23  
24      /** The SI unit for length is meter. */
25      public static final LengthUnit SI;
26  
27      /** meter. */
28      public static final LengthUnit METER;
29  
30      /** millimeter. */
31      public static final LengthUnit MILLIMETER;
32  
33      /** centimeter. */
34      public static final LengthUnit CENTIMETER;
35  
36      /** decimeter. */
37      public static final LengthUnit DECIMETER;
38  
39      /** decameter. */
40      public static final LengthUnit DEKAMETER;
41  
42      /** hectometer. */
43      public static final LengthUnit HECTOMETER;
44  
45      /** kilometer. */
46      public static final LengthUnit KILOMETER;
47  
48      /** foot (international) = 0.3048 m = 1/3 yd = 12 inches. */
49      public static final LengthUnit FOOT;
50  
51      /** inch (international) = 2.54 cm = 1/36 yd = 1/12 ft. */
52      public static final LengthUnit INCH;
53  
54      /** mile (international) = 5280 ft = 1760 yd. */
55      public static final LengthUnit MILE;
56  
57      /** nautical mile (international) = 1852 m. */
58      public static final LengthUnit NAUTICAL_MILE;
59  
60      /** yard (international) = 0.9144 m = 3 ft = 36 in. */
61      public static final LengthUnit YARD;
62  
63      static
64      {
65          SI = new LengthUnit("LengthUnit.meter", "LengthUnit.m", SI_BASE);
66          METER = SI;
67          MILLIMETER = new LengthUnit("LengthUnit.millimeter", "LengthUnit.mm", SI_BASE, METER, 0.001, true);
68          CENTIMETER = new LengthUnit("LengthUnit.centimeter", "LengthUnit.cm", SI_BASE, METER, 0.01, true);
69          DECIMETER = new LengthUnit("LengthUnit.decimeter", "LengthUnit.dm", SI_BASE, METER, 0.1, true);
70          DEKAMETER = new LengthUnit("LengthUnit.dekameter", "LengthUnit.dam", SI_BASE, METER, 10.0, true);
71          HECTOMETER = new LengthUnit("LengthUnit.hectometer", "LengthUnit.hm", SI_BASE, METER, 100.0, true);
72          KILOMETER = new LengthUnit("LengthUnit.kilometer", "LengthUnit.km", SI_BASE, METER, 1000.0, true);
73          FOOT = new LengthUnit("LengthUnit.foot", "LengthUnit.ft", IMPERIAL, METER, 0.3048, true);
74          INCH = new LengthUnit("LengthUnit.inch", "LengthUnit.in", IMPERIAL, FOOT, 1.0 / 12.0, true);
75          MILE = new LengthUnit("LengthUnit.mile", "LengthUnit.mi", IMPERIAL, FOOT, 5280.0, true);
76          NAUTICAL_MILE = new LengthUnit("LengthUnit.nauticalMile", "LengthUnit.NM", IMPERIAL, METER, 1852.0, true);
77          YARD = new LengthUnit("LengthUnit.yard", "LengthUnit.yd", IMPERIAL, FOOT, 3.0, true);
78      }
79  
80      /**
81       * Build a standard LengthUnit.
82       * @param nameKey the key to the locale file for the long name of the unit
83       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
84       * @param unitSystem the unit system, e.g. SI or Imperial
85       */
86      private LengthUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem)
87      {
88          super(nameKey, abbreviationKey, unitSystem, true);
89      }
90  
91      /**
92       * Build a LengthUnit with a conversion factor to another LengthUnit.
93       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
94       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
95       *            otherwise the abbreviation itself
96       * @param unitSystem the unit system, e.g. SI or Imperial
97       * @param referenceUnit the unit to convert to
98       * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
99       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
100      */
101     private LengthUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
102         final UnitSystem unitSystem, final LengthUnit referenceUnit, final double conversionFactorToReferenceUnit,
103         final boolean standardUnit)
104     {
105         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit,
106             standardUnit);
107     }
108 
109     /**
110      * Build a user-defined LengthUnit with a conversion factor to another LengthUnit.
111      * @param name the long name of the unit
112      * @param abbreviation the abbreviation of the unit
113      * @param unitSystem the unit system, e.g. SI or Imperial
114      * @param referenceUnit the unit to convert to
115      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
116      */
117     public LengthUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
118         final LengthUnit referenceUnit, final double conversionFactorToReferenceUnit)
119     {
120         this(name, abbreviation, unitSystem, referenceUnit, conversionFactorToReferenceUnit, false);
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final LengthUnit getStandardUnit()
126     {
127         return METER;
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public final String getSICoefficientsString()
133     {
134         return "m";
135     }
136 
137 }