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