View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
4   
5   import org.djunits.unit.unitsystem.UnitSystem;
6   
7   /**
8    * Standard density unit based on mass and length.
9    * <p>
10   * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
12   * <p>
13   * $LastChangedDate: 2019-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, by $Author: averbraeck $,
14   * initial version May 15, 2014 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   */
17  public class DensityUnit extends LinearUnit<DensityUnit>
18  {
19      /** */
20      private static final long serialVersionUID = 20140607L;
21  
22      /** the actual mass unit, e.g. kg. */
23      private final MassUnit massUnit;
24  
25      /** the actual length unit, e.g. meter. */
26      private final LengthUnit lengthUnit;
27  
28      /** The SI unit for standard density is kg/m^3. */
29      public static final DensityUnit SI;
30  
31      /** kg/m^3. */
32      public static final DensityUnit KG_PER_METER_3;
33  
34      /** g/cm^3. */
35      public static final DensityUnit GRAM_PER_CENTIMETER_3;
36  
37      static
38      {
39          SI = new DensityUnit(MassUnit.KILOGRAM, LengthUnit.METER, "DensityUnit.kg/m^3", SI_DERIVED);
40          KG_PER_METER_3 = SI;
41          GRAM_PER_CENTIMETER_3 = new DensityUnit(MassUnit.GRAM, LengthUnit.CENTIMETER, "DensityUnit.g/cm^3", SI_DERIVED);
42      }
43  
44      /**
45       * Define density unit based on mass and length. You can define units like kg/m^3 here.
46       * @param massUnit MassUnit; the unit of mass for the density unit, e.g., kg
47       * @param lengthUnit LengthUnit; the unit of length for the density unit, e.g., meter
48       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
49       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
50       */
51      private DensityUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final String abbreviationKey,
52              final UnitSystem unitSystem)
53      {
54          super(abbreviationKey, unitSystem, KG_PER_METER_3,
55                  massUnit.getScaleFactor() / Math.pow(lengthUnit.getScaleFactor(), 3.0));
56          this.massUnit = massUnit;
57          this.lengthUnit = lengthUnit;
58      }
59  
60      /**
61       * Define user defined density unit based on mass and length. You can define units like kg/in^3 here.
62       * @param massUnit MassUnit; the unit of mass for the density unit, e.g., kg
63       * @param lengthUnit LengthUnit; the unit of length for the density unit, e.g., meter
64       * @param name String; the long name of the unit
65       * @param abbreviation String; the abbreviation of the unit
66       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
67       */
68      public DensityUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final String name, final String abbreviation,
69              final UnitSystem unitSystem)
70      {
71          super(name, abbreviation, unitSystem, KG_PER_METER_3,
72                  massUnit.getScaleFactor() / Math.pow(lengthUnit.getScaleFactor(), 3.0));
73          this.massUnit = massUnit;
74          this.lengthUnit = lengthUnit;
75      }
76  
77      /**
78       * Build a unit with a conversion factor to another unit.
79       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
80       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
81       * @param referenceUnit DensityUnit; the unit to convert to
82       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
83       *            unit
84       */
85      private DensityUnit(final String abbreviationKey, final UnitSystem unitSystem, final DensityUnit referenceUnit,
86              final double scaleFactorToReferenceUnit)
87      {
88          super(abbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
89          this.massUnit = referenceUnit.getMassUnit();
90          this.lengthUnit = referenceUnit.getLengthUnit();
91      }
92  
93      /**
94       * Build a user-defined unit with a conversion factor to another unit.
95       * @param name String; the long name of the unit
96       * @param abbreviation String; the abbreviation of the unit
97       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
98       * @param referenceUnit DensityUnit; the unit to convert to
99       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
100      *            unit
101      */
102     public DensityUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
103             final DensityUnit referenceUnit, final double scaleFactorToReferenceUnit)
104     {
105         super(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
106         this.massUnit = referenceUnit.getMassUnit();
107         this.lengthUnit = referenceUnit.getLengthUnit();
108     }
109 
110     /**
111      * @return massUnit
112      */
113     public final MassUnit getMassUnit()
114     {
115         return this.massUnit;
116     }
117 
118     /**
119      * @return lengthUnit
120      */
121     public final LengthUnit getLengthUnit()
122     {
123         return this.lengthUnit;
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public final DensityUnit getStandardUnit()
129     {
130         return KG_PER_METER_3;
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public final String getSICoefficientsString()
136     {
137         return "kg/m3";
138     }
139 
140 }