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 * Mass is the amount of matter in an object, measured in kilograms (kg).
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 Mass extends Quantity<Mass>
26 {
27 /** Constant with value zero. */
28 public static final Mass ZERO = ofSi(0.0);
29
30 /** Constant with value one. */
31 public static final Mass ONE = ofSi(1.0);
32
33 /** Constant with value NaN. */
34 @SuppressWarnings("checkstyle:constantname")
35 public static final Mass NaN = ofSi(Double.NaN);
36
37 /** Constant with value POSITIVE_INFINITY. */
38 public static final Mass POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
39
40 /** Constant with value NEGATIVE_INFINITY. */
41 public static final Mass NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
42
43 /** Constant with value MAX_VALUE. */
44 public static final Mass POS_MAXVALUE = ofSi(Double.MAX_VALUE);
45
46 /** Constant with value -MAX_VALUE. */
47 public static final Mass NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
48
49 /** */
50 private static final long serialVersionUID = 600L;
51
52 /**
53 * Instantiate a Mass 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 Mass(final double value, final Mass.Unit displayUnit, final boolean useSi)
59 {
60 super(value, displayUnit, useSi);
61 }
62
63 /**
64 * Instantiate a Mass 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 Mass(final double valueInUnit, final Mass.Unit unit)
69 {
70 this(valueInUnit, unit, false);
71 }
72
73 /**
74 * Return a Mass instance based on an SI value.
75 * @param si the si value
76 * @return the Mass instance based on an SI value
77 */
78 public static Mass ofSi(final double si)
79 {
80 return new Mass(si, Mass.Unit.SI, true);
81 }
82
83 /**
84 * Instantiate a Mass 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 Mass instance based on an SI value with the given display unit
88 */
89 public static Mass ofSi(final double siValue, final Mass.Unit displayUnit)
90 {
91 return new Mass(siValue, displayUnit, true);
92 }
93
94 @Override
95 public Mass instantiateSi(final double siValue, final UnitInterface<Mass> displayUnit)
96 {
97 return new Mass(siValue, (Mass.Unit) displayUnit, true);
98 }
99
100 /**
101 * Returns a Mass representation of a textual representation of a value with a unit. The String representation that can be
102 * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
103 * but not required, between the value and the unit.
104 * @param text the textual representation to parse into a Mass
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 Mass valueOf(final String text)
110 {
111 return Quantity.valueOf(text, ZERO);
112 }
113
114 /**
115 * Returns a Mass 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 Mass representation of the value in its unit
119 */
120 public static Mass of(final double valueInUnit, final Mass.Unit unit)
121 {
122 return new Mass(valueInUnit, unit);
123 }
124
125 /**
126 * Returns a Mass 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 Mass of(final double valueInUnit, final String unitString)
134 {
135 return Quantity.of(valueInUnit, unitString, ZERO);
136 }
137
138 @Override
139 public Mass.Unit getDisplayUnit()
140 {
141 return (Mass.Unit) super.getDisplayUnit();
142 }
143
144 /**
145 * Calculate the division of Mass and Mass, which results in a Dimensionless quantity.
146 * @param v quantity
147 * @return quantity as a division of Mass and Mass
148 */
149 public Dimensionless divide(final Mass v)
150 {
151 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
152 }
153
154 /**
155 * Calculate the division of Mass and FlowMass, which results in a Duration scalar.
156 * @param v scalar
157 * @return scalar as a division of Mass and FlowMass
158 */
159 public Duration divide(final FlowMass v)
160 {
161 return new Duration(this.si() / v.si(), Duration.Unit.SI);
162 }
163
164 /**
165 * Calculate the division of Mass and Duration, which results in a FlowMass scalar.
166 * @param v scalar
167 * @return scalar as a division of Mass and Duration
168 */
169 public FlowMass divide(final Duration v)
170 {
171 return new FlowMass(this.si() / v.si(), FlowMass.Unit.SI);
172 }
173
174 /**
175 * Calculate the multiplication of Mass and Acceleration, which results in a Force scalar.
176 * @param v scalar
177 * @return scalar as a multiplication of Mass and Acceleration
178 */
179 public Force multiply(final Acceleration v)
180 {
181 return new Force(this.si() * v.si(), Force.Unit.SI);
182 }
183
184 /**
185 * Calculate the multiplication of Mass and Frequency, which results in a FlowMass scalar.
186 * @param v scalar
187 * @return scalar as a multiplication of Mass and Frequency
188 */
189 public FlowMass multiply(final Frequency v)
190 {
191 return new FlowMass(this.si() * v.si(), FlowMass.Unit.SI);
192 }
193
194 /**
195 * Calculate the division of Mass and Density, which results in a Volume scalar.
196 * @param v scalar
197 * @return scalar as a division of Mass and Density
198 */
199 public Volume divide(final Density v)
200 {
201 return new Volume(this.si() / v.si(), Volume.Unit.SI);
202 }
203
204 /**
205 * Calculate the division of Mass and Volume, which results in a Density scalar.
206 * @param v scalar
207 * @return scalar as a division of Mass and Volume
208 */
209 public Density divide(final Volume v)
210 {
211 return new Density(this.si() / v.si(), Density.Unit.SI);
212 }
213
214 /**
215 * Calculate the multiplication of Mass and Speed, which results in a Momentum scalar.
216 * @param v scalar
217 * @return scalar as a multiplication of Mass and Speed
218 */
219 public Momentum multiply(final Speed v)
220 {
221 return new Momentum(this.si() * v.si(), Momentum.Unit.SI);
222 }
223
224 /******************************************************************************************************/
225 /********************************************** UNIT CLASS ********************************************/
226 /******************************************************************************************************/
227
228 /**
229 * Mass.Unit encodes the unit of the amount of matter in an object.
230 * <p>
231 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
232 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
233 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
234 * @author Alexander Verbraeck
235 */
236 @SuppressWarnings("checkstyle:constantname")
237 public static class Unit extends AbstractUnit<Mass>
238 {
239 /** Constant for pound (lb). */
240 public static final double CONST_LB = 0.45359237;
241
242 /** Constant for ounce. */
243 public static final double CONST_OUNCE = CONST_LB / 16.0;
244
245 /** Constant for short ton. */
246 public static final double CONST_TON_SHORT = 2000.0 * CONST_LB;
247
248 /** Constant for long ton. */
249 public static final double CONST_TON_LONG = 2240.0 * CONST_LB;
250
251 /** The dimensions of mass: kg. */
252 public static final SIUnit SI_UNIT = SIUnit.of("kg");
253
254 /** kilogram. */
255 public static final Mass.Unit kg =
256 new Mass.Unit("kg", "kg", "kilogram", IdentityScale.SCALE, UnitSystem.SI_BASE, SIPrefixes.getSiPrefixKilo("k"));
257
258 /** The SI or BASE unit. */
259 public static final Mass.Unit SI = (Unit) kg.generateSiPrefixes(true, false);
260
261 /** gram. */
262 public static final Mass.Unit g = Units.resolve(Mass.Unit.class, "g");
263
264 /** microgram. */
265 public static final Mass.Unit mug = Units.resolve(Mass.Unit.class, "mug");
266
267 /** milligram. */
268 public static final Mass.Unit mg = Units.resolve(Mass.Unit.class, "mg");
269
270 /** pound. */
271 public static final Mass.Unit lb = kg.deriveUnit("lb", "pound", CONST_LB, UnitSystem.IMPERIAL);
272
273 /** ounce. */
274 public static final Mass.Unit oz = kg.deriveUnit("oz", "ounce", CONST_OUNCE, UnitSystem.IMPERIAL);
275
276 /** long ton = 2240 lb. */
277 public static final Mass.Unit long_tn = kg.deriveUnit("long tn", "long ton", CONST_TON_LONG, UnitSystem.IMPERIAL);
278
279 /** short ton = 2000 lb. */
280 public static final Mass.Unit sh_tn = kg.deriveUnit("sh tn", "short ton", CONST_TON_SHORT, UnitSystem.US_CUSTOMARY);
281
282 /** metric ton = 1000 kg. */
283 public static final Mass.Unit t = kg.deriveUnit("t", "metric tonne", 1000.0, UnitSystem.SI_ACCEPTED);
284
285 /** metric ton = 1000 kg. */
286 public static final Mass.Unit t_mts = kg.deriveUnit("t(mts)", "tonne", 1000.0, UnitSystem.MTS);
287
288 /** Dalton, according to CODATA 2018. */
289 public static final Mass.Unit Da = kg.deriveUnit("Da", "Dalton", 1.66053906660E-27, UnitSystem.SI_ACCEPTED);
290
291 /** electronvolt = 1.782661907E-36 kg. See http://physics.nist.gov/cuu/Constants/Table/allascii.txt. */
292 public static final Mass.Unit eV = kg.deriveUnit("eV", "electronvolt", 1.782661907E-36, UnitSystem.OTHER);
293
294 /** microelectronvolt. */
295 public static final Mass.Unit mueV =
296 eV.deriveUnit("mueV", "\u03BCeV", "microelectronvolt", 1E-6, UnitSystem.OTHER, null);
297
298 /** millielectronvolt (note, no dash between milli and electron; the SI style guide forbids spaces or hyphens). */
299 public static final Mass.Unit meV = eV.deriveUnit("meV", "millielectronvolt", 1E-3, UnitSystem.OTHER);
300
301 /** kiloelectronvolt. */
302 public static final Mass.Unit keV = eV.deriveUnit("keV", "kiloelectronvolt", 1E3, UnitSystem.OTHER);
303
304 /** megaelectronvolt. */
305 public static final Mass.Unit MeV = eV.deriveUnit("MeV", "megaelectronvolt", 1E6, UnitSystem.OTHER);
306
307 /** gigaelectronvolt. */
308 public static final Mass.Unit GeV = eV.deriveUnit("GeV", "gigaelectronvolt", 1E9, UnitSystem.OTHER);
309
310 /**
311 * Create a new Mass unit.
312 * @param id the id or main abbreviation of the unit
313 * @param name the full name of the unit
314 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
315 * @param unitSystem the unit system such as SI or IMPERIAL
316 */
317 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
318 {
319 super(id, name, scaleFactorToBaseUnit, unitSystem);
320 }
321
322 /**
323 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
324 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
325 * @param displayAbbreviation the display abbreviation of the unit
326 * @param name the full name of the unit
327 * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
328 * @param unitSystem unit system, e.g. SI or Imperial
329 * @param siPrefix the SI Prefix of this unit
330 */
331 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
332 final UnitSystem unitSystem, final SIPrefix siPrefix)
333 {
334 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
335 }
336
337 @Override
338 public SIUnit siUnit()
339 {
340 return SI_UNIT;
341 }
342
343 @Override
344 public Unit getBaseUnit()
345 {
346 return SI;
347 }
348
349 @Override
350 public Mass ofSi(final double si, final UnitInterface<Mass> displayUnit)
351 {
352 return new Mass(si, (Unit) displayUnit, true);
353 }
354
355 @Override
356 public Mass.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
357 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
358 {
359 if (getScale() instanceof LinearScale ls)
360 {
361 return new Mass.Unit(textualAbbreviation, displayAbbreviation, name,
362 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
363 }
364 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
365 }
366
367 @Override
368 public Mass.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
369 final UnitSystem unitSystem)
370 {
371 return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
372 }
373
374 }
375
376 }