View Javadoc
1   package org.djunits.value.vdouble.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.vdouble.scalar.base.DoubleScalarRel;
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 Density DoubleScalar, 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 Density extends DoubleScalarRel<DensityUnit, Density>
26  {
27      /** */
28      private static final long serialVersionUID = 20150905L;
29  
30      /** Constant with value zero. */
31      public static final Density ZERO = new Density(0.0, DensityUnit.SI);
32  
33      /** Constant with value one. */
34      public static final Density ONE = new Density(1.0, DensityUnit.SI);
35  
36      /** Constant with value NaN. */
37      @SuppressWarnings("checkstyle:constantname")
38      public static final Density NaN = new Density(Double.NaN, DensityUnit.SI);
39  
40      /** Constant with value POSITIVE_INFINITY. */
41      public static final Density POSITIVE_INFINITY = new Density(Double.POSITIVE_INFINITY, DensityUnit.SI);
42  
43      /** Constant with value NEGATIVE_INFINITY. */
44      public static final Density NEGATIVE_INFINITY = new Density(Double.NEGATIVE_INFINITY, DensityUnit.SI);
45  
46      /** Constant with value MAX_VALUE. */
47      public static final Density POS_MAXVALUE = new Density(Double.MAX_VALUE, DensityUnit.SI);
48  
49      /** Constant with value -MAX_VALUE. */
50      public static final Density NEG_MAXVALUE = new Density(-Double.MAX_VALUE, DensityUnit.SI);
51  
52      /**
53       * Construct Density scalar with a unit.
54       * @param value the double value, expressed in the given unit
55       * @param unit unit for the double value
56       */
57      public Density(final double value, final DensityUnit unit)
58      {
59          super(value, unit);
60      }
61  
62      /**
63       * Construct Density scalar.
64       * @param value Scalar from which to construct this instance
65       */
66      public Density(final Density value)
67      {
68          super(value);
69      }
70  
71      @Override
72      public final Density instantiateRel(final double value, final DensityUnit unit)
73      {
74          return new Density(value, unit);
75      }
76  
77      /**
78       * Construct Density scalar based on an SI value.
79       * @param value the double value in SI units
80       * @return the new scalar with the SI value
81       */
82      public static final Density ofSI(final double value)
83      {
84          return new Density(value, DensityUnit.SI);
85      }
86  
87      /**
88       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
89       * @param zero the value at a ratio of zero
90       * @param one the value at a ratio of one
91       * @param ratio the ratio between 0 and 1, inclusive
92       * @return a Density at the given ratio between 0 and 1
93       */
94      public static Density interpolate(final Density zero, final Density one, final double ratio)
95      {
96          Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
97                  "ratio for interpolation should be between 0 and 1, but is %f", ratio);
98          return new Density(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
99                  zero.getDisplayUnit());
100     }
101 
102     /**
103      * Return the maximum value of two relative scalars.
104      * @param r1 the first scalar
105      * @param r2 the second scalar
106      * @return the maximum value of two relative scalars
107      */
108     public static Density max(final Density r1, final Density r2)
109     {
110         return r1.gt(r2) ? r1 : r2;
111     }
112 
113     /**
114      * Return the maximum value of more than two relative scalars.
115      * @param r1 the first scalar
116      * @param r2 the second scalar
117      * @param rn the other scalars
118      * @return the maximum value of more than two relative scalars
119      */
120     public static Density max(final Density r1, final Density r2, final Density... rn)
121     {
122         Density maxr = r1.gt(r2) ? r1 : r2;
123         for (Density r : rn)
124         {
125             if (r.gt(maxr))
126             {
127                 maxr = r;
128             }
129         }
130         return maxr;
131     }
132 
133     /**
134      * Return the minimum value of two relative scalars.
135      * @param r1 the first scalar
136      * @param r2 the second scalar
137      * @return the minimum value of two relative scalars
138      */
139     public static Density min(final Density r1, final Density r2)
140     {
141         return r1.lt(r2) ? r1 : r2;
142     }
143 
144     /**
145      * Return the minimum value of more than two relative scalars.
146      * @param r1 the first scalar
147      * @param r2 the second scalar
148      * @param rn the other scalars
149      * @return the minimum value of more than two relative scalars
150      */
151     public static Density min(final Density r1, final Density r2, final Density... rn)
152     {
153         Density minr = r1.lt(r2) ? r1 : r2;
154         for (Density r : rn)
155         {
156             if (r.lt(minr))
157             {
158                 minr = r;
159             }
160         }
161         return minr;
162     }
163 
164     /**
165      * Returns a Density representation of a textual representation of a value with a unit. The String representation that can
166      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
167      * allowed, but not required, between the value and the unit.
168      * @param text the textual representation to parse into a Density
169      * @return the Scalar representation of the value in its unit
170      * @throws IllegalArgumentException when the text cannot be parsed
171      * @throws NullPointerException when the text argument is null
172      */
173     public static Density valueOf(final String text)
174     {
175         Throw.whenNull(text, "Error parsing Density: text to parse is null");
176         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Density: empty text to parse");
177         try
178         {
179             NumberParser numberParser = new NumberParser().lenient().trailing();
180             double d = numberParser.parseDouble(text);
181             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
182             DensityUnit unit = DensityUnit.BASE.getUnitByAbbreviation(unitString);
183             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Density", unitString);
184             return new Density(d, unit);
185         }
186         catch (Exception exception)
187         {
188             throw new IllegalArgumentException(
189                     "Error parsing Density from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
190                     exception);
191         }
192     }
193 
194     /**
195      * Returns a Density based on a value and the textual representation of the unit, which can be localized.
196      * @param value the value to use
197      * @param unitString the textual representation of the unit
198      * @return the Scalar representation of the value in its unit
199      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
200      * @throws NullPointerException when the unitString argument is null
201      */
202     public static Density of(final double value, final String unitString)
203     {
204         Throw.whenNull(unitString, "Error parsing Density: unitString is null");
205         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Density: empty unitString");
206         DensityUnit unit = DensityUnit.BASE.getUnitByAbbreviation(unitString);
207         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Density with unit %s", unitString);
208         return new Density(value, unit);
209     }
210 
211     /**
212      * Calculate the division of Density and Density, which results in a Dimensionless scalar.
213      * @param v scalar
214      * @return scalar as a division of Density and Density
215      */
216     public final Dimensionless divide(final Density v)
217     {
218         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
219     }
220 
221     /**
222      * Calculate the multiplication of Density and Volume, which results in a Mass scalar.
223      * @param v scalar
224      * @return scalar as a multiplication of Density and Volume
225      */
226     public final Mass times(final Volume v)
227     {
228         return new Mass(this.si * v.si, MassUnit.SI);
229     }
230 
231     /**
232      * Calculate the multiplication of Density and FlowVolume, which results in a FlowMass scalar.
233      * @param v scalar
234      * @return scalar as a multiplication of Density and FlowVolume
235      */
236     public final FlowMass times(final FlowVolume v)
237     {
238         return new FlowMass(this.si * v.si, FlowMassUnit.SI);
239     }
240 
241     @Override
242     public SIScalar reciprocal()
243     {
244         return SIScalar.divide(Dimensionless.ONE, this);
245     }
246 
247     /**
248      * Multiply two scalars that result in a scalar of type Density.
249      * @param scalar1 the first scalar
250      * @param scalar2 the second scalar
251      * @return the multiplication of both scalars as an instance of Density
252      */
253     public static Density multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
254     {
255         Throw.whenNull(scalar1, "scalar1 cannot be null");
256         Throw.whenNull(scalar2, "scalar2 cannot be null");
257         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
258                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(DensityUnit.BASE.getSiDimensions()),
259                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Density",
260                 scalar1.toDisplayString(), scalar2.toDisplayString());
261         return new Density(scalar1.si * scalar2.si, DensityUnit.SI);
262     }
263 
264     /**
265      * Divide two scalars that result in a scalar of type Density.
266      * @param scalar1 the first scalar
267      * @param scalar2 the second scalar
268      * @return the division of scalar1 by scalar2 as an instance of Density
269      */
270     public static Density divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
271     {
272         Throw.whenNull(scalar1, "scalar1 cannot be null");
273         Throw.whenNull(scalar2, "scalar2 cannot be null");
274         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
275                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(DensityUnit.BASE.getSiDimensions()),
276                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Density",
277                 scalar1.toDisplayString(), scalar2.toDisplayString());
278         return new Density(scalar1.si / scalar2.si, DensityUnit.SI);
279     }
280 
281 }