View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.quantity.Quantity;
4   import org.djunits.unit.scale.IdentityScale;
5   import org.djunits.unit.si.SIPrefixes;
6   import org.djunits.unit.unitsystem.UnitSystem;
7   
8   /**
9    * VolumeUnit defines a number of common units for volumes.
10   * <p>
11   * Copyright (c) 2015-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
13   * <p>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   */
16  public class VolumeUnit extends Unit<VolumeUnit>
17  {
18      /** */
19      private static final long serialVersionUID = 20140604L;
20  
21      /** The base, with "m2" as the SI signature. */
22      public static final Quantity<VolumeUnit> BASE = new Quantity<>("Volume", "m3");
23  
24      /** The SI unit for area is m^3. */
25      public static final VolumeUnit SI =
26              new VolumeUnit().build(new Unit.Builder<VolumeUnit>().setQuantity(BASE).setId("m^3").setName("cubic meter")
27                      .setUnitSystem(UnitSystem.SI_DERIVED).setSiPrefixes(SIPrefixes.UNIT, 3.0).setScale(IdentityScale.SCALE));
28  
29      /** m^3. */
30      public static final VolumeUnit CUBIC_METER = SI;
31  
32      /** mm^3. */
33      public static final VolumeUnit CUBIC_MILLIMETER = CUBIC_METER.deriveLinear(1.0E-9, "mm^3", "cubic millimeter");
34  
35      /** cm^3. */
36      public static final VolumeUnit CUBIC_CENTIMETER = CUBIC_METER.deriveLinear(1.0E-6, "cm^3", "cubic centimeter");
37  
38      /** dm^3. */
39      public static final VolumeUnit CUBIC_DECIMETER = CUBIC_METER.deriveLinear(1.0E-3, "dm^3", "cubic decimeter");
40  
41      /** dam^3. */
42      public static final VolumeUnit CUBIC_DECAMETER = CUBIC_METER.deriveLinear(1.0E3, "dam^3", "cubic decameter");
43  
44      /** hm^3. */
45      public static final VolumeUnit CUBIC_HECTOMETER = CUBIC_METER.deriveLinear(1.0E6, "hm^3", "cubic hectometer");
46  
47      /** km^3. */
48      public static final VolumeUnit CUBIC_KILOMETER = CUBIC_METER.deriveLinear(1.0E9, "km^3", "cubic kilometer");
49  
50      /** mile^3. */
51      public static final VolumeUnit CUBIC_MILE =
52              CUBIC_METER.deriveLinear(cubedLength(LengthUnit.MILE), "mi^3", "cubic mile", UnitSystem.IMPERIAL);
53  
54      /** Nautical mile^3. */
55      public static final VolumeUnit CUBIC_NAUTICAL_MILE =
56              CUBIC_METER.deriveLinear(cubedLength(LengthUnit.NAUTICAL_MILE), "NM^3", "cubic Nautical Mile", UnitSystem.OTHER);
57  
58      /** ft^3. */
59      public static final VolumeUnit CUBIC_FOOT =
60              CUBIC_METER.deriveLinear(cubedLength(LengthUnit.FOOT), "ft^3", "cubic foot", UnitSystem.IMPERIAL);
61  
62      /** in^3. */
63      public static final VolumeUnit CUBIC_INCH =
64              CUBIC_METER.deriveLinear(cubedLength(LengthUnit.INCH), "in^3", "cubic inch", UnitSystem.IMPERIAL);
65  
66      /** yd^3. */
67      public static final VolumeUnit CUBIC_YARD =
68              CUBIC_METER.deriveLinear(cubedLength(LengthUnit.YARD), "yd^3", "cubic yard", UnitSystem.IMPERIAL);
69  
70      /** liter. */
71      public static final VolumeUnit LITER = CUBIC_DECIMETER.deriveLinear(1.0, "L", "liter", UnitSystem.SI_ACCEPTED);
72  
73      /** gallon (US), fluids. */
74      public static final VolumeUnit GALLON_US =
75              CUBIC_INCH.deriveLinear(231.0, "gal(US)", "gallon (US)", UnitSystem.US_CUSTOMARY);
76  
77      /** gallon (imperial). */
78      public static final VolumeUnit GALLON_IMP = LITER.deriveLinear(4.54609, "gal(imp)", "gallon (imp)", UnitSystem.IMPERIAL);
79  
80      /** quart (fluid US) = 1/4 US gallon. */
81      public static final VolumeUnit QUART_US = GALLON_US.deriveLinear(0.25, "qt(US)", "quart (US)");
82  
83      /** quart (imperial) = 1/4 imp gallon. */
84      public static final VolumeUnit QUART_IMP = GALLON_IMP.deriveLinear(0.25, "qt(imp)", "quart (imp)");
85  
86      /** pint (fluid US) = 1/2 US quart. */
87      public static final VolumeUnit PINT_US = QUART_US.deriveLinear(0.5, "pt(US)", "pint (US)");
88  
89      /** pint (imperial) = 1/2 imp quart. */
90      public static final VolumeUnit PINT_IMP = QUART_IMP.deriveLinear(0.5, "pt(imp)", "pint (imp)");
91  
92      /** ounce (fluid US) = 1/16 US pint. */
93      public static final VolumeUnit FLUID_OUNCE_US = PINT_US.deriveLinear(1.0 / 16.0, "fl.oz(US)", "fluid ounce (US)");
94  
95      /** ounce (fluid imperial) = 1/20 imp pint. */
96      public static final VolumeUnit FLUID_OUNCE_IMP = PINT_IMP.deriveLinear(1.0 / 20.0, "fl.oz(imp)", "fluid ounce (imp)");
97  
98      /** Cubic lightyear. */
99      public static final VolumeUnit CUBIC_LIGHTYEAR =
100             CUBIC_METER.deriveLinear(cubedLength(LengthUnit.LIGHTYEAR), "ly^3", "cubic lightyear", UnitSystem.OTHER);
101 
102     /** Cubic Parsec. */
103     public static final VolumeUnit CUBIC_PARSEC =
104             CUBIC_METER.deriveLinear(cubedLength(LengthUnit.PARSEC), "pc^3", "cubic Parsec", UnitSystem.OTHER);
105 
106     /**
107      * Calculate the conversion factor for a "cubed" length.
108      * @param lu LengthUnit; the LengthUnit to use as the base
109      * @return double; the conversion factor
110      */
111     private static double cubedLength(final LengthUnit lu)
112     {
113         double factor = lu.getScale().toStandardUnit(1.0);
114         return factor * factor * factor;
115     }
116 
117 }