View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.IMPERIAL;
4   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_ACCEPTED;
5   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_DERIVED;
6   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.US_CUSTOMARY;
7   
8   import org.opentrafficsim.core.unit.unitsystem.UnitSystem;
9   
10  /**
11   * VolumeUnit defines a number of common units for volumes.
12   * <p>
13   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
15   * <p>
16   * @version May 15, 2014 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class VolumeUnit extends Unit<VolumeUnit>
20  {
21      /** */
22      private static final long serialVersionUID = 20140604L;
23  
24      /** The unit of length for the volume unit, e.g., meter. */
25      private final LengthUnit lengthUnit;
26  
27      /** The SI unit for volume is m^3. */
28      public static final VolumeUnit SI;
29  
30      /** m^3. */
31      public static final VolumeUnit CUBIC_METER;
32  
33      /** dm^3. */
34      public static final VolumeUnit CUBIC_DECIMETER;
35  
36      /** liter. */
37      public static final VolumeUnit LITER;
38  
39      /** cm^3. */
40      public static final VolumeUnit CUBIC_CENTIMETER;
41  
42      /** km^3. */
43      public static final VolumeUnit CUBIC_KM;
44  
45      /** mile^3. */
46      public static final VolumeUnit CUBIC_MILE;
47  
48      /** ft^3. */
49      public static final VolumeUnit CUBIC_FOOT;
50  
51      /** in^3. */
52      public static final VolumeUnit CUBIC_INCH;
53  
54      /** yd^3. */
55      public static final VolumeUnit CUBIC_YARD;
56  
57      /** gallon (US), fluids. */
58      public static final VolumeUnit GALLON_US_FLUID;
59  
60      /** gallon (imperial). */
61      public static final VolumeUnit GALLON_IMP;
62  
63      /** ounce (fluid US). */
64      public static final VolumeUnit OUNCE_US_FLUID;
65  
66      /** ounce (fluid imperial). */
67      public static final VolumeUnit OUNCE_IMP_FLUID;
68  
69      /** pint (fluid US). */
70      public static final VolumeUnit PINT_US_FLUID;
71  
72      /** pint (imperial). */
73      public static final VolumeUnit PINT_IMP;
74  
75      /** quart (fluid US). */
76      public static final VolumeUnit QUART_US_FLUID;
77  
78      /** quart (imperial). */
79      public static final VolumeUnit QUART_IMP;
80  
81      static
82      {
83          SI = new VolumeUnit(LengthUnit.METER, "VolumeUnit.cubic_meter", "VolumeUnit.m^3", SI_DERIVED);
84          CUBIC_METER = SI;
85          CUBIC_DECIMETER =
86                  new VolumeUnit(LengthUnit.DECIMETER, "VolumeUnit.cubic_decimeter", "VolumeUnit.dm^3", SI_DERIVED);
87          LITER = new VolumeUnit("VolumeUnit.liter", "VolumeUnit.L", SI_ACCEPTED, CUBIC_DECIMETER, 1.0);
88          CUBIC_CENTIMETER =
89                  new VolumeUnit(LengthUnit.CENTIMETER, "VolumeUnit.cubic_centimeter", "VolumeUnit.cm^3", SI_DERIVED);
90          CUBIC_KM = new VolumeUnit(LengthUnit.KILOMETER, "VolumeUnit.cubic_kilometer", "VolumeUnit.km^3", SI_DERIVED);
91          CUBIC_MILE = new VolumeUnit(LengthUnit.MILE, "VolumeUnit.cubic_mile", "VolumeUnit.mi^3", IMPERIAL);
92          CUBIC_FOOT = new VolumeUnit(LengthUnit.FOOT, "VolumeUnit.cubic_foot", "VolumeUnit.ft^3", IMPERIAL);
93          CUBIC_INCH = new VolumeUnit(LengthUnit.INCH, "VolumeUnit.cubic_inch", "VolumeUnit.in^3", IMPERIAL);
94          CUBIC_YARD = new VolumeUnit(LengthUnit.YARD, "VolumeUnit.cubic_yard", "VolumeUnit.yd^3", IMPERIAL);
95          GALLON_US_FLUID =
96                  new VolumeUnit("VolumeUnit.gallon_(US)", "VolumeUnit.gal(US)", US_CUSTOMARY, CUBIC_INCH, 231.0);
97          GALLON_IMP = new VolumeUnit("VolumeUnit.gallon_(imp)", "VolumeUnit.gal(imp)", IMPERIAL, LITER, 4.5409);
98          OUNCE_US_FLUID =
99                  new VolumeUnit("VolumeUnit.ounce_(fluid_US)", "VolumeUnit.US_fl_oz", US_CUSTOMARY, GALLON_US_FLUID,
100                         1.0 / 128.0);
101         OUNCE_IMP_FLUID =
102                 new VolumeUnit("VolumeUnit.ounce_(fluid_imperial)", "VolumeUnit.fl_oz_(imp)", IMPERIAL, GALLON_IMP,
103                         1.0 / 160.0);
104         PINT_US_FLUID =
105                 new VolumeUnit("VolumeUnit.pint_(US_fluid)", "VolumeUnit.pt(US_fl)", US_CUSTOMARY, GALLON_US_FLUID,
106                         1.0 / 8.0);
107         PINT_IMP = new VolumeUnit("VolumeUnit.pint_(imperial)", "VolumeUnit.pt_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 8.0);
108         QUART_US_FLUID =
109                 new VolumeUnit("VolumeUnit.quart_(US_fluid)", "VolumeUnit.qt(US_fl)", US_CUSTOMARY, GALLON_US_FLUID,
110                         1.0 / 4.0);
111         QUART_IMP =
112                 new VolumeUnit("VolumeUnit.quart_(imperial)", "VolumeUnit.qt_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 4.0);
113     }
114 
115     /**
116      * Define volume unit based on length, e.g. a m^3 is based on meters.
117      * @param lengthUnit the unit of length for the speed unit, e.g., meter
118      * @param nameKey the key to the locale file for the long name of the unit
119      * @param abbreviationKey the key to the locale file for the abbreviation of the unit
120      * @param unitSystem the unit system, e.g. SI or Imperial
121      */
122     public VolumeUnit(final LengthUnit lengthUnit, final String nameKey, final String abbreviationKey,
123             final UnitSystem unitSystem)
124     {
125         super(nameKey, abbreviationKey, unitSystem, CUBIC_METER, Math.pow(
126                 lengthUnit.getConversionFactorToStandardUnit(), 3), true);
127         this.lengthUnit = lengthUnit;
128     }
129 
130     /**
131      * This constructor constructs a unit out of another defined unit, e.g. quart is 0.25 gallon.
132      * @param nameKey the key to the locale file for the long name of the unit
133      * @param abbreviationKey the key to the locale file for the abbreviation of the unit
134      * @param unitSystem the unit system, e.g. SI or Imperial
135      * @param referenceUnit the unit to convert to
136      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given
137      *            reference unit
138      */
139     public VolumeUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem,
140             final VolumeUnit referenceUnit, final double conversionFactorToReferenceUnit)
141     {
142         super(nameKey, abbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit, true);
143         this.lengthUnit = referenceUnit.getLengthUnit();
144     }
145 
146     /**
147      * @return lengthUnit
148      */
149     public final LengthUnit getLengthUnit()
150     {
151         return this.lengthUnit;
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     public final VolumeUnit getStandardUnit()
157     {
158         return CUBIC_METER;
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public final String getSICoefficientsString()
164     {
165         return "m3";
166     }
167 
168 }