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