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, Volume.Unit>
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 value the value, expressed in the unit
51       * @param unit the unit in which the value is expressed
52       */
53      public Volume(final double value, final Volume.Unit unit)
54      {
55          super(value, unit);
56      }
57  
58      /**
59       * Instantiate a Volume quantity with a unit, expressed as a String.
60       * @param value 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 value, final String abbreviation)
64      {
65          this(value, 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 instantiate(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 value the value to use
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 value, final String unitString)
123     {
124         return Quantity.of(value, unitString, ZERO);
125     }
126 
127     /**
128      * Calculate the division of Volume and Volume, which results in a Dimensionless scalar.
129      * @param v scalar
130      * @return scalar as a division of Volume and Volume
131      */
132     public final Dimensionless divide(final Volume v)
133     {
134         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
135     }
136 
137     /**
138      * Calculate the multiplication of Volume and Density, which results in a Mass scalar.
139      * @param v scalar
140      * @return scalar as a multiplication of Volume and Density
141      */
142     public final Mass multiply(final Density v)
143     {
144         return new Mass(this.si() * v.si(), Mass.Unit.SI);
145     }
146 
147     /**
148      * Calculate the multiplication of Volume and Pressure, which results in a Energy scalar.
149      * @param v scalar
150      * @return scalar as a multiplication of Volume and Pressure
151      */
152     public final Energy multiply(final Pressure v)
153     {
154         return new Energy(this.si() * v.si(), Energy.Unit.SI);
155     }
156 
157     /**
158      * Calculate the division of Volume and Length, which results in a Area scalar.
159      * @param v scalar
160      * @return scalar as a division of Volume and Length
161      */
162     public final Area divide(final Length v)
163     {
164         return new Area(this.si() / v.si(), Area.Unit.SI);
165     }
166 
167     /**
168      * Calculate the division of Volume and Area, which results in a Length scalar.
169      * @param v scalar
170      * @return scalar as a division of Volume and Area
171      */
172     public final Length divide(final Area v)
173     {
174         return new Length(this.si() / v.si(), Length.Unit.SI);
175     }
176 
177     /**
178      * Calculate the multiplication of Volume and LinearObjectDensity, which results in a Area scalar.
179      * @param v scalar
180      * @return scalar as a multiplication of Volume and LinearObjectDensity
181      */
182     public final Area multiply(final LinearObjectDensity v)
183     {
184         return new Area(this.si() * v.si(), Area.Unit.SI);
185     }
186 
187     /**
188      * Calculate the division of Volume and Duration, which results in a FlowVolume scalar.
189      * @param v scalar
190      * @return scalar as a division of Volume and Duration
191      */
192     public final FlowVolume divide(final Duration v)
193     {
194         return new FlowVolume(this.si() / v.si(), FlowVolume.Unit.SI);
195     }
196 
197     /**
198      * Calculate the division of Volume and FlowVolume, which results in a Duration scalar.
199      * @param v scalar
200      * @return scalar as a division of Volume and FlowVolume
201      */
202     public final Duration divide(final FlowVolume v)
203     {
204         return new Duration(this.si() / v.si(), Duration.Unit.SI);
205     }
206 
207     @Override
208     public VolumetricObjectDensity reciprocal()
209     {
210         return VolumetricObjectDensity.ofSi(1.0 / this.si());
211     }
212 
213     /******************************************************************************************************/
214     /********************************************** UNIT CLASS ********************************************/
215     /******************************************************************************************************/
216 
217     /**
218      * Volume.Unit encodes the volume unit (length x length x length).
219      * <p>
220      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
221      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
222      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
223      * @author Alexander Verbraeck
224      */
225     @SuppressWarnings("checkstyle:constantname")
226     public static class Unit extends AbstractUnit<Volume.Unit, Volume>
227     {
228         /** Constant for the cubic inch. */
229         public static final double CONST_CUBIC_INCH = cubed(Length.Unit.CONST_IN);
230 
231         /** Constant for the cubic foot. */
232         public static final double CONST_CUBIC_FOOT = cubed(Length.Unit.CONST_FT);
233 
234         /** Constant for the cubic yard. */
235         public static final double CONST_CUBIC_YARD = cubed(Length.Unit.CONST_YD);
236 
237         /** Constant for the imperial gallon. */
238         public static final double CONST_GALLON_IMP = 4.54609E-3;
239 
240         /** Constant for imperial fluid ounce. */
241         public static final double CONST_OZ_IMP = CONST_GALLON_IMP / 160.0;
242 
243         /** Constant for US gallon. */
244         public static final double CONST_GALLON_US = 231.0 * CONST_CUBIC_INCH;
245 
246         /** Constant for US fluid ounce. */
247         public static final double CONST_OZ_US = CONST_GALLON_US / 128.0;
248 
249         /** The dimensions of Volume: m3. */
250         public static final SIUnit SI_UNIT = SIUnit.of("m3");
251 
252         /** Cubic meter. */
253         public static final Volume.Unit m3 = new Volume.Unit("m3", "cubic meter", 1.0, UnitSystem.SI_BASE);
254 
255         /** The SI or BASE unit. */
256         public static final Volume.Unit SI = m3;
257 
258         /** mm^3. */
259         public static final Volume.Unit mm3 = m3.deriveUnit("mm3", "cubic millimeter", 1.0E-9, UnitSystem.SI_BASE);
260 
261         /** cm^3. */
262         public static final Volume.Unit cm3 = m3.deriveUnit("cm3", "cubic centimeter", 1.0E-6, UnitSystem.SI_BASE);
263 
264         /** dm^3. */
265         public static final Volume.Unit dm3 = m3.deriveUnit("dm3", "cubic decimeter", 1.0E-3, UnitSystem.SI_BASE);
266 
267         /** dam^3. */
268         public static final Volume.Unit dam3 = m3.deriveUnit("dam3", "cubic decameter", 1.0E3, UnitSystem.SI_BASE);
269 
270         /** hm^3. */
271         public static final Volume.Unit hm3 = m3.deriveUnit("hm3", "cubic hectometer", 1.0E6, UnitSystem.SI_BASE);
272 
273         /** km^3. */
274         public static final Volume.Unit km3 = m3.deriveUnit("km3", "cubic kilometer", 1.0E9, UnitSystem.SI_BASE);
275 
276         /** in^3. */
277         public static final Volume.Unit in3 = m3.deriveUnit("in3", "cubic inch", CONST_CUBIC_INCH, UnitSystem.IMPERIAL);
278 
279         /** ft^3. */
280         public static final Volume.Unit ft3 = m3.deriveUnit("ft3", "cubic foot", CONST_CUBIC_FOOT, UnitSystem.IMPERIAL);
281 
282         /** yd^3. */
283         public static final Volume.Unit yd3 = m3.deriveUnit("yd3", "cubic yard", CONST_CUBIC_YARD, UnitSystem.IMPERIAL);
284 
285         /** mile^3. */
286         public static final Volume.Unit mi3 =
287                 m3.deriveUnit("mi3", "cubic mile", cubed(Length.Unit.CONST_MI), UnitSystem.IMPERIAL);
288 
289         /** Nautical mile^3. */
290         public static final Volume.Unit NM3 =
291                 m3.deriveUnit("NM3", "cubic Nautical Mile", cubed(Length.Unit.CONST_NM), UnitSystem.OTHER);
292 
293         /** liter. */
294         public static final Volume.Unit L = dm3.deriveUnit("L", "liter", 1.0, UnitSystem.SI_ACCEPTED);
295 
296         /** gallon (US), fluids. */
297         public static final Volume.Unit gal_US =
298                 m3.deriveUnit("gal(US)", "gallon (US)", CONST_GALLON_US, UnitSystem.US_CUSTOMARY);
299 
300         /** gallon (imperial). */
301         public static final Volume.Unit gal_imp =
302                 m3.deriveUnit("gal(imp)", "gallon (imp)", CONST_GALLON_IMP, UnitSystem.IMPERIAL);
303 
304         /** quart (fluid US) = 1/4 US gallon. */
305         public static final Volume.Unit qt_US = gal_US.deriveUnit("qt(US)", "quart (US)", 0.25, UnitSystem.US_CUSTOMARY);
306 
307         /** quart (imperial) = 1/4 imp gallon. */
308         public static final Volume.Unit qt_imp = gal_imp.deriveUnit("qt(imp)", "quart (imp)", 0.25, UnitSystem.IMPERIAL);
309 
310         /** pint (fluid US) = 1/2 US quart. */
311         public static final Volume.Unit pt_US = qt_US.deriveUnit("pt(US)", "pint (US)", 0.5, UnitSystem.US_CUSTOMARY);
312 
313         /** pint (imperial) = 1/2 imp quart. */
314         public static final Volume.Unit pt_imp = qt_imp.deriveUnit("pt(imp)", "pint (imp)", 0.5, UnitSystem.IMPERIAL);
315 
316         /** ounce (fluid US) = 1/16 US pint. */
317         public static final Volume.Unit fl_oz_US =
318                 m3.deriveUnit("fl.oz(US)", "fluid ounce (US)", CONST_OZ_US, UnitSystem.US_CUSTOMARY);
319 
320         /** ounce (fluid imperial) = 1/20 imp pint. */
321         public static final Volume.Unit fl_oz_imp =
322                 m3.deriveUnit("fl.oz(imp)", "fluid ounce (imp)", CONST_OZ_IMP, UnitSystem.IMPERIAL);
323 
324         /** Cubic lightyear. */
325         public static final Volume.Unit ly3 =
326                 m3.deriveUnit("ly3", "cubic lightyear", cubed(Length.Unit.CONST_LY), UnitSystem.OTHER);
327 
328         /** Cubic Parsec. */
329         public static final Volume.Unit pc3 =
330                 m3.deriveUnit("pc3", "cubic Parsec", cubed(Length.Unit.CONST_PC), UnitSystem.OTHER);
331 
332         /**
333          * Create a new Volume unit.
334          * @param id the id or main abbreviation of the unit
335          * @param name the full name of the unit
336          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
337          * @param unitSystem the unit system such as SI or IMPERIAL
338          */
339         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
340         {
341             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
342         }
343 
344         /**
345          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
346          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
347          * @param displayAbbreviation the display abbreviation of the unit
348          * @param name the full name of the unit
349          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
350          * @param unitSystem unit system, e.g. SI or Imperial
351          */
352         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
353                 final UnitSystem unitSystem)
354         {
355             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
356         }
357 
358         @Override
359         public SIUnit siUnit()
360         {
361             return SI_UNIT;
362         }
363 
364         @Override
365         public Unit getBaseUnit()
366         {
367             return SI;
368         }
369 
370         @Override
371         public Volume ofSi(final double si)
372         {
373             return Volume.ofSi(si);
374         }
375 
376         @Override
377         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
378                 final double scaleFactor, final UnitSystem unitSystem)
379         {
380             if (getScale() instanceof LinearScale ls)
381             {
382                 return new Volume.Unit(textualAbbreviation, displayAbbreviation, name,
383                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
384             }
385             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
386         }
387 
388         /**
389          * Return the cubed value of the argument.
390          * @param x the value to cube
391          * @return x^3
392          */
393         private static double cubed(final double x)
394         {
395             return x * x * x;
396         }
397     }
398 }