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