View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.IMPERIAL;
4   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.OTHER;
5   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_DERIVED;
6   
7   import org.opentrafficsim.core.unit.unitsystem.UnitSystem;
8   
9   /**
10   * AreaUnit defines a number of common units for areas.
11   * <p>
12   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version May 15, 2014 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   */
18  public class AreaUnit extends Unit<AreaUnit>
19  {
20      /** */
21      private static final long serialVersionUID = 20140607L;
22  
23      /** The unit of length for the area unit, e.g., meter. */
24      private final LengthUnit lengthUnit;
25  
26      /** The SI unit for area is m^2. */
27      public static final AreaUnit SI;
28  
29      /** m^2. */
30      public static final AreaUnit SQUARE_METER;
31  
32      /** km^2. */
33      public static final AreaUnit SQUARE_KM;
34  
35      /** cm^2. */
36      public static final AreaUnit SQUARE_CENTIMETER;
37  
38      /** cm^2. */
39      public static final AreaUnit SQUARE_MILLIMETER;
40  
41      /** are. */
42      public static final AreaUnit ARE;
43  
44      /** hectare. */
45      public static final AreaUnit HECTARE;
46  
47      /** mile^2. */
48      public static final AreaUnit SQUARE_MILE;
49  
50      /** ft^2. */
51      public static final AreaUnit SQUARE_FOOT;
52  
53      /** in^2. */
54      public static final AreaUnit SQUARE_INCH;
55  
56      /** yd^2. */
57      public static final AreaUnit SQUARE_YARD;
58  
59      /** acre (international). */
60      public static final AreaUnit ACRE;
61  
62      static
63      {
64          SI = new AreaUnit(LengthUnit.METER, "AreaUnit.square_meter", "AreaUnit.m^2", SI_DERIVED);
65          SQUARE_METER = SI;
66          SQUARE_KM = new AreaUnit(LengthUnit.KILOMETER, "AreaUnit.square_kilometer", "AreaUnit.km^2", SI_DERIVED);
67          SQUARE_CENTIMETER =
68                  new AreaUnit(LengthUnit.CENTIMETER, "AreaUnit.square_centimeter", "AreaUnit.cm^2", SI_DERIVED);
69          SQUARE_MILLIMETER =
70                  new AreaUnit(LengthUnit.MILLIMETER, "AreaUnit.square_millimeter", "AreaUnit.mm^2", SI_DERIVED);
71          ARE = new AreaUnit("AreaUnit.are", "AreaUnit.a", OTHER, SQUARE_METER, 100.0);
72          HECTARE = new AreaUnit("AreaUnit.hectare", "AreaUnit.ha", OTHER, ARE, 100.0);
73          SQUARE_MILE = new AreaUnit(LengthUnit.MILE, "AreaUnit.square_mile", "AreaUnit.mi^2", IMPERIAL);
74          SQUARE_FOOT = new AreaUnit(LengthUnit.FOOT, "AreaUnit.square_foot", "AreaUnit.ft^2", IMPERIAL);
75          SQUARE_INCH = new AreaUnit(LengthUnit.INCH, "AreaUnit.square_inch", "AreaUnit.in^2", IMPERIAL);
76          SQUARE_YARD = new AreaUnit(LengthUnit.YARD, "AreaUnit.square_yard", "AreaUnit.yd^2", IMPERIAL);
77          ACRE = new AreaUnit("AreaUnit.acre", "AreaUnit.ac", IMPERIAL, SQUARE_YARD, 4840.0);
78      }
79  
80      /**
81       * Define area unit based on length.
82       * @param lengthUnit the unit of length for the area unit, e.g., meter
83       * @param nameKey the key to the locale file for the long name of the unit
84       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
85       * @param unitSystem the unit system, e.g. SI or Imperial
86       */
87      public AreaUnit(final LengthUnit lengthUnit, final String nameKey, final String abbreviationKey,
88              final UnitSystem unitSystem)
89      {
90          super(nameKey, abbreviationKey, unitSystem, SQUARE_METER, lengthUnit.getConversionFactorToStandardUnit()
91                  * lengthUnit.getConversionFactorToStandardUnit(), true);
92          this.lengthUnit = lengthUnit;
93      }
94  
95      /**
96       * This constructor constructs a unit out of another defined unit, e.g. an are is 100 m^2.
97       * @param nameKey the key to the locale file for the long name of the unit
98       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
99       * @param unitSystem the unit system, e.g. SI or Imperial
100      * @param referenceUnit the unit to convert to
101      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given
102      *            reference unit
103      */
104     public AreaUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem,
105             final AreaUnit referenceUnit, final double conversionFactorToReferenceUnit)
106     {
107         super(nameKey, abbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit, true);
108         this.lengthUnit = referenceUnit.getLengthUnit();
109     }
110 
111     /**
112      * @return lengthUnit
113      */
114     public final LengthUnit getLengthUnit()
115     {
116         return this.lengthUnit;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public final AreaUnit getStandardUnit()
122     {
123         return SQUARE_METER;
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public final String getSICoefficientsString()
129     {
130         return "m2";
131     }
132 
133 }