View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.IMPERIAL;
4   import static org.djunits.unit.unitsystem.UnitSystem.SI_ACCEPTED;
5   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
6   import static org.djunits.unit.unitsystem.UnitSystem.US_CUSTOMARY;
7   
8   import org.djunits.unit.unitsystem.UnitSystem;
9   
10  /**
11   * VolumeUnit defines a number of common units for volumes.
12   * <p>
13   * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
15   * <p>
16   * $LastChangedDate: 2019-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, by $Author: averbraeck $,
17   * initial version May 15, 2014 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  public class VolumeUnit extends LinearUnit<VolumeUnit>
21  {
22      /** */
23      private static final long serialVersionUID = 20140604L;
24  
25      /** The unit of length for the volume unit, e.g., meter. */
26      private final LengthUnit lengthUnit;
27  
28      /** The SI unit for volume is m^3. */
29      public static final VolumeUnit SI;
30  
31      /** am^3. */
32      public static final VolumeUnit CUBIC_ATTOMETER;
33  
34      /** fm^3. */
35      public static final VolumeUnit CUBIC_FEMTOMETER;
36  
37      /** pm^3. */
38      public static final VolumeUnit CUBIC_PICOMETER;
39  
40      /** nm^3. */
41      public static final VolumeUnit CUBIC_NANOMETER;
42  
43      /** &#181;m^3. */
44      public static final VolumeUnit CUBIC_MICROMETER;
45  
46      /** mm^3. */
47      public static final VolumeUnit CUBIC_MILLIMETER;
48  
49      /** cm^3. */
50      public static final VolumeUnit CUBIC_CENTIMETER;
51  
52      /** dm^3. */
53      public static final VolumeUnit CUBIC_DECIMETER;
54  
55      /** m^3. */
56      public static final VolumeUnit CUBIC_METER;
57  
58      /** dam^3. */
59      public static final VolumeUnit CUBIC_DEKAMETER;
60  
61      /** hm^3. */
62      public static final VolumeUnit CUBIC_HECTOMETER;
63  
64      /** km^3. */
65      public static final VolumeUnit CUBIC_KILOMETER;
66  
67      /** Mm^3. */
68      public static final VolumeUnit CUBIC_MEGAMETER;
69  
70      /** liter. */
71      public static final VolumeUnit LITER;
72  
73      /** mile^3. */
74      public static final VolumeUnit CUBIC_MILE;
75  
76      /** ft^3. */
77      public static final VolumeUnit CUBIC_FOOT;
78  
79      /** in^3. */
80      public static final VolumeUnit CUBIC_INCH;
81  
82      /** yd^3. */
83      public static final VolumeUnit CUBIC_YARD;
84  
85      /** gallon (US), fluids. */
86      public static final VolumeUnit GALLON_US_FLUID;
87  
88      /** gallon (imperial). */
89      public static final VolumeUnit GALLON_IMP;
90  
91      /** ounce (fluid US). */
92      public static final VolumeUnit OUNCE_US_FLUID;
93  
94      /** ounce (fluid imperial). */
95      public static final VolumeUnit OUNCE_IMP_FLUID;
96  
97      /** pint (fluid US). */
98      public static final VolumeUnit PINT_US_FLUID;
99  
100     /** pint (imperial). */
101     public static final VolumeUnit PINT_IMP;
102 
103     /** quart (fluid US). */
104     public static final VolumeUnit QUART_US_FLUID;
105 
106     /** quart (imperial). */
107     public static final VolumeUnit QUART_IMP;
108 
109     /** Cubic lightyear. */
110     public static final VolumeUnit CUBIC_LIGHTYEAR;
111 
112     /** Cubic Parsec. */
113     public static final VolumeUnit CUBIC_PARSEC;
114 
115     static
116     {
117         SI = new VolumeUnit(LengthUnit.METER, "VolumeUnit.m^3", SI_DERIVED);
118         CUBIC_METER = SI;
119         CUBIC_ATTOMETER = new VolumeUnit(LengthUnit.ATTOMETER, "VolumeUnit.am^3", SI_DERIVED);
120         CUBIC_FEMTOMETER = new VolumeUnit(LengthUnit.FEMTOMETER, "VolumeUnit.fm^3", SI_DERIVED);
121         CUBIC_PICOMETER = new VolumeUnit(LengthUnit.PICOMETER, "VolumeUnit.pm^3", SI_DERIVED);
122         CUBIC_NANOMETER = new VolumeUnit(LengthUnit.NANOMETER, "VolumeUnit.nm^3", SI_DERIVED);
123         CUBIC_MICROMETER = new VolumeUnit(LengthUnit.MICROMETER, "VolumeUnit.mum^3", SI_DERIVED);
124         CUBIC_MILLIMETER = new VolumeUnit(LengthUnit.MILLIMETER, "VolumeUnit.mm^3", SI_DERIVED);
125         CUBIC_CENTIMETER = new VolumeUnit(LengthUnit.CENTIMETER, "VolumeUnit.cm^3", SI_DERIVED);
126         CUBIC_DECIMETER = new VolumeUnit(LengthUnit.DECIMETER, "VolumeUnit.dm^3", SI_DERIVED);
127         CUBIC_DEKAMETER = new VolumeUnit(LengthUnit.DEKAMETER, "VolumeUnit.dam^3", SI_DERIVED);
128         CUBIC_HECTOMETER = new VolumeUnit(LengthUnit.HECTOMETER, "VolumeUnit.hm^3", SI_DERIVED);
129         CUBIC_KILOMETER = new VolumeUnit(LengthUnit.KILOMETER, "VolumeUnit.km^3", SI_DERIVED);
130         CUBIC_MEGAMETER = new VolumeUnit(LengthUnit.MEGAMETER, "VolumeUnit.Mm^3", SI_DERIVED);
131         LITER = new VolumeUnit("VolumeUnit.L", SI_ACCEPTED, CUBIC_DECIMETER, 1.0);
132         CUBIC_MILE = new VolumeUnit(LengthUnit.MILE, "VolumeUnit.mi^3", IMPERIAL);
133         CUBIC_FOOT = new VolumeUnit(LengthUnit.FOOT, "VolumeUnit.ft^3", IMPERIAL);
134         CUBIC_INCH = new VolumeUnit(LengthUnit.INCH, "VolumeUnit.in^3", IMPERIAL);
135         CUBIC_YARD = new VolumeUnit(LengthUnit.YARD, "VolumeUnit.yd^3", IMPERIAL);
136         GALLON_US_FLUID = new VolumeUnit("VolumeUnit.gal(US)", US_CUSTOMARY, CUBIC_INCH, 231.0);
137         GALLON_IMP = new VolumeUnit("VolumeUnit.gal(imp)", IMPERIAL, LITER, 4.5409);
138         OUNCE_US_FLUID = new VolumeUnit("VolumeUnit.US_fl_oz", US_CUSTOMARY, GALLON_US_FLUID, 1.0 / 128.0);
139         OUNCE_IMP_FLUID = new VolumeUnit("VolumeUnit.fl_oz_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 160.0);
140         PINT_US_FLUID = new VolumeUnit("VolumeUnit.pt(US_fl)", US_CUSTOMARY, GALLON_US_FLUID, 1.0 / 8.0);
141         PINT_IMP = new VolumeUnit("VolumeUnit.pt_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 8.0);
142         QUART_US_FLUID = new VolumeUnit("VolumeUnit.qt(US_fl)", US_CUSTOMARY, GALLON_US_FLUID, 1.0 / 4.0);
143         QUART_IMP = new VolumeUnit("VolumeUnit.qt_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 4.0);
144         CUBIC_LIGHTYEAR = new VolumeUnit(LengthUnit.LIGHTYEAR, "VolumeUnit.ly^3", SI_DERIVED);
145         CUBIC_PARSEC = new VolumeUnit(LengthUnit.PARSEC, "VolumeUnit.pc^3", SI_DERIVED);
146     }
147 
148     /**
149      * Define volume unit based on length, e.g. a m^3 is based on meters.
150      * @param lengthUnit LengthUnit; the unit of length for the speed unit, e.g., meter
151      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
152      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
153      */
154     private VolumeUnit(final LengthUnit lengthUnit, final String abbreviationKey, final UnitSystem unitSystem)
155     {
156         super(abbreviationKey, unitSystem, CUBIC_METER, Math.pow(lengthUnit.getScaleFactor(), 3));
157         this.lengthUnit = lengthUnit;
158     }
159 
160     /**
161      * Define a user-defined volume unit based on length, e.g. a m^3 is based on meters.
162      * @param lengthUnit LengthUnit; the unit of length for the speed unit, e.g., meter
163      * @param name String; the long name of the unit
164      * @param abbreviation String; the abbreviation of the unit
165      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
166      */
167     public VolumeUnit(final LengthUnit lengthUnit, final String name, final String abbreviation, final UnitSystem unitSystem)
168     {
169         super(name, abbreviation, unitSystem, CUBIC_METER, Math.pow(lengthUnit.getScaleFactor(), 3));
170         this.lengthUnit = lengthUnit;
171     }
172 
173     /**
174      * This constructor constructs a uVolumeUnitnit out of another defined VolumeUnit, e.g. quart is 0.25 gallon.
175      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
176      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
177      * @param referenceUnit VolumeUnit; the unit to convert to
178      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
179      *            unit
180      */
181     private VolumeUnit(final String abbreviationKey, final UnitSystem unitSystem, final VolumeUnit referenceUnit,
182             final double scaleFactorToReferenceUnit)
183     {
184         super(abbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
185         this.lengthUnit = referenceUnit.getLengthUnit();
186     }
187 
188     /**
189      * Build a user-defined VolumeUnit with a conversion factor to another VolumeUnit, e.g. quart is 0.25 gallon.
190      * @param name String; the long name of the unit
191      * @param abbreviation String; the abbreviation of the unit
192      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
193      * @param referenceUnit VolumeUnit; the unit to convert to
194      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
195      *            unit
196      */
197     public VolumeUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final VolumeUnit referenceUnit,
198             final double scaleFactorToReferenceUnit)
199     {
200         super(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
201         this.lengthUnit = referenceUnit.getLengthUnit();
202     }
203 
204     /**
205      * @return lengthUnit
206      */
207     public final LengthUnit getLengthUnit()
208     {
209         return this.lengthUnit;
210     }
211 
212     /** {@inheritDoc} */
213     @Override
214     public final VolumeUnit getStandardUnit()
215     {
216         return CUBIC_METER;
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public final String getSICoefficientsString()
222     {
223         return "m3";
224     }
225 
226 }