View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.AbstractUnit;
5   import org.djunits.unit.UnitRuntimeException;
6   import org.djunits.unit.Unitless;
7   import org.djunits.unit.Units;
8   import org.djunits.unit.scale.LinearScale;
9   import org.djunits.unit.scale.Scale;
10  import org.djunits.unit.si.SIUnit;
11  import org.djunits.unit.system.UnitSystem;
12  
13  /**
14   * Density is mass per unit volume of a substance, measured in kilograms per cubic meter (kg/m3).
15   * <p>
16   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
18   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
19   * @author Alexander Verbraeck
20   */
21  public class Density extends Quantity<Density, Density.Unit>
22  {
23      /** Constant with value zero. */
24      public static final Density ZERO = Density.ofSi(0.0);
25  
26      /** Constant with value one. */
27      public static final Density ONE = Density.ofSi(1.0);
28  
29      /** Constant with value NaN. */
30      @SuppressWarnings("checkstyle:constantname")
31      public static final Density NaN = Density.ofSi(Double.NaN);
32  
33      /** Constant with value POSITIVE_INFINITY. */
34      public static final Density POSITIVE_INFINITY = Density.ofSi(Double.POSITIVE_INFINITY);
35  
36      /** Constant with value NEGATIVE_INFINITY. */
37      public static final Density NEGATIVE_INFINITY = Density.ofSi(Double.NEGATIVE_INFINITY);
38  
39      /** Constant with value MAX_VALUE. */
40      public static final Density POS_MAXVALUE = Density.ofSi(Double.MAX_VALUE);
41  
42      /** Constant with value -MAX_VALUE. */
43      public static final Density NEG_MAXVALUE = Density.ofSi(-Double.MAX_VALUE);
44  
45      /** */
46      private static final long serialVersionUID = 600L;
47  
48      /**
49       * Instantiate a Density quantity with a unit.
50       * @param value the value, expressed in the unit
51       * @param unit the unit in which the value is expressed
52       */
53      public Density(final double value, final Density.Unit unit)
54      {
55          super(value, unit);
56      }
57  
58      /**
59       * Instantiate a Density quantity with a unit, expressed as a String.
60       * @param value the value, expressed in the unit
61       * @param abbreviation the String abbreviation of the unit in which the value is expressed
62       */
63      public Density(final double value, final String abbreviation)
64      {
65          this(value, Units.resolve(Density.Unit.class, abbreviation));
66      }
67  
68      /**
69       * Construct Density quantity.
70       * @param value Scalar from which to construct this instance
71       */
72      public Density(final Density value)
73      {
74          super(value.si(), Density.Unit.SI);
75          setDisplayUnit(value.getDisplayUnit());
76      }
77  
78      /**
79       * Return a Density instance based on an SI value.
80       * @param si the si value
81       * @return the Density instance based on an SI value
82       */
83      public static Density ofSi(final double si)
84      {
85          return new Density(si, Density.Unit.SI);
86      }
87  
88      @Override
89      public Density instantiate(final double si)
90      {
91          return ofSi(si);
92      }
93  
94      @Override
95      public SIUnit siUnit()
96      {
97          return Density.Unit.SI_UNIT;
98      }
99  
100     /**
101      * Returns a Density representation of a textual representation of a value with a unit. The String representation that can
102      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
103      * allowed, but not required, between the value and the unit.
104      * @param text the textual representation to parse into a Density
105      * @return the Scalar representation of the value in its unit
106      * @throws IllegalArgumentException when the text cannot be parsed
107      * @throws NullPointerException when the text argument is null
108      */
109     public static Density valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a Density based on a value and the textual representation of the unit, which can be localized.
116      * @param value the value to use
117      * @param unitString the textual representation of the unit
118      * @return the Scalar representation of the value in its unit
119      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
120      * @throws NullPointerException when the unitString argument is null
121      */
122     public static Density of(final double value, final String unitString)
123     {
124         return Quantity.of(value, unitString, ZERO);
125     }
126 
127     /**
128      * Calculate the division of Density and Density, which results in a Dimensionless quantity.
129      * @param v quantity
130      * @return quantity as a division of Density and Density
131      */
132     public final Dimensionless divide(final Density v)
133     {
134         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
135     }
136 
137     /**
138      * Calculate the multiplication of Density and Volume, which results in a Mass scalar.
139      * @param v scalar
140      * @return scalar as a multiplication of Density and Volume
141      */
142     public final Mass multiply(final Volume v)
143     {
144         return new Mass(this.si() * v.si(), Mass.Unit.SI);
145     }
146 
147     /**
148      * Calculate the multiplication of Density and FlowVolume, which results in a FlowMass scalar.
149      * @param v scalar
150      * @return scalar as a multiplication of Density and FlowVolume
151      */
152     public final FlowMass multiply(final FlowVolume v)
153     {
154         return new FlowMass(this.si() * v.si(), FlowMass.Unit.SI);
155     }
156 
157     /******************************************************************************************************/
158     /********************************************** UNIT CLASS ********************************************/
159     /******************************************************************************************************/
160 
161     /**
162      * Density.Unit encodes the units of density based on mass per volume.
163      * <p>
164      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
165      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
166      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
167      * @author Alexander Verbraeck
168      */
169     @SuppressWarnings("checkstyle:constantname")
170     public static class Unit extends AbstractUnit<Density.Unit, Density>
171     {
172         /** The dimensions of the absorbed dose: kg/m3. */
173         public static final SIUnit SI_UNIT = SIUnit.of("kg/m3");
174 
175         /** kg/m^3. */
176         public static final Density.Unit kg_m3 =
177                 new Density.Unit("kg/m3", "kilogram per cubic meter", 1.0, UnitSystem.SI_DERIVED);
178 
179         /** The SI or BASE unit. */
180         public static final Density.Unit SI = kg_m3;
181 
182         /** g/cm^3. */
183         public static final Density.Unit g_cm3 =
184                 kg_m3.deriveUnit("g/cm3", "gram per cubic centimeter", 1.0E3, UnitSystem.SI_DERIVED);
185 
186         /**
187          * Create a new Density unit.
188          * @param id the id or main abbreviation of the unit
189          * @param name the full name of the unit
190          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
191          * @param unitSystem the unit system such as SI or IMPERIAL
192          */
193         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
194         {
195             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
196         }
197 
198         /**
199          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
200          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
201          * @param displayAbbreviation the display abbreviation of the unit
202          * @param name the full name of the unit
203          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
204          * @param unitSystem unit system, e.g. SI or Imperial
205          */
206         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
207                 final UnitSystem unitSystem)
208         {
209             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
210         }
211 
212         @Override
213         public SIUnit siUnit()
214         {
215             return SI_UNIT;
216         }
217 
218         @Override
219         public Unit getBaseUnit()
220         {
221             return SI;
222         }
223 
224         @Override
225         public Density ofSi(final double si)
226         {
227             return Density.ofSi(si);
228         }
229 
230         @Override
231         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
232                 final double scaleFactor, final UnitSystem unitSystem)
233         {
234             if (getScale() instanceof LinearScale ls)
235             {
236                 return new Density.Unit(textualAbbreviation, displayAbbreviation, name,
237                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
238             }
239             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
240         }
241 
242     }
243 }