View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_DERIVED;
4   
5   import org.opentrafficsim.core.unit.unitsystem.UnitSystem;
6   
7   /**
8    * Standard density units based on mass and length.
9    * <p>
10   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
12   * <p>
13   * @version May 15, 2014 <br>
14   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   */
16  public class DensityUnit extends Unit<DensityUnit>
17  {
18      /** */
19      private static final long serialVersionUID = 20140607L;
20  
21      /** the actual mass unit, e.g. kg. */
22      private final MassUnit massUnit;
23  
24      /** the actual length unit, e.g. meter. */
25      private final LengthUnit lengthUnit;
26  
27      /** The SI unit for standard density is kg/m^3. */
28      public static final DensityUnit SI;
29  
30      /** kg/m^3. */
31      public static final DensityUnit KG_PER_METER_3;
32  
33      /** g/cm^3. */
34      public static final DensityUnit GRAM_PER_CENTIMETER_3;
35  
36      static
37      {
38          SI =
39                  new DensityUnit(MassUnit.KILOGRAM, LengthUnit.METER, "DensityUnit.kilogram_per_cubic_meter",
40                          "DensityUnit.kg/m^3", SI_DERIVED);
41          KG_PER_METER_3 = SI;
42          GRAM_PER_CENTIMETER_3 =
43                  new DensityUnit(MassUnit.GRAM, LengthUnit.CENTIMETER, "DensityUnit.gram_per_cubic_centimeter",
44                          "DensityUnit.g/cm^3", SI_DERIVED);
45      }
46  
47      /**
48       * Define density units based on mass and length. You can define units like kg/m^3 here.
49       * @param massUnit the unit of mass for the density unit, e.g., kg
50       * @param lengthUnit the unit of length for the density unit, e.g., meter
51       * @param nameKey the key to the locale file for the long name of the unit
52       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
53       * @param unitSystem the unit system, e.g. SI or Imperial
54       */
55      public DensityUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final String nameKey,
56              final String abbreviationKey, final UnitSystem unitSystem)
57      {
58          super(nameKey, abbreviationKey, unitSystem, KG_PER_METER_3, massUnit.getConversionFactorToStandardUnit()
59                  / Math.pow(lengthUnit.getConversionFactorToStandardUnit(), 3.0), true);
60          this.massUnit = massUnit;
61          this.lengthUnit = lengthUnit;
62      }
63  
64      /**
65       * @param nameKey the key to the locale file for the long name of the unit
66       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
67       * @param unitSystem the unit system, e.g. SI or Imperial
68       * @param referenceUnit the unit to convert to
69       * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given
70       *            reference unit
71       */
72      public DensityUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem,
73              final DensityUnit referenceUnit, final double conversionFactorToReferenceUnit)
74      {
75          super(nameKey, abbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit, true);
76          this.massUnit = referenceUnit.getMassUnit();
77          this.lengthUnit = referenceUnit.getLengthUnit();
78      }
79  
80      /**
81       * @return massUnit
82       */
83      public final MassUnit getMassUnit()
84      {
85          return this.massUnit;
86      }
87  
88      /**
89       * @return lengthUnit
90       */
91      public final LengthUnit getLengthUnit()
92      {
93          return this.lengthUnit;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public final DensityUnit getStandardUnit()
99      {
100         return KG_PER_METER_3;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final String getSICoefficientsString()
106     {
107         return "kg/m3";
108     }
109 
110 }