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   * Volumetric object density counts the number of objects per unit of volume, measured in number per cubic meter (/m3).
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 VolumetricObjectDensity extends Quantity<VolumetricObjectDensity>
23  {
24      /** Constant with value zero. */
25      public static final VolumetricObjectDensity ZERO = ofSi(0.0);
26  
27      /** Constant with value one. */
28      public static final VolumetricObjectDensity ONE = ofSi(1.0);
29  
30      /** Constant with value NaN. */
31      @SuppressWarnings("checkstyle:constantname")
32      public static final VolumetricObjectDensity NaN = ofSi(Double.NaN);
33  
34      /** Constant with value POSITIVE_INFINITY. */
35      public static final VolumetricObjectDensity POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
36  
37      /** Constant with value NEGATIVE_INFINITY. */
38      public static final VolumetricObjectDensity NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
39  
40      /** Constant with value MAX_VALUE. */
41      public static final VolumetricObjectDensity POS_MAXVALUE = ofSi(Double.MAX_VALUE);
42  
43      /** Constant with value -MAX_VALUE. */
44      public static final VolumetricObjectDensity NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
45  
46      /** */
47      private static final long serialVersionUID = 600L;
48  
49      /**
50       * Instantiate a VolumetricObjectDensity 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 VolumetricObjectDensity(final double value, final VolumetricObjectDensity.Unit displayUnit, final boolean useSi)
56      {
57          super(value, displayUnit, useSi);
58      }
59  
60      /**
61       * Instantiate a VolumetricObjectDensity 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 VolumetricObjectDensity(final double valueInUnit, final VolumetricObjectDensity.Unit unit)
66      {
67          this(valueInUnit, unit, false);
68      }
69  
70      /**
71       * Return a VolumetricObjectDensity instance based on an SI value.
72       * @param si the si value
73       * @return the VolumetricObjectDensity instance based on an SI value
74       */
75      public static VolumetricObjectDensity ofSi(final double si)
76      {
77          return new VolumetricObjectDensity(si, VolumetricObjectDensity.Unit.SI, true);
78      }
79  
80      /**
81       * Instantiate a VolumetricObjectDensity 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 VolumetricObjectDensity instance based on an SI value with the given display unit
85       */
86      public static VolumetricObjectDensity ofSi(final double siValue, final VolumetricObjectDensity.Unit displayUnit)
87      {
88          return new VolumetricObjectDensity(siValue, displayUnit, true);
89      }
90  
91      @Override
92      public VolumetricObjectDensity instantiateSi(final double siValue, final UnitInterface<VolumetricObjectDensity> displayUnit)
93      {
94          return new VolumetricObjectDensity(siValue, (VolumetricObjectDensity.Unit) displayUnit, true);
95      }
96  
97      /**
98       * Returns a VolumetricObjectDensity representation of a textual representation of a value with a unit. The String
99       * representation that can be parsed is the double value in the unit, followed by a localized or English abbreviation of the
100      * unit. Spaces are allowed, but not required, between the value and the unit.
101      * @param text the textual representation to parse into a VolumetricObjectDensity
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 VolumetricObjectDensity valueOf(final String text)
107     {
108         return Quantity.valueOf(text, ZERO);
109     }
110 
111     /**
112      * Returns a VolumetricObjectDensity 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 VolumetricObjectDensity representation of the value in its unit
116      */
117     public static VolumetricObjectDensity of(final double valueInUnit, final VolumetricObjectDensity.Unit unit)
118     {
119         return new VolumetricObjectDensity(valueInUnit, unit);
120     }
121 
122     /**
123      * Returns a VolumetricObjectDensity 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 VolumetricObjectDensity of(final double valueInUnit, final String unitString)
131     {
132         return Quantity.of(valueInUnit, unitString, ZERO);
133     }
134 
135     @Override
136     public VolumetricObjectDensity.Unit getDisplayUnit()
137     {
138         return (VolumetricObjectDensity.Unit) super.getDisplayUnit();
139     }
140 
141     /**
142      * Multiplies this volumetric object density by a volume to yield a dimensionless count.
143      * <p>
144      * Formula: (1/m³) x m³ = 1.
145      * @param volume the volume multiplier; must not be {@code null}.
146      * @return the resulting dimensionless count in SI (1).
147      * @throws NullPointerException if {@code volume} is {@code null}.
148      */
149     public Dimensionless multiply(final Volume volume)
150     {
151         return new Dimensionless(this.si() * volume.si(), Unitless.BASE);
152     }
153 
154     /**
155      * Multiplies this volumetric object density by an area to yield a linear object density.
156      * <p>
157      * Formula: (1/m³) x m² = 1/m.
158      * @param area the area multiplier; must not be {@code null}.
159      * @return the resulting linear object density in SI (1/m).
160      * @throws NullPointerException if {@code area} is {@code null}.
161      */
162     public LinearObjectDensity multiply(final Area area)
163     {
164         return new LinearObjectDensity(this.si() * area.si(), LinearObjectDensity.Unit.SI);
165     }
166 
167     /**
168      * Multiplies this volumetric object density by a length to yield an areal object density.
169      * <p>
170      * Formula: (1/m³) x m = 1/m².
171      * @param length the length multiplier; must not be {@code null}.
172      * @return the resulting areal object density in SI (1/m²).
173      * @throws NullPointerException if {@code length} is {@code null}.
174      */
175     public ArealObjectDensity multiply(final Length length)
176     {
177         return new ArealObjectDensity(this.si() * length.si(), ArealObjectDensity.Unit.SI);
178     }
179 
180     /**
181      * Divides this volumetric object density by a linear object density to yield an areal object density.
182      * <p>
183      * Formula: (1/m³) / (1/m) = 1/m².
184      * @param lod the linear object density divisor; must not be {@code null}.
185      * @return the resulting areal object density in SI (1/m²).
186      * @throws NullPointerException if {@code lod} is {@code null}.
187      */
188     public ArealObjectDensity divide(final LinearObjectDensity lod)
189     {
190         return new ArealObjectDensity(this.si() / lod.si(), ArealObjectDensity.Unit.SI);
191     }
192 
193     /**
194      * Divides this volumetric object density by an areal object density to yield a linear object density.
195      * <p>
196      * Formula: (1/m³) / (1/m²) = 1/m.
197      * @param aod the areal object density divisor; must not be {@code null}.
198      * @return the resulting linear object density in SI (1/m).
199      * @throws NullPointerException if {@code aod} is {@code null}.
200      */
201     public LinearObjectDensity divide(final ArealObjectDensity aod)
202     {
203         return new LinearObjectDensity(this.si() / aod.si(), LinearObjectDensity.Unit.SI);
204     }
205 
206     /**
207      * Divides this volumetric object density by another volumetric object density to yield a dimensionless ratio.
208      * <p>
209      * Formula: (1/m³) / (1/m³) = 1.
210      * @param other the volumetric object density divisor; must not be {@code null}.
211      * @return the resulting dimensionless ratio in SI (1).
212      * @throws NullPointerException if {@code other} is {@code null}.
213      */
214     public Dimensionless divide(final VolumetricObjectDensity other)
215     {
216         return new Dimensionless(this.si() / other.si(), Unitless.BASE);
217     }
218 
219     @Override
220     public Volume reciprocal()
221     {
222         return Volume.ofSi(1.0 / this.si());
223     }
224 
225     /******************************************************************************************************/
226     /********************************************** UNIT CLASS ********************************************/
227     /******************************************************************************************************/
228 
229     /**
230      * VolumetricObjectDensity.Unit encodes the unit for number of objects per unit of volume.
231      * <p>
232      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
233      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
234      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
235      * @author Alexander Verbraeck
236      */
237     @SuppressWarnings("checkstyle:constantname")
238     public static class Unit extends AbstractUnit<VolumetricObjectDensity>
239     {
240         /** The dimensions of the number of objects per unit of volume: per cubic meter (/m3). */
241         public static final SIUnit SI_UNIT = SIUnit.of("/m3");
242 
243         /** per cubic meter. */
244         public static final VolumetricObjectDensity.Unit per_m3 =
245                 new VolumetricObjectDensity.Unit("/m3", "per cubic meter", 1.0, UnitSystem.SI_DERIVED);
246 
247         /** The SI or BASE unit. */
248         public static final VolumetricObjectDensity.Unit SI = per_m3;
249 
250         /**
251          * Create a new VolumetricObjectDensity unit.
252          * @param id the id or main abbreviation of the unit
253          * @param name the full name of the unit
254          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
255          * @param unitSystem the unit system such as SI or IMPERIAL
256          */
257         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
258         {
259             super(id, name, scaleFactorToBaseUnit, unitSystem);
260         }
261 
262         /**
263          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
264          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
265          * @param displayAbbreviation the display abbreviation of the unit
266          * @param name the full name of the unit
267          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
268          * @param unitSystem unit system, e.g. SI or Imperial
269          * @param siPrefix the SI Prefix of this unit
270          */
271         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
272                 final UnitSystem unitSystem, final SIPrefix siPrefix)
273         {
274             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
275         }
276 
277         @Override
278         public SIUnit siUnit()
279         {
280             return SI_UNIT;
281         }
282 
283         @Override
284         public Unit getBaseUnit()
285         {
286             return SI;
287         }
288 
289         @Override
290         public VolumetricObjectDensity ofSi(final double si, final UnitInterface<VolumetricObjectDensity> displayUnit)
291         {
292             return new VolumetricObjectDensity(si, (Unit) displayUnit, true);
293         }
294 
295         @Override
296         public VolumetricObjectDensity.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation,
297                 final String name, final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
298         {
299             if (getScale() instanceof LinearScale ls)
300             {
301                 return new VolumetricObjectDensity.Unit(textualAbbreviation, displayAbbreviation, name,
302                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
303             }
304             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
305         }
306 
307     }
308 
309 }