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.OTHER;
5   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
6   
7   import org.djunits.unit.unitsystem.UnitSystem;
8   
9   /**
10   * AreaUnit defines a number of common units for areas.
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 AreaUnit extends Unit<AreaUnit>
20  {
21      /** */
22      private static final long serialVersionUID = 20140607L;
23  
24      /** The unit of length for the area unit, e.g., meter. */
25      private final LengthUnit lengthUnit;
26  
27      /** The SI unit for area is m^2. */
28      public static final AreaUnit SI;
29  
30      /** m^2. */
31      public static final AreaUnit SQUARE_METER;
32  
33      /** km^2. */
34      public static final AreaUnit SQUARE_KM;
35  
36      /** cm^2. */
37      public static final AreaUnit SQUARE_CENTIMETER;
38  
39      /** cm^2. */
40      public static final AreaUnit SQUARE_MILLIMETER;
41  
42      /** are. */
43      public static final AreaUnit ARE;
44  
45      /** hectare. */
46      public static final AreaUnit HECTARE;
47  
48      /** mile^2. */
49      public static final AreaUnit SQUARE_MILE;
50  
51      /** ft^2. */
52      public static final AreaUnit SQUARE_FOOT;
53  
54      /** in^2. */
55      public static final AreaUnit SQUARE_INCH;
56  
57      /** yd^2. */
58      public static final AreaUnit SQUARE_YARD;
59  
60      /** acre (international). */
61      public static final AreaUnit ACRE;
62  
63      static
64      {
65          SI = new AreaUnit(LengthUnit.METER, "AreaUnit.square_meter", "AreaUnit.m^2", SI_DERIVED, true);
66          SQUARE_METER = SI;
67          SQUARE_KM = new AreaUnit(LengthUnit.KILOMETER, "AreaUnit.square_kilometer", "AreaUnit.km^2", SI_DERIVED, true);
68          SQUARE_CENTIMETER =
69              new AreaUnit(LengthUnit.CENTIMETER, "AreaUnit.square_centimeter", "AreaUnit.cm^2", SI_DERIVED, true);
70          SQUARE_MILLIMETER =
71              new AreaUnit(LengthUnit.MILLIMETER, "AreaUnit.square_millimeter", "AreaUnit.mm^2", SI_DERIVED, true);
72          ARE = new AreaUnit("AreaUnit.are", "AreaUnit.a", OTHER, SQUARE_METER, 100.0, true);
73          HECTARE = new AreaUnit("AreaUnit.hectare", "AreaUnit.ha", OTHER, ARE, 100.0, true);
74          SQUARE_MILE = new AreaUnit(LengthUnit.MILE, "AreaUnit.square_mile", "AreaUnit.mi^2", IMPERIAL, true);
75          SQUARE_FOOT = new AreaUnit(LengthUnit.FOOT, "AreaUnit.square_foot", "AreaUnit.ft^2", IMPERIAL, true);
76          SQUARE_INCH = new AreaUnit(LengthUnit.INCH, "AreaUnit.square_inch", "AreaUnit.in^2", IMPERIAL, true);
77          SQUARE_YARD = new AreaUnit(LengthUnit.YARD, "AreaUnit.square_yard", "AreaUnit.yd^2", IMPERIAL, true);
78          ACRE = new AreaUnit("AreaUnit.acre", "AreaUnit.ac", IMPERIAL, SQUARE_YARD, 4840.0, true);
79      }
80  
81      /**
82       * Define area unit based on length.
83       * @param lengthUnit the unit of length for the area unit, e.g., meter
84       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
85       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
86       *            otherwise the abbreviation itself
87       * @param unitSystem the unit system, e.g. SI or Imperial
88       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
89       */
90      private AreaUnit(final LengthUnit lengthUnit, final String nameOrNameKey,
91          final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
92      {
93          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, SQUARE_METER, lengthUnit
94              .getConversionFactorToStandardUnit()
95              * lengthUnit.getConversionFactorToStandardUnit(), standardUnit);
96          this.lengthUnit = lengthUnit;
97      }
98  
99      /**
100      * Create a user-defined area unit based on length.
101      * @param lengthUnit the unit of length for the area unit, e.g., meter
102      * @param name the long name of the unit
103      * @param abbreviation the abbreviation of the unit
104      * @param unitSystem the unit system, e.g. SI or Imperial
105      */
106     public AreaUnit(final LengthUnit lengthUnit, final String name, final String abbreviation,
107         final UnitSystem unitSystem)
108     {
109         this(lengthUnit, name, abbreviation, unitSystem, false);
110     }
111 
112     /**
113      * This constructor constructs a unit out of another defined unit, e.g. an are is 100 m^2.
114      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
115      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
116      *            otherwise the abbreviation itself
117      * @param unitSystem the unit system, e.g. SI or Imperial
118      * @param referenceUnit the unit to convert to
119      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
120      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
121      */
122     private AreaUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
123         final UnitSystem unitSystem, final AreaUnit referenceUnit, final double conversionFactorToReferenceUnit,
124         final boolean standardUnit)
125     {
126         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit,
127             standardUnit);
128         this.lengthUnit = referenceUnit.getLengthUnit();
129     }
130 
131     /**
132      * Build a user-defined unit with a conversion factor to another unit, e.g. an are is 100 m^2.
133      * @param name the long name of the unit
134      * @param abbreviation the abbreviation of the unit
135      * @param unitSystem the unit system, e.g. SI or Imperial
136      * @param referenceUnit the unit to convert to
137      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
138      */
139     public AreaUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
140         final AreaUnit referenceUnit, final double conversionFactorToReferenceUnit)
141     {
142         this(name, abbreviation, unitSystem, referenceUnit, conversionFactorToReferenceUnit, false);
143     }
144 
145     /**
146      * @return lengthUnit
147      */
148     public final LengthUnit getLengthUnit()
149     {
150         return this.lengthUnit;
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public final AreaUnit getStandardUnit()
156     {
157         return SQUARE_METER;
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public final String getSICoefficientsString()
163     {
164         return "m2";
165     }
166 
167 }