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   * Volume is the amount of three-dimensional space occupied by matter, measured in cubic meters (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 Volume extends Quantity<Volume>
23  {
24      /** Constant with value zero. */
25      public static final Volume ZERO = ofSi(0.0);
26  
27      /** Constant with value one. */
28      public static final Volume ONE = ofSi(1.0);
29  
30      /** Constant with value NaN. */
31      @SuppressWarnings("checkstyle:constantname")
32      public static final Volume NaN = ofSi(Double.NaN);
33  
34      /** Constant with value POSITIVE_INFINITY. */
35      public static final Volume POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
36  
37      /** Constant with value NEGATIVE_INFINITY. */
38      public static final Volume NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
39  
40      /** Constant with value MAX_VALUE. */
41      public static final Volume POS_MAXVALUE = ofSi(Double.MAX_VALUE);
42  
43      /** Constant with value -MAX_VALUE. */
44      public static final Volume NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
45  
46      /** */
47      private static final long serialVersionUID = 600L;
48  
49      /**
50       * Instantiate a Volume 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 Volume(final double value, final Volume.Unit displayUnit, final boolean useSi)
56      {
57          super(value, displayUnit, useSi);
58      }
59  
60      /**
61       * Instantiate a Volume 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 Volume(final double valueInUnit, final Volume.Unit unit)
66      {
67          this(valueInUnit, unit, false);
68      }
69  
70      /**
71       * Return a Volume instance based on an SI value.
72       * @param si the si value
73       * @return the Volume instance based on an SI value
74       */
75      public static Volume ofSi(final double si)
76      {
77          return new Volume(si, Volume.Unit.SI, true);
78      }
79  
80      /**
81       * Instantiate a Volume 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 Volume instance based on an SI value with the given display unit
85       */
86      public static Volume ofSi(final double siValue, final Volume.Unit displayUnit)
87      {
88          return new Volume(siValue, displayUnit, true);
89      }
90  
91      @Override
92      public Volume instantiateSi(final double siValue, final UnitInterface<Volume> displayUnit)
93      {
94          return new Volume(siValue, (Volume.Unit) displayUnit, true);
95      }
96  
97      /**
98       * Returns a Volume representation of a textual representation of a value with a unit. The String representation that can be
99       * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
100      * but not required, between the value and the unit.
101      * @param text the textual representation to parse into a Volume
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 Volume valueOf(final String text)
107     {
108         return Quantity.valueOf(text, ZERO);
109     }
110 
111     /**
112      * Returns a Volume 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 Volume representation of the value in its unit
116      */
117     public static Volume of(final double valueInUnit, final Volume.Unit unit)
118     {
119         return new Volume(valueInUnit, unit);
120     }
121 
122     /**
123      * Returns a Volume 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 Volume of(final double valueInUnit, final String unitString)
131     {
132         return Quantity.of(valueInUnit, unitString, ZERO);
133     }
134 
135     @Override
136     public Volume.Unit getDisplayUnit()
137     {
138         return (Volume.Unit) super.getDisplayUnit();
139     }
140 
141     /**
142      * Calculate the division of Volume and Volume, which results in a Dimensionless scalar.
143      * @param v scalar
144      * @return scalar as a division of Volume and Volume
145      */
146     public Dimensionless divide(final Volume v)
147     {
148         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
149     }
150 
151     /**
152      * Calculate the multiplication of Volume and Density, which results in a Mass scalar.
153      * @param v scalar
154      * @return scalar as a multiplication of Volume and Density
155      */
156     public Mass multiply(final Density v)
157     {
158         return new Mass(this.si() * v.si(), Mass.Unit.SI);
159     }
160 
161     /**
162      * Calculate the multiplication of Volume and Pressure, which results in a Energy scalar.
163      * @param v scalar
164      * @return scalar as a multiplication of Volume and Pressure
165      */
166     public Energy multiply(final Pressure v)
167     {
168         return new Energy(this.si() * v.si(), Energy.Unit.SI);
169     }
170 
171     /**
172      * Calculate the division of Volume and Length, which results in a Area scalar.
173      * @param v scalar
174      * @return scalar as a division of Volume and Length
175      */
176     public Area divide(final Length v)
177     {
178         return new Area(this.si() / v.si(), Area.Unit.SI);
179     }
180 
181     /**
182      * Calculate the division of Volume and Area, which results in a Length scalar.
183      * @param v scalar
184      * @return scalar as a division of Volume and Area
185      */
186     public Length divide(final Area v)
187     {
188         return new Length(this.si() / v.si(), Length.Unit.SI);
189     }
190 
191     /**
192      * Calculate the multiplication of Volume and LinearObjectDensity, which results in a Area scalar.
193      * @param v scalar
194      * @return scalar as a multiplication of Volume and LinearObjectDensity
195      */
196     public Area multiply(final LinearObjectDensity v)
197     {
198         return new Area(this.si() * v.si(), Area.Unit.SI);
199     }
200 
201     /**
202      * Calculate the division of Volume and Duration, which results in a FlowVolume scalar.
203      * @param v scalar
204      * @return scalar as a division of Volume and Duration
205      */
206     public FlowVolume divide(final Duration v)
207     {
208         return new FlowVolume(this.si() / v.si(), FlowVolume.Unit.SI);
209     }
210 
211     /**
212      * Calculate the division of Volume and FlowVolume, which results in a Duration scalar.
213      * @param v scalar
214      * @return scalar as a division of Volume and FlowVolume
215      */
216     public Duration divide(final FlowVolume v)
217     {
218         return new Duration(this.si() / v.si(), Duration.Unit.SI);
219     }
220 
221     @Override
222     public VolumetricObjectDensity reciprocal()
223     {
224         return VolumetricObjectDensity.ofSi(1.0 / this.si());
225     }
226 
227     /******************************************************************************************************/
228     /********************************************** UNIT CLASS ********************************************/
229     /******************************************************************************************************/
230 
231     /**
232      * Volume.Unit encodes the volume unit (length x length x length).
233      * <p>
234      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
235      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
236      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
237      * @author Alexander Verbraeck
238      */
239     @SuppressWarnings("checkstyle:constantname")
240     public static class Unit extends AbstractUnit<Volume>
241     {
242         /** Constant for the cubic inch. */
243         public static final double CONST_CUBIC_INCH = cubed(Length.Unit.CONST_IN);
244 
245         /** Constant for the cubic foot. */
246         public static final double CONST_CUBIC_FOOT = cubed(Length.Unit.CONST_FT);
247 
248         /** Constant for the cubic yard. */
249         public static final double CONST_CUBIC_YARD = cubed(Length.Unit.CONST_YD);
250 
251         /** Constant for the imperial gallon. */
252         public static final double CONST_GALLON_IMP = 4.54609E-3;
253 
254         /** Constant for imperial fluid ounce. */
255         public static final double CONST_OZ_IMP = CONST_GALLON_IMP / 160.0;
256 
257         /** Constant for US gallon. */
258         public static final double CONST_GALLON_US = 231.0 * CONST_CUBIC_INCH;
259 
260         /** Constant for US fluid ounce. */
261         public static final double CONST_OZ_US = CONST_GALLON_US / 128.0;
262 
263         /** The dimensions of Volume: m3. */
264         public static final SIUnit SI_UNIT = SIUnit.of("m3");
265 
266         /** Cubic meter. */
267         public static final Volume.Unit m3 = new Volume.Unit("m3", "cubic meter", 1.0, UnitSystem.SI_BASE);
268 
269         /** The SI or BASE unit. */
270         public static final Volume.Unit SI = m3;
271 
272         /** mm^3. */
273         public static final Volume.Unit mm3 = m3.deriveUnit("mm3", "cubic millimeter", 1.0E-9, UnitSystem.SI_BASE);
274 
275         /** cm^3. */
276         public static final Volume.Unit cm3 = m3.deriveUnit("cm3", "cubic centimeter", 1.0E-6, UnitSystem.SI_BASE);
277 
278         /** dm^3. */
279         public static final Volume.Unit dm3 = m3.deriveUnit("dm3", "cubic decimeter", 1.0E-3, UnitSystem.SI_BASE);
280 
281         /** dam^3. */
282         public static final Volume.Unit dam3 = m3.deriveUnit("dam3", "cubic decameter", 1.0E3, UnitSystem.SI_BASE);
283 
284         /** hm^3. */
285         public static final Volume.Unit hm3 = m3.deriveUnit("hm3", "cubic hectometer", 1.0E6, UnitSystem.SI_BASE);
286 
287         /** km^3. */
288         public static final Volume.Unit km3 = m3.deriveUnit("km3", "cubic kilometer", 1.0E9, UnitSystem.SI_BASE);
289 
290         /** in^3. */
291         public static final Volume.Unit in3 = m3.deriveUnit("in3", "cubic inch", CONST_CUBIC_INCH, UnitSystem.IMPERIAL);
292 
293         /** ft^3. */
294         public static final Volume.Unit ft3 = m3.deriveUnit("ft3", "cubic foot", CONST_CUBIC_FOOT, UnitSystem.IMPERIAL);
295 
296         /** yd^3. */
297         public static final Volume.Unit yd3 = m3.deriveUnit("yd3", "cubic yard", CONST_CUBIC_YARD, UnitSystem.IMPERIAL);
298 
299         /** mile^3. */
300         public static final Volume.Unit mi3 =
301                 m3.deriveUnit("mi3", "cubic mile", cubed(Length.Unit.CONST_MI), UnitSystem.IMPERIAL);
302 
303         /** Nautical mile^3. */
304         public static final Volume.Unit NM3 =
305                 m3.deriveUnit("NM3", "cubic Nautical Mile", cubed(Length.Unit.CONST_NM), UnitSystem.OTHER);
306 
307         /** liter. */
308         public static final Volume.Unit L = dm3.deriveUnit("L", "liter", 1.0, UnitSystem.SI_ACCEPTED);
309 
310         /** gallon (US), fluids. */
311         public static final Volume.Unit gal_US =
312                 m3.deriveUnit("gal(US)", "gallon (US)", CONST_GALLON_US, UnitSystem.US_CUSTOMARY);
313 
314         /** gallon (imperial). */
315         public static final Volume.Unit gal_imp =
316                 m3.deriveUnit("gal(imp)", "gallon (imp)", CONST_GALLON_IMP, UnitSystem.IMPERIAL);
317 
318         /** quart (fluid US) = 1/4 US gallon. */
319         public static final Volume.Unit qt_US = gal_US.deriveUnit("qt(US)", "quart (US)", 0.25, UnitSystem.US_CUSTOMARY);
320 
321         /** quart (imperial) = 1/4 imp gallon. */
322         public static final Volume.Unit qt_imp = gal_imp.deriveUnit("qt(imp)", "quart (imp)", 0.25, UnitSystem.IMPERIAL);
323 
324         /** pint (fluid US) = 1/2 US quart. */
325         public static final Volume.Unit pt_US = qt_US.deriveUnit("pt(US)", "pint (US)", 0.5, UnitSystem.US_CUSTOMARY);
326 
327         /** pint (imperial) = 1/2 imp quart. */
328         public static final Volume.Unit pt_imp = qt_imp.deriveUnit("pt(imp)", "pint (imp)", 0.5, UnitSystem.IMPERIAL);
329 
330         /** ounce (fluid US) = 1/16 US pint. */
331         public static final Volume.Unit fl_oz_US =
332                 m3.deriveUnit("fl.oz(US)", "fluid ounce (US)", CONST_OZ_US, UnitSystem.US_CUSTOMARY);
333 
334         /** ounce (fluid imperial) = 1/20 imp pint. */
335         public static final Volume.Unit fl_oz_imp =
336                 m3.deriveUnit("fl.oz(imp)", "fluid ounce (imp)", CONST_OZ_IMP, UnitSystem.IMPERIAL);
337 
338         /** Cubic lightyear. */
339         public static final Volume.Unit ly3 =
340                 m3.deriveUnit("ly3", "cubic lightyear", cubed(Length.Unit.CONST_LY), UnitSystem.OTHER);
341 
342         /** Cubic Parsec. */
343         public static final Volume.Unit pc3 =
344                 m3.deriveUnit("pc3", "cubic Parsec", cubed(Length.Unit.CONST_PC), UnitSystem.OTHER);
345 
346         /**
347          * Create a new Volume unit.
348          * @param id the id or main abbreviation of the unit
349          * @param name the full name of the unit
350          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
351          * @param unitSystem the unit system such as SI or IMPERIAL
352          */
353         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
354         {
355             super(id, name, scaleFactorToBaseUnit, unitSystem);
356         }
357 
358         /**
359          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
360          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
361          * @param displayAbbreviation the display abbreviation of the unit
362          * @param name the full name of the unit
363          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
364          * @param unitSystem unit system, e.g. SI or Imperial
365          * @param siPrefix the SI Prefix of this unit
366          */
367         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
368                 final UnitSystem unitSystem, final SIPrefix siPrefix)
369         {
370             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
371         }
372 
373         @Override
374         public SIUnit siUnit()
375         {
376             return SI_UNIT;
377         }
378 
379         @Override
380         public Unit getBaseUnit()
381         {
382             return SI;
383         }
384 
385         @Override
386         public Volume ofSi(final double si, final UnitInterface<Volume> displayUnit)
387         {
388             return new Volume(si, (Unit) displayUnit, true);
389         }
390 
391         @Override
392         public Volume.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
393                 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
394         {
395             if (getScale() instanceof LinearScale ls)
396             {
397                 return new Volume.Unit(textualAbbreviation, displayAbbreviation, name,
398                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
399             }
400             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
401         }
402 
403         @Override
404         public Volume.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
405                 final UnitSystem unitSystem)
406         {
407             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
408         }
409 
410         /**
411          * Return the cubed value of the argument.
412          * @param x the value to cube
413          * @return x^3
414          */
415         private static double cubed(final double x)
416         {
417             return x * x * x;
418         }
419     }
420 }