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-2018 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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, 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.kilogram_per_cubic_meter", "DensityUnit.kg/m^3",
40                  SI_DERIVED, true);
41          KG_PER_METER_3 = SI;
42          GRAM_PER_CENTIMETER_3 = new DensityUnit(MassUnit.GRAM, LengthUnit.CENTIMETER, "DensityUnit.gram_per_cubic_centimeter",
43                  "DensityUnit.g/cm^3", SI_DERIVED, true);
44      }
45  
46      /**
47       * Define density unit based on mass and length. You can define units like kg/m^3 here.
48       * @param massUnit the unit of mass for the density unit, e.g., kg
49       * @param lengthUnit the unit of length for the density unit, e.g., meter
50       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
51       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
52       *            otherwise the abbreviation itself
53       * @param unitSystem the unit system, e.g. SI or Imperial
54       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
55       */
56      private DensityUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final String nameOrNameKey,
57              final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
58      {
59          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, KG_PER_METER_3,
60                  massUnit.getScaleFactor() / Math.pow(lengthUnit.getScaleFactor(), 3.0), standardUnit);
61          this.massUnit = massUnit;
62          this.lengthUnit = lengthUnit;
63      }
64  
65      /**
66       * Define user defined density unit based on mass and length. You can define units like kg/in^3 here.
67       * @param massUnit the unit of mass for the density unit, e.g., kg
68       * @param lengthUnit the unit of length for the density unit, e.g., meter
69       * @param name the long name of the unit
70       * @param abbreviation the abbreviation of the unit
71       * @param unitSystem the unit system, e.g. SI or Imperial
72       */
73      public DensityUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final String name, final String abbreviation,
74              final UnitSystem unitSystem)
75      {
76          this(massUnit, lengthUnit, name, abbreviation, unitSystem, false);
77      }
78  
79      /**
80       * Build a unit with a conversion factor to another unit.
81       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
82       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
83       *            otherwise the abbreviation itself
84       * @param unitSystem the unit system, e.g. SI or Imperial
85       * @param referenceUnit the unit to convert to
86       * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
87       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
88       */
89      private DensityUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
90              final DensityUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
91      {
92          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
93                  standardUnit);
94          this.massUnit = referenceUnit.getMassUnit();
95          this.lengthUnit = referenceUnit.getLengthUnit();
96      }
97  
98      /**
99       * Build a user-defined unit with a conversion factor to another unit.
100      * @param name the long name of the unit
101      * @param abbreviation the abbreviation of the unit
102      * @param unitSystem the unit system, e.g. SI or Imperial
103      * @param referenceUnit the unit to convert to
104      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
105      */
106     public DensityUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
107             final DensityUnit referenceUnit, final double scaleFactorToReferenceUnit)
108     {
109         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
110     }
111 
112     /**
113      * @return massUnit
114      */
115     public final MassUnit getMassUnit()
116     {
117         return this.massUnit;
118     }
119 
120     /**
121      * @return lengthUnit
122      */
123     public final LengthUnit getLengthUnit()
124     {
125         return this.lengthUnit;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public final DensityUnit getStandardUnit()
131     {
132         return KG_PER_METER_3;
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public final String getSICoefficientsString()
138     {
139         return "kg/m3";
140     }
141 
142 }