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