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