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-2018 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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
16   * initial version May 15, 2014 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class AreaUnit extends LinearUnit<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      /** am^2. */
31      public static final AreaUnit SQUARE_ATTOMETER;
32  
33      /** fm^2. */
34      public static final AreaUnit SQUARE_FEMTOMETER;
35  
36      /** pm^2. */
37      public static final AreaUnit SQUARE_PICOMETER;
38  
39      /** nm^2. */
40      public static final AreaUnit SQUARE_NANOMETER;
41  
42      /** &#181;m^2. */
43      public static final AreaUnit SQUARE_MICROMETER;
44  
45      /** mm^2. */
46      public static final AreaUnit SQUARE_MILLIMETER;
47  
48      /** m^2. */
49      public static final AreaUnit SQUARE_METER;
50  
51      /** cm^2. */
52      public static final AreaUnit SQUARE_CENTIMETER;
53  
54      /** dm^2. */
55      public static final AreaUnit SQUARE_DECIMETER;
56  
57      /** dam^2. */
58      public static final AreaUnit SQUARE_DEKAMETER;
59  
60      /** hm^2. */
61      public static final AreaUnit SQUARE_HECTOMETER;
62  
63      /** km^2. */
64      public static final AreaUnit SQUARE_KILOMETER;
65  
66      /** Mm^2. */
67      public static final AreaUnit SQUARE_MEGAMETER;
68  
69      /** centiare. */
70      public static final AreaUnit CENTIARE;
71  
72      /** are. */
73      public static final AreaUnit ARE;
74  
75      /** hectare. */
76      public static final AreaUnit HECTARE;
77  
78      /** mile^2. */
79      public static final AreaUnit SQUARE_MILE;
80  
81      /** Nautical mile^2. */
82      public static final AreaUnit SQUARE_NAUTICAL_MILE;
83  
84      /** ft^2. */
85      public static final AreaUnit SQUARE_FOOT;
86  
87      /** in^2. */
88      public static final AreaUnit SQUARE_INCH;
89  
90      /** yd^2. */
91      public static final AreaUnit SQUARE_YARD;
92  
93      /** acre (international). */
94      public static final AreaUnit ACRE;
95  
96      static
97      {
98          SI = new AreaUnit(LengthUnit.METER, "AreaUnit.square_meter", "AreaUnit.m^2", SI_DERIVED, true);
99          SQUARE_METER = SI;
100         SQUARE_ATTOMETER = new AreaUnit(LengthUnit.ATTOMETER, "AreaUnit.square_attometer", "AreaUnit.am^2", SI_DERIVED, true);
101         SQUARE_FEMTOMETER =
102                 new AreaUnit(LengthUnit.FEMTOMETER, "AreaUnit.square_femtometer", "AreaUnit.fm^2", SI_DERIVED, true);
103         SQUARE_PICOMETER = new AreaUnit(LengthUnit.PICOMETER, "AreaUnit.square_picometer", "AreaUnit.pm^2", SI_DERIVED, true);
104         SQUARE_NANOMETER = new AreaUnit(LengthUnit.NANOMETER, "AreaUnit.square_nanometer", "AreaUnit.nm^2", SI_DERIVED, true);
105         SQUARE_MICROMETER =
106                 new AreaUnit(LengthUnit.MICROMETER, "AreaUnit.square_micrometer", "AreaUnit.mum^2", SI_DERIVED, true);
107         SQUARE_MILLIMETER =
108                 new AreaUnit(LengthUnit.MILLIMETER, "AreaUnit.square_millimeter", "AreaUnit.mm^2", SI_DERIVED, true);
109         SQUARE_CENTIMETER =
110                 new AreaUnit(LengthUnit.CENTIMETER, "AreaUnit.square_centimeter", "AreaUnit.cm^2", SI_DERIVED, true);
111         SQUARE_DECIMETER = new AreaUnit(LengthUnit.DECIMETER, "AreaUnit.square_decimeter", "AreaUnit.dm^2", SI_DERIVED, true);
112         SQUARE_DEKAMETER = new AreaUnit(LengthUnit.DEKAMETER, "AreaUnit.square_dekameter", "AreaUnit.dam^2", SI_DERIVED, true);
113         SQUARE_HECTOMETER =
114                 new AreaUnit(LengthUnit.HECTOMETER, "AreaUnit.square_hectometer", "AreaUnit.hm^2", SI_DERIVED, true);
115         SQUARE_KILOMETER = new AreaUnit(LengthUnit.KILOMETER, "AreaUnit.square_kilometer", "AreaUnit.km^2", SI_DERIVED, true);
116         SQUARE_MEGAMETER = new AreaUnit(LengthUnit.MEGAMETER, "AreaUnit.square_megameter", "AreaUnit.Mm^2", SI_DERIVED, true);
117         CENTIARE = new AreaUnit("AreaUnit.centiare", "AreaUnit.ca", OTHER, SQUARE_METER, 1.0, true);
118         ARE = new AreaUnit("AreaUnit.are", "AreaUnit.a", OTHER, SQUARE_METER, 100.0, true);
119         HECTARE = new AreaUnit("AreaUnit.hectare", "AreaUnit.ha", OTHER, ARE, 100.0, true);
120         SQUARE_NAUTICAL_MILE =
121                 new AreaUnit(LengthUnit.NAUTICAL_MILE, "AreaUnit.square_nautical_mile", "AreaUnit.Nmi^2", OTHER, true);
122         SQUARE_MILE = new AreaUnit(LengthUnit.MILE, "AreaUnit.square_mile", "AreaUnit.mi^2", IMPERIAL, true);
123         SQUARE_FOOT = new AreaUnit(LengthUnit.FOOT, "AreaUnit.square_foot", "AreaUnit.ft^2", IMPERIAL, true);
124         SQUARE_INCH = new AreaUnit(LengthUnit.INCH, "AreaUnit.square_inch", "AreaUnit.in^2", IMPERIAL, true);
125         SQUARE_YARD = new AreaUnit(LengthUnit.YARD, "AreaUnit.square_yard", "AreaUnit.yd^2", IMPERIAL, true);
126         ACRE = new AreaUnit("AreaUnit.acre", "AreaUnit.ac", IMPERIAL, SQUARE_YARD, 4840.0, true);
127     }
128 
129     /**
130      * Define area unit based on length.
131      * @param lengthUnit the unit of length for the area unit, e.g., meter
132      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
133      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
134      *            otherwise the abbreviation itself
135      * @param unitSystem the unit system, e.g. SI or Imperial
136      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
137      */
138     private AreaUnit(final LengthUnit lengthUnit, final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
139             final UnitSystem unitSystem, final boolean standardUnit)
140     {
141         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, SQUARE_METER,
142                 lengthUnit.getScaleFactor() * lengthUnit.getScaleFactor(), standardUnit);
143         this.lengthUnit = lengthUnit;
144     }
145 
146     /**
147      * Create a user-defined area unit based on length.
148      * @param lengthUnit the unit of length for the area unit, e.g., meter
149      * @param name the long name of the unit
150      * @param abbreviation the abbreviation of the unit
151      * @param unitSystem the unit system, e.g. SI or Imperial
152      */
153     public AreaUnit(final LengthUnit lengthUnit, final String name, final String abbreviation, final UnitSystem unitSystem)
154     {
155         this(lengthUnit, name, abbreviation, unitSystem, false);
156     }
157 
158     /**
159      * This constructor constructs a unit out of another defined unit, e.g. an are is 100 m^2.
160      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
161      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
162      *            otherwise the abbreviation itself
163      * @param unitSystem the unit system, e.g. SI or Imperial
164      * @param referenceUnit the unit to convert to
165      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
166      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
167      */
168     private AreaUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
169             final AreaUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
170     {
171         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
172                 standardUnit);
173         this.lengthUnit = referenceUnit.getLengthUnit();
174     }
175 
176     /**
177      * Build a user-defined unit with a conversion factor to another unit, e.g. an are is 100 m^2.
178      * @param name the long name of the unit
179      * @param abbreviation the abbreviation of the unit
180      * @param unitSystem the unit system, e.g. SI or Imperial
181      * @param referenceUnit the unit to convert to
182      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
183      */
184     public AreaUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final AreaUnit referenceUnit,
185             final double scaleFactorToReferenceUnit)
186     {
187         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
188     }
189 
190     /**
191      * @return lengthUnit
192      */
193     public final LengthUnit getLengthUnit()
194     {
195         return this.lengthUnit;
196     }
197 
198     /** {@inheritDoc} */
199     @Override
200     public final AreaUnit getStandardUnit()
201     {
202         return SQUARE_METER;
203     }
204 
205     /** {@inheritDoc} */
206     @Override
207     public final String getSICoefficientsString()
208     {
209         return "m2";
210     }
211 
212 }