View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.AreaUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.FlowVolumeUnit;
8   import org.djunits.unit.ForceUnit;
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.unit.LinearDensityUnit;
11  import org.djunits.unit.LuminousFluxUnit;
12  import org.djunits.unit.VolumeUnit;
13  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
14  import org.djutils.base.NumberParser;
15  import org.djutils.exceptions.Throw;
16  
17  import jakarta.annotation.Generated;
18  
19  /**
20   * Easy access methods for the Area DoubleScalar, which is relative by definition.
21   * <p>
22   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
24   * </p>
25   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
27   */
28  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
29  public class Area extends DoubleScalarRel<AreaUnit, Area>
30  {
31      /** */
32      private static final long serialVersionUID = 20150905L;
33  
34      /** Constant with value zero. */
35      public static final Area ZERO = new Area(0.0, AreaUnit.SI);
36  
37      /** Constant with value one. */
38      public static final Area ONE = new Area(1.0, AreaUnit.SI);
39  
40      /** Constant with value NaN. */
41      @SuppressWarnings("checkstyle:constantname")
42      public static final Area NaN = new Area(Double.NaN, AreaUnit.SI);
43  
44      /** Constant with value POSITIVE_INFINITY. */
45      public static final Area POSITIVE_INFINITY = new Area(Double.POSITIVE_INFINITY, AreaUnit.SI);
46  
47      /** Constant with value NEGATIVE_INFINITY. */
48      public static final Area NEGATIVE_INFINITY = new Area(Double.NEGATIVE_INFINITY, AreaUnit.SI);
49  
50      /** Constant with value MAX_VALUE. */
51      public static final Area POS_MAXVALUE = new Area(Double.MAX_VALUE, AreaUnit.SI);
52  
53      /** Constant with value -MAX_VALUE. */
54      public static final Area NEG_MAXVALUE = new Area(-Double.MAX_VALUE, AreaUnit.SI);
55  
56      /**
57       * Construct Area scalar with a unit.
58       * @param value the double value, expressed in the given unit
59       * @param unit unit for the double value
60       */
61      public Area(final double value, final AreaUnit unit)
62      {
63          super(value, unit);
64      }
65  
66      /**
67       * Construct Area scalar.
68       * @param value Scalar from which to construct this instance
69       */
70      public Area(final Area value)
71      {
72          super(value);
73      }
74  
75      @Override
76      public final Area instantiateRel(final double value, final AreaUnit unit)
77      {
78          return new Area(value, unit);
79      }
80  
81      /**
82       * Construct Area scalar based on an SI value.
83       * @param value the double value in SI units
84       * @return the new scalar with the SI value
85       */
86      public static final Area ofSI(final double value)
87      {
88          return new Area(value, AreaUnit.SI);
89      }
90  
91      /**
92       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
93       * @param zero the value at a ratio of zero
94       * @param one the value at a ratio of one
95       * @param ratio the ratio between 0 and 1, inclusive
96       * @return a Area at the given ratio between 0 and 1
97       */
98      public static Area interpolate(final Area zero, final Area one, final double ratio)
99      {
100         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
101                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
102         return new Area(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
103     }
104 
105     /**
106      * Return the maximum value of two relative scalars.
107      * @param r1 the first scalar
108      * @param r2 the second scalar
109      * @return the maximum value of two relative scalars
110      */
111     public static Area max(final Area r1, final Area r2)
112     {
113         return r1.gt(r2) ? r1 : r2;
114     }
115 
116     /**
117      * Return the maximum value of more than two relative scalars.
118      * @param r1 the first scalar
119      * @param r2 the second scalar
120      * @param rn the other scalars
121      * @return the maximum value of more than two relative scalars
122      */
123     public static Area max(final Area r1, final Area r2, final Area... rn)
124     {
125         Area maxr = r1.gt(r2) ? r1 : r2;
126         for (Area r : rn)
127         {
128             if (r.gt(maxr))
129             {
130                 maxr = r;
131             }
132         }
133         return maxr;
134     }
135 
136     /**
137      * Return the minimum value of two relative scalars.
138      * @param r1 the first scalar
139      * @param r2 the second scalar
140      * @return the minimum value of two relative scalars
141      */
142     public static Area min(final Area r1, final Area r2)
143     {
144         return r1.lt(r2) ? r1 : r2;
145     }
146 
147     /**
148      * Return the minimum value of more than two relative scalars.
149      * @param r1 the first scalar
150      * @param r2 the second scalar
151      * @param rn the other scalars
152      * @return the minimum value of more than two relative scalars
153      */
154     public static Area min(final Area r1, final Area r2, final Area... rn)
155     {
156         Area minr = r1.lt(r2) ? r1 : r2;
157         for (Area r : rn)
158         {
159             if (r.lt(minr))
160             {
161                 minr = r;
162             }
163         }
164         return minr;
165     }
166 
167     /**
168      * Returns a Area representation of a textual representation of a value with a unit. The String representation that can be
169      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
170      * but not required, between the value and the unit.
171      * @param text the textual representation to parse into a Area
172      * @return the Scalar representation of the value in its unit
173      * @throws IllegalArgumentException when the text cannot be parsed
174      * @throws NullPointerException when the text argument is null
175      */
176     public static Area valueOf(final String text)
177     {
178         Throw.whenNull(text, "Error parsing Area: text to parse is null");
179         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Area: empty text to parse");
180         try
181         {
182             NumberParser numberParser = new NumberParser().lenient().trailing();
183             double d = numberParser.parseDouble(text);
184             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
185             AreaUnit unit = AreaUnit.BASE.getUnitByAbbreviation(unitString);
186             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Area", unitString);
187             return new Area(d, unit);
188         }
189         catch (Exception exception)
190         {
191             throw new IllegalArgumentException(
192                     "Error parsing Area from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
193                     exception);
194         }
195     }
196 
197     /**
198      * Returns a Area based on a value and the textual representation of the unit, which can be localized.
199      * @param value the value to use
200      * @param unitString the textual representation of the unit
201      * @return the Scalar representation of the value in its unit
202      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
203      * @throws NullPointerException when the unitString argument is null
204      */
205     public static Area of(final double value, final String unitString)
206     {
207         Throw.whenNull(unitString, "Error parsing Area: unitString is null");
208         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Area: empty unitString");
209         AreaUnit unit = AreaUnit.BASE.getUnitByAbbreviation(unitString);
210         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Area with unit %s", unitString);
211         return new Area(value, unit);
212     }
213 
214     /**
215      * Calculate the division of Area and Area, which results in a Dimensionless scalar.
216      * @param v scalar
217      * @return scalar as a division of Area and Area
218      */
219     public final Dimensionless divide(final Area v)
220     {
221         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
222     }
223 
224     /**
225      * Calculate the multiplication of Area and Length, which results in a Volume scalar.
226      * @param v scalar
227      * @return scalar as a multiplication of Area and Length
228      */
229     public final Volume times(final Length v)
230     {
231         return new Volume(this.si * v.si, VolumeUnit.SI);
232     }
233 
234     /**
235      * Calculate the division of Area and LinearDensity, which results in a Volume scalar.
236      * @param v scalar
237      * @return scalar as a division of Area and LinearDensity
238      */
239     public final Volume divide(final LinearDensity v)
240     {
241         return new Volume(this.si / v.si, VolumeUnit.SI);
242     }
243 
244     /**
245      * Calculate the division of Area and Volume, which results in a LinearDensity scalar.
246      * @param v scalar
247      * @return scalar as a division of Area and Volume
248      */
249     public final LinearDensity divide(final Volume v)
250     {
251         return new LinearDensity(this.si / v.si, LinearDensityUnit.SI);
252     }
253 
254     /**
255      * Calculate the division of Area and Length, which results in a Length scalar.
256      * @param v scalar
257      * @return scalar as a division of Area and Length
258      */
259     public final Length divide(final Length v)
260     {
261         return new Length(this.si / v.si, LengthUnit.SI);
262     }
263 
264     /**
265      * Calculate the multiplication of Area and LinearDensity, which results in a Length scalar.
266      * @param v scalar
267      * @return scalar as a multiplication of Area and LinearDensity
268      */
269     public final Length times(final LinearDensity v)
270     {
271         return new Length(this.si * v.si, LengthUnit.SI);
272     }
273 
274     /**
275      * Calculate the multiplication of Area and Speed, which results in a FlowVolume scalar.
276      * @param v scalar
277      * @return scalar as a multiplication of Area and Speed
278      */
279     public final FlowVolume times(final Speed v)
280     {
281         return new FlowVolume(this.si * v.si, FlowVolumeUnit.SI);
282     }
283 
284     /**
285      * Calculate the multiplication of Area and Pressure, which results in a Force scalar.
286      * @param v scalar
287      * @return scalar as a multiplication of Area and Pressure
288      */
289     public final Force times(final Pressure v)
290     {
291         return new Force(this.si * v.si, ForceUnit.SI);
292     }
293 
294     /**
295      * Calculate the multiplication of Area and Illuminance, which results in a LuminousFlux scalar.
296      * @param v scalar
297      * @return scalar as a multiplication of Area and Illuminance
298      */
299     public final LuminousFlux times(final Illuminance v)
300     {
301         return new LuminousFlux(this.si * v.si, LuminousFluxUnit.SI);
302     }
303 
304     @Override
305     public SIScalar reciprocal()
306     {
307         return SIScalar.divide(Dimensionless.ONE, this);
308     }
309 
310     /**
311      * Multiply two scalars that result in a scalar of type Area.
312      * @param scalar1 the first scalar
313      * @param scalar2 the second scalar
314      * @return the multiplication of both scalars as an instance of Area
315      */
316     public static Area multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
317     {
318         Throw.whenNull(scalar1, "scalar1 cannot be null");
319         Throw.whenNull(scalar2, "scalar2 cannot be null");
320         Throw.when(
321                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
322                         .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AreaUnit.BASE.getSiDimensions()),
323                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Area",
324                 scalar1.toDisplayString(), scalar2.toDisplayString());
325         return new Area(scalar1.si * scalar2.si, AreaUnit.SI);
326     }
327 
328     /**
329      * Divide two scalars that result in a scalar of type Area.
330      * @param scalar1 the first scalar
331      * @param scalar2 the second scalar
332      * @return the division of scalar1 by scalar2 as an instance of Area
333      */
334     public static Area divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
335     {
336         Throw.whenNull(scalar1, "scalar1 cannot be null");
337         Throw.whenNull(scalar2, "scalar2 cannot be null");
338         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
339                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AreaUnit.BASE.getSiDimensions()),
340                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Area",
341                 scalar1.toDisplayString(), scalar2.toDisplayString());
342         return new Area(scalar1.si / scalar2.si, AreaUnit.SI);
343     }
344 
345 }