View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.AbstractUnit;
5   import org.djunits.unit.UnitInterface;
6   import org.djunits.unit.UnitRuntimeException;
7   import org.djunits.unit.Unitless;
8   import org.djunits.unit.scale.LinearScale;
9   import org.djunits.unit.scale.Scale;
10  import org.djunits.unit.si.SIPrefix;
11  import org.djunits.unit.si.SIUnit;
12  import org.djunits.unit.system.UnitSystem;
13  
14  /**
15   * Area is a measure of a two-dimensional surface, expressed in square meters (m2).
16   * <p>
17   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
19   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
20   * @author Alexander Verbraeck
21   */
22  public class Area extends Quantity<Area>
23  {
24      /** Constant with value zero. */
25      public static final Area ZERO = ofSi(0.0);
26  
27      /** Constant with value one. */
28      public static final Area ONE = ofSi(1.0);
29  
30      /** Constant with value NaN. */
31      @SuppressWarnings("checkstyle:constantname")
32      public static final Area NaN = ofSi(Double.NaN);
33  
34      /** Constant with value POSITIVE_INFINITY. */
35      public static final Area POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
36  
37      /** Constant with value NEGATIVE_INFINITY. */
38      public static final Area NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
39  
40      /** Constant with value MAX_VALUE. */
41      public static final Area POS_MAXVALUE = ofSi(Double.MAX_VALUE);
42  
43      /** Constant with value -MAX_VALUE. */
44      public static final Area NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
45  
46      /** */
47      private static final long serialVersionUID = 600L;
48  
49      /**
50       * Instantiate a Area quantity with an SI or base value and a display unit.
51       * @param value the quantity value expressed in the SI or base unit
52       * @param displayUnit the display unit to use
53       * @param useSi use SI value when true, use value in unit when false
54       */
55      public Area(final double value, final Area.Unit displayUnit, final boolean useSi)
56      {
57          super(value, displayUnit, useSi);
58      }
59  
60      /**
61       * Instantiate a Area quantity expressed in the given unit.
62       * @param valueInUnit the quantity value expressed in the given unit
63       * @param unit the unit of the value, also acts as the display unit
64       */
65      public Area(final double valueInUnit, final Area.Unit unit)
66      {
67          this(valueInUnit, unit, false);
68      }
69  
70      /**
71       * Return a Area instance based on an SI value.
72       * @param si the si value
73       * @return the Area instance based on an SI value
74       */
75      public static Area ofSi(final double si)
76      {
77          return new Area(si, Area.Unit.SI, true);
78      }
79  
80      /**
81       * Instantiate a Area quantity with an SI or base value and a display unit.
82       * @param siValue the quantity value expressed in the SI or base unit
83       * @param displayUnit the display unit to use
84       * @return the Area instance based on an SI value with the given display unit
85       */
86      public static Area ofSi(final double siValue, final Area.Unit displayUnit)
87      {
88          return new Area(siValue, displayUnit, true);
89      }
90  
91      @Override
92      public Area instantiateSi(final double siValue, final UnitInterface<Area> displayUnit)
93      {
94          return new Area(siValue, (Area.Unit) displayUnit, true);
95      }
96  
97      /**
98       * Returns a Area representation of a textual representation of a value with a unit. The String representation that can be
99       * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
100      * but not required, between the value and the unit.
101      * @param text the textual representation to parse into a Area
102      * @return the Scalar representation of the value in its unit
103      * @throws IllegalArgumentException when the text cannot be parsed
104      * @throws NullPointerException when the text argument is null
105      */
106     public static Area valueOf(final String text)
107     {
108         return Quantity.valueOf(text, ZERO);
109     }
110 
111     /**
112      * Returns a Area based on a value expressed in the unit.
113      * @param valueInUnit the value, expressed in the given unit
114      * @param unit the unit of the value, also acts as the display unit
115      * @return ab Area representation of the value in its unit
116      */
117     public static Area of(final double valueInUnit, final Area.Unit unit)
118     {
119         return new Area(valueInUnit, unit);
120     }
121 
122     /**
123      * Returns a Area based on a value and the textual representation of the unit, which can be localized.
124      * @param valueInUnit the value, expressed in the unit as given by unitString
125      * @param unitString the textual representation of the unit
126      * @return the Scalar representation of the value in its unit
127      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
128      * @throws NullPointerException when the unitString argument is null
129      */
130     public static Area of(final double valueInUnit, final String unitString)
131     {
132         return Quantity.of(valueInUnit, unitString, ZERO);
133     }
134 
135     @Override
136     public Area.Unit getDisplayUnit()
137     {
138         return (Area.Unit) super.getDisplayUnit();
139     }
140 
141     /**
142      * Calculate the division of Area and Area, which results in a Dimensionless scalar.
143      * @param v scalar
144      * @return scalar as a division of Area and Area
145      */
146     public Dimensionless divide(final Area v)
147     {
148         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
149     }
150 
151     /**
152      * Calculate the multiplication of Area and ArealObjectDensity, which results in a Dimensionless scalar.
153      * @param v scalar
154      * @return scalar as a multiplication of Area and ArealObjectDensity
155      */
156     public Dimensionless multiply(final ArealObjectDensity v)
157     {
158         return new Dimensionless(this.si() * v.si(), Unitless.BASE);
159     }
160 
161     /**
162      * Calculate the multiplication of Area and Length, which results in a Volume scalar.
163      * @param v scalar
164      * @return scalar as a multiplication of Area and Length
165      */
166     public Volume multiply(final Length v)
167     {
168         return new Volume(this.si() * v.si(), Volume.Unit.SI);
169     }
170 
171     /**
172      * Calculate the division of Area and LinearObjectDensity, which results in a Volume scalar.
173      * @param v scalar
174      * @return scalar as a division of Area and LinearObjectDensity
175      */
176     public Volume divide(final LinearObjectDensity v)
177     {
178         return new Volume(this.si() / v.si(), Volume.Unit.SI);
179     }
180 
181     /**
182      * Calculate the division of Area and Volume, which results in a LinearObjectDensity scalar.
183      * @param v scalar
184      * @return scalar as a division of Area and Volume
185      */
186     public LinearObjectDensity divide(final Volume v)
187     {
188         return new LinearObjectDensity(this.si() / v.si(), LinearObjectDensity.Unit.SI);
189     }
190 
191     /**
192      * Calculate the division of Area and Length, which results in a Length scalar.
193      * @param v scalar
194      * @return scalar as a division of Area and Length
195      */
196     public Length divide(final Length v)
197     {
198         return new Length(this.si() / v.si(), Length.Unit.SI);
199     }
200 
201     /**
202      * Calculate the multiplication of Area and LinearObjectDensity, which results in a Length scalar.
203      * @param v scalar
204      * @return scalar as a multiplication of Area and LinearObjectDensity
205      */
206     public Length multiply(final LinearObjectDensity v)
207     {
208         return new Length(this.si() * v.si(), Length.Unit.SI);
209     }
210 
211     /**
212      * Calculate the multiplication of Area and Speed, which results in a FlowVolume scalar.
213      * @param v scalar
214      * @return scalar as a multiplication of Area and Speed
215      */
216     public FlowVolume multiply(final Speed v)
217     {
218         return new FlowVolume(this.si() * v.si(), FlowVolume.Unit.SI);
219     }
220 
221     /**
222      * Calculate the multiplication of Area and Pressure, which results in a Force scalar.
223      * @param v scalar
224      * @return scalar as a multiplication of Area and Pressure
225      */
226     public Force multiply(final Pressure v)
227     {
228         return new Force(this.si() * v.si(), Force.Unit.SI);
229     }
230 
231     /**
232      * Calculate the multiplication of Area and Illuminance, which results in a LuminousFlux scalar.
233      * @param v scalar
234      * @return scalar as a multiplication of Area and Illuminance
235      */
236     public LuminousFlux multiply(final Illuminance v)
237     {
238         return new LuminousFlux(this.si() * v.si(), LuminousFlux.Unit.SI);
239     }
240 
241     @Override
242     public ArealObjectDensity reciprocal()
243     {
244         return ArealObjectDensity.ofSi(1.0 / this.si());
245     }
246 
247     /******************************************************************************************************/
248     /********************************************** UNIT CLASS ********************************************/
249     /******************************************************************************************************/
250 
251     /**
252      * Area.Unit encodes the area unit (length x length).
253      * <p>
254      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
255      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
256      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
257      * @author Alexander Verbraeck
258      */
259     @SuppressWarnings("checkstyle:constantname")
260     public static class Unit extends AbstractUnit<Area>
261     {
262         /** The dimensions of Area: m2. */
263         public static final SIUnit SI_UNIT = SIUnit.of("m2");
264 
265         /** Square meter. */
266         public static final Area.Unit m2 = new Area.Unit("m2", "square meter", 1.0, UnitSystem.SI_BASE);
267 
268         /** The SI or BASE unit. */
269         public static final Area.Unit SI = m2;
270 
271         /** Square kilometer. */
272         public static final Area.Unit km2 = m2.deriveUnit("km2", "square kilometer", 1.0E6, UnitSystem.SI_BASE);
273 
274         /** Square hectometer. */
275         public static final Area.Unit hm2 = m2.deriveUnit("hm2", "square hectometer", 1.0E4, UnitSystem.SI_BASE);
276 
277         /** Square decameter. */
278         public static final Area.Unit dam2 = m2.deriveUnit("dam2", "square decameter", 1.0E2, UnitSystem.SI_BASE);
279 
280         /** Square decimeter. */
281         public static final Area.Unit dm2 = m2.deriveUnit("dm2", "square decimeter", 1.0E-2, UnitSystem.SI_BASE);
282 
283         /** Square centimeter. */
284         public static final Area.Unit cm2 = m2.deriveUnit("cm2", "square centimeter", 1.0E-4, UnitSystem.SI_BASE);
285 
286         /** Square millimeter. */
287         public static final Area.Unit mm2 = m2.deriveUnit("mm2", "square millimeter", 1.0E-6, UnitSystem.SI_BASE);
288 
289         /** Square micrometer. */
290         public static final Area.Unit mum2 =
291                 m2.deriveUnit("mum2", "\u03BCm2", "square micrometer", 1.0E-12, UnitSystem.SI_BASE, null);
292 
293         /** Square nanometer. */
294         public static final Area.Unit nm2 = m2.deriveUnit("nm2", "square nanometer", 1.0E-18, UnitSystem.SI_BASE);
295 
296         /** Square picometer. */
297         public static final Area.Unit pm2 = m2.deriveUnit("pm2", "square picometer", 1.0E-24, UnitSystem.SI_BASE);
298 
299         /** Square femtometer. */
300         public static final Area.Unit fm2 = m2.deriveUnit("fm2", "square femtometer", 1.0E-30, UnitSystem.SI_BASE);
301 
302         /** Square attometer. */
303         public static final Area.Unit am2 = m2.deriveUnit("am2", "square attometer", 1.0E-36, UnitSystem.SI_BASE);
304 
305         /** centiare. */
306         public static final Area.Unit ca = new Area.Unit("ca", "centiare", 1.0, UnitSystem.OTHER);
307 
308         /** are. */
309         public static final Area.Unit a = new Area.Unit("a", "are", 100.0, UnitSystem.OTHER);
310 
311         /** hectare. */
312         public static final Area.Unit ha = new Area.Unit("ha", "hectare", 100.0 * 100.0, UnitSystem.OTHER);
313 
314         /** mile2. */
315         public static final Area.Unit mi2 =
316                 new Area.Unit("mi2", "square mile", Length.Unit.CONST_MI * Length.Unit.CONST_MI, UnitSystem.IMPERIAL);
317 
318         /** Nautical mile2. */
319         public static final Area.Unit NM2 =
320                 new Area.Unit("NM2", "square nautical mile", Length.Unit.CONST_NM * Length.Unit.CONST_NM, UnitSystem.OTHER);
321 
322         /** ft2. */
323         public static final Area.Unit ft2 =
324                 new Area.Unit("ft2", "square foot", Length.Unit.CONST_FT * Length.Unit.CONST_FT, UnitSystem.IMPERIAL);
325 
326         /** in2. */
327         public static final Area.Unit in2 =
328                 new Area.Unit("in2", "square inch", Length.Unit.CONST_IN * Length.Unit.CONST_IN, UnitSystem.IMPERIAL);
329 
330         /** yd2. */
331         public static final Area.Unit yd2 =
332                 new Area.Unit("yd2", "square yard", Length.Unit.CONST_YD * Length.Unit.CONST_YD, UnitSystem.IMPERIAL);
333 
334         /** acre (international) defined as 1/640 square mile or 4840 square yards. */
335         public static final Area.Unit ac =
336                 new Area.Unit("ac", "acre", Length.Unit.CONST_MI * Length.Unit.CONST_MI / 640.0, UnitSystem.IMPERIAL);
337 
338         /**
339          * Create a new Area unit.
340          * @param id the id or main abbreviation of the unit
341          * @param name the full name of the unit
342          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
343          * @param unitSystem the unit system such as SI or IMPERIAL
344          */
345         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
346         {
347             super(id, name, scaleFactorToBaseUnit, unitSystem);
348         }
349 
350         /**
351          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
352          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
353          * @param displayAbbreviation the display abbreviation of the unit
354          * @param name the full name of the unit
355          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
356          * @param unitSystem unit system, e.g. SI or Imperial
357          * @param siPrefix the SI Prefix of this unit
358          */
359         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
360                 final UnitSystem unitSystem, final SIPrefix siPrefix)
361         {
362             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
363         }
364 
365         @Override
366         public SIUnit siUnit()
367         {
368             return SI_UNIT;
369         }
370 
371         @Override
372         public Unit getBaseUnit()
373         {
374             return SI;
375         }
376 
377         @Override
378         public Area ofSi(final double si, final UnitInterface<Area> displayUnit)
379         {
380             return new Area(si, (Unit) displayUnit, true);
381         }
382 
383         @Override
384         public Area.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
385                 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
386         {
387             if (getScale() instanceof LinearScale ls)
388             {
389                 return new Area.Unit(textualAbbreviation, displayAbbreviation, name,
390                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
391             }
392             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
393         }
394 
395         @Override
396         public Area.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
397                 final UnitSystem unitSystem)
398         {
399             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
400         }
401 
402     }
403 
404 }