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-2019 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: 2019-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 324 $, 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 LengthUnit; the unit of length for the area unit, e.g., meter
132      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
133      *            name itself
134      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
135      *            unit, otherwise the abbreviation itself
136      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
137      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
138      *            unit
139      */
140     private AreaUnit(final LengthUnit lengthUnit, final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
141             final UnitSystem unitSystem, final boolean standardUnit)
142     {
143         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, SQUARE_METER,
144                 lengthUnit.getScaleFactor() * lengthUnit.getScaleFactor(), standardUnit);
145         this.lengthUnit = lengthUnit;
146     }
147 
148     /**
149      * Create a user-defined area unit based on length.
150      * @param lengthUnit LengthUnit; the unit of length for the area unit, e.g., meter
151      * @param name String; the long name of the unit
152      * @param abbreviation String; the abbreviation of the unit
153      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
154      */
155     public AreaUnit(final LengthUnit lengthUnit, final String name, final String abbreviation, final UnitSystem unitSystem)
156     {
157         this(lengthUnit, name, abbreviation, unitSystem, false);
158     }
159 
160     /**
161      * This constructor constructs a unit out of another defined unit, e.g. an are is 100 m^2.
162      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
163      *            name itself
164      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
165      *            unit, otherwise the abbreviation itself
166      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
167      * @param referenceUnit AreaUnit; the unit to convert to
168      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
169      *            unit
170      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
171      *            unit
172      */
173     private AreaUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
174             final AreaUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
175     {
176         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
177                 standardUnit);
178         this.lengthUnit = referenceUnit.getLengthUnit();
179     }
180 
181     /**
182      * Build a user-defined unit with a conversion factor to another unit, e.g. an are is 100 m^2.
183      * @param name String; the long name of the unit
184      * @param abbreviation String; the abbreviation of the unit
185      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
186      * @param referenceUnit AreaUnit; the unit to convert to
187      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
188      *            unit
189      */
190     public AreaUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final AreaUnit referenceUnit,
191             final double scaleFactorToReferenceUnit)
192     {
193         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
194     }
195 
196     /**
197      * @return lengthUnit
198      */
199     public final LengthUnit getLengthUnit()
200     {
201         return this.lengthUnit;
202     }
203 
204     /** {@inheritDoc} */
205     @Override
206     public final AreaUnit getStandardUnit()
207     {
208         return SQUARE_METER;
209     }
210 
211     /** {@inheritDoc} */
212     @Override
213     public final String getSICoefficientsString()
214     {
215         return "m2";
216     }
217 
218 }