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