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-2018 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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, 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.cubic_meter", "VolumeUnit.m^3", SI_DERIVED, true);
118         CUBIC_METER = SI;
119         CUBIC_ATTOMETER =
120                 new VolumeUnit(LengthUnit.ATTOMETER, "VolumeUnit.cubic_attometer", "VolumeUnit.am^3", SI_DERIVED, true);
121         CUBIC_FEMTOMETER =
122                 new VolumeUnit(LengthUnit.FEMTOMETER, "VolumeUnit.cubic_femtometer", "VolumeUnit.fm^3", SI_DERIVED, true);
123         CUBIC_PICOMETER =
124                 new VolumeUnit(LengthUnit.PICOMETER, "VolumeUnit.cubic_picometer", "VolumeUnit.pm^3", SI_DERIVED, true);
125         CUBIC_NANOMETER =
126                 new VolumeUnit(LengthUnit.NANOMETER, "VolumeUnit.cubic_nanometer", "VolumeUnit.nm^3", SI_DERIVED, true);
127         CUBIC_MICROMETER =
128                 new VolumeUnit(LengthUnit.MICROMETER, "VolumeUnit.cubic_micrometer", "VolumeUnit.mum^3", SI_DERIVED, true);
129         CUBIC_MILLIMETER =
130                 new VolumeUnit(LengthUnit.MILLIMETER, "VolumeUnit.cubic_millimeter", "VolumeUnit.mm^3", SI_DERIVED, true);
131         CUBIC_CENTIMETER =
132                 new VolumeUnit(LengthUnit.CENTIMETER, "VolumeUnit.cubic_centimeter", "VolumeUnit.cm^3", SI_DERIVED, true);
133         CUBIC_DECIMETER =
134                 new VolumeUnit(LengthUnit.DECIMETER, "VolumeUnit.cubic_decimeter", "VolumeUnit.dm^3", SI_DERIVED, true);
135         CUBIC_DEKAMETER =
136                 new VolumeUnit(LengthUnit.DEKAMETER, "VolumeUnit.cubic_dekameter", "VolumeUnit.dam^3", SI_DERIVED, true);
137         CUBIC_HECTOMETER =
138                 new VolumeUnit(LengthUnit.HECTOMETER, "VolumeUnit.cubic_hectometer", "VolumeUnit.hm^3", SI_DERIVED, true);
139         CUBIC_KILOMETER =
140                 new VolumeUnit(LengthUnit.KILOMETER, "VolumeUnit.cubic_kilometer", "VolumeUnit.km^3", SI_DERIVED, true);
141         CUBIC_MEGAMETER =
142                 new VolumeUnit(LengthUnit.MEGAMETER, "VolumeUnit.cubic_megameter", "VolumeUnit.Mm^3", SI_DERIVED, true);
143         LITER = new VolumeUnit("VolumeUnit.liter", "VolumeUnit.L", SI_ACCEPTED, CUBIC_DECIMETER, 1.0, true);
144         CUBIC_MILE = new VolumeUnit(LengthUnit.MILE, "VolumeUnit.cubic_mile", "VolumeUnit.mi^3", IMPERIAL, true);
145         CUBIC_FOOT = new VolumeUnit(LengthUnit.FOOT, "VolumeUnit.cubic_foot", "VolumeUnit.ft^3", IMPERIAL, true);
146         CUBIC_INCH = new VolumeUnit(LengthUnit.INCH, "VolumeUnit.cubic_inch", "VolumeUnit.in^3", IMPERIAL, true);
147         CUBIC_YARD = new VolumeUnit(LengthUnit.YARD, "VolumeUnit.cubic_yard", "VolumeUnit.yd^3", IMPERIAL, true);
148         GALLON_US_FLUID = new VolumeUnit("VolumeUnit.gallon_(US)", "VolumeUnit.gal(US)", US_CUSTOMARY, CUBIC_INCH, 231.0, true);
149         GALLON_IMP = new VolumeUnit("VolumeUnit.gallon_(imp)", "VolumeUnit.gal(imp)", IMPERIAL, LITER, 4.5409, true);
150         OUNCE_US_FLUID = new VolumeUnit("VolumeUnit.ounce_(fluid_US)", "VolumeUnit.US_fl_oz", US_CUSTOMARY, GALLON_US_FLUID,
151                 1.0 / 128.0, true);
152         OUNCE_IMP_FLUID = new VolumeUnit("VolumeUnit.ounce_(fluid_imperial)", "VolumeUnit.fl_oz_(imp)", IMPERIAL, GALLON_IMP,
153                 1.0 / 160.0, true);
154         PINT_US_FLUID = new VolumeUnit("VolumeUnit.pint_(US_fluid)", "VolumeUnit.pt(US_fl)", US_CUSTOMARY, GALLON_US_FLUID,
155                 1.0 / 8.0, true);
156         PINT_IMP = new VolumeUnit("VolumeUnit.pint_(imperial)", "VolumeUnit.pt_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 8.0, true);
157         QUART_US_FLUID = new VolumeUnit("VolumeUnit.quart_(US_fluid)", "VolumeUnit.qt(US_fl)", US_CUSTOMARY, GALLON_US_FLUID,
158                 1.0 / 4.0, true);
159         QUART_IMP = new VolumeUnit("VolumeUnit.quart_(imperial)", "VolumeUnit.qt_(imp)", IMPERIAL, GALLON_IMP, 1.0 / 4.0, true);
160         CUBIC_LIGHTYEAR =
161                 new VolumeUnit(LengthUnit.LIGHTYEAR, "VolumeUnit.cubic_lightyear", "VolumeUnit.ly^3", SI_DERIVED, true);
162         CUBIC_PARSEC = new VolumeUnit(LengthUnit.PARSEC, "VolumeUnit.cubic_parsec", "VolumeUnit.pc^3", SI_DERIVED, true);
163     }
164 
165     /**
166      * Define volume unit based on length, e.g. a m^3 is based on meters.
167      * @param lengthUnit the unit of length for the speed unit, e.g., meter
168      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
169      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
170      *            otherwise the abbreviation itself
171      * @param unitSystem the unit system, e.g. SI or Imperial
172      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
173      */
174     private VolumeUnit(final LengthUnit lengthUnit, final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
175             final UnitSystem unitSystem, final boolean standardUnit)
176     {
177         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, CUBIC_METER, Math.pow(lengthUnit.getScaleFactor(), 3),
178                 standardUnit);
179         this.lengthUnit = lengthUnit;
180     }
181 
182     /**
183      * Define a user-defined volume unit based on length, e.g. a m^3 is based on meters.
184      * @param lengthUnit the unit of length for the speed unit, e.g., meter
185      * @param name the long name of the unit
186      * @param abbreviation the abbreviation of the unit
187      * @param unitSystem the unit system, e.g. SI or Imperial
188      */
189     public VolumeUnit(final LengthUnit lengthUnit, final String name, final String abbreviation, final UnitSystem unitSystem)
190     {
191         this(lengthUnit, name, abbreviation, unitSystem, false);
192     }
193 
194     /**
195      * This constructor constructs a uVolumeUnitnit out of another defined VolumeUnit, e.g. quart is 0.25 gallon.
196      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
197      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
198      *            otherwise the abbreviation itself
199      * @param unitSystem the unit system, e.g. SI or Imperial
200      * @param referenceUnit the unit to convert to
201      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
202      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
203      */
204     private VolumeUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
205             final VolumeUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
206     {
207         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
208                 standardUnit);
209         this.lengthUnit = referenceUnit.getLengthUnit();
210     }
211 
212     /**
213      * Build a user-defined VolumeUnit with a conversion factor to another VolumeUnit, e.g. quart is 0.25 gallon.
214      * @param name the long name of the unit
215      * @param abbreviation the abbreviation of the unit
216      * @param unitSystem the unit system, e.g. SI or Imperial
217      * @param referenceUnit the unit to convert to
218      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
219      */
220     public VolumeUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final VolumeUnit referenceUnit,
221             final double scaleFactorToReferenceUnit)
222     {
223         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
224     }
225 
226     /**
227      * @return lengthUnit
228      */
229     public final LengthUnit getLengthUnit()
230     {
231         return this.lengthUnit;
232     }
233 
234     /** {@inheritDoc} */
235     @Override
236     public final VolumeUnit getStandardUnit()
237     {
238         return CUBIC_METER;
239     }
240 
241     /** {@inheritDoc} */
242     @Override
243     public final String getSICoefficientsString()
244     {
245         return "m3";
246     }
247 
248 }