View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.DensityUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.unit.FlowMassUnit;
9   import org.djunits.unit.ForceUnit;
10  import org.djunits.unit.MassUnit;
11  import org.djunits.unit.MomentumUnit;
12  import org.djunits.unit.VolumeUnit;
13  import org.djunits.unit.si.SIPrefixes;
14  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
15  import org.djutils.base.NumberParser;
16  import org.djutils.exceptions.Throw;
17  
18  import jakarta.annotation.Generated;
19  
20  /**
21   * Easy access methods for the Mass DoubleScalar, which is relative by definition.
22   * <p>
23   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
25   * </p>
26   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
28   */
29  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
30  public class Mass extends DoubleScalarRel<MassUnit, Mass>
31  {
32      /** */
33      private static final long serialVersionUID = 20150905L;
34  
35      /** Constant with value zero. */
36      public static final Mass ZERO = new Mass(0.0, MassUnit.SI);
37  
38      /** Constant with value one. */
39      public static final Mass ONE = new Mass(1.0, MassUnit.SI);
40  
41      /** Constant with value NaN. */
42      @SuppressWarnings("checkstyle:constantname")
43      public static final Mass NaN = new Mass(Double.NaN, MassUnit.SI);
44  
45      /** Constant with value POSITIVE_INFINITY. */
46      public static final Mass POSITIVE_INFINITY = new Mass(Double.POSITIVE_INFINITY, MassUnit.SI);
47  
48      /** Constant with value NEGATIVE_INFINITY. */
49      public static final Mass NEGATIVE_INFINITY = new Mass(Double.NEGATIVE_INFINITY, MassUnit.SI);
50  
51      /** Constant with value MAX_VALUE. */
52      public static final Mass POS_MAXVALUE = new Mass(Double.MAX_VALUE, MassUnit.SI);
53  
54      /** Constant with value -MAX_VALUE. */
55      public static final Mass NEG_MAXVALUE = new Mass(-Double.MAX_VALUE, MassUnit.SI);
56  
57      /**
58       * Construct Mass scalar with a unit.
59       * @param value the double value, expressed in the given unit
60       * @param unit unit for the double value
61       */
62      public Mass(final double value, final MassUnit unit)
63      {
64          super(value, unit);
65      }
66  
67      /**
68       * Construct Mass scalar.
69       * @param value Scalar from which to construct this instance
70       */
71      public Mass(final Mass value)
72      {
73          super(value);
74      }
75  
76      @Override
77      public final Mass instantiateRel(final double value, final MassUnit unit)
78      {
79          return new Mass(value, unit);
80      }
81  
82      /**
83       * Construct Mass scalar based on an SI value.
84       * @param value the double value in SI units
85       * @return the new scalar with the SI value
86       */
87      public static final Mass ofSI(final double value)
88      {
89          return new Mass(value, MassUnit.SI);
90      }
91  
92      /**
93       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
94       * @param zero the value at a ratio of zero
95       * @param one the value at a ratio of one
96       * @param ratio the ratio between 0 and 1, inclusive
97       * @return a Mass at the given ratio between 0 and 1
98       */
99      public static Mass interpolate(final Mass zero, final Mass one, final double ratio)
100     {
101         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
102                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
103         return new Mass(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
104     }
105 
106     /**
107      * Return the maximum value of two relative scalars.
108      * @param r1 the first scalar
109      * @param r2 the second scalar
110      * @return the maximum value of two relative scalars
111      */
112     public static Mass max(final Mass r1, final Mass r2)
113     {
114         return r1.gt(r2) ? r1 : r2;
115     }
116 
117     /**
118      * Return the maximum value of more than two relative scalars.
119      * @param r1 the first scalar
120      * @param r2 the second scalar
121      * @param rn the other scalars
122      * @return the maximum value of more than two relative scalars
123      */
124     public static Mass max(final Mass r1, final Mass r2, final Mass... rn)
125     {
126         Mass maxr = r1.gt(r2) ? r1 : r2;
127         for (Mass r : rn)
128         {
129             if (r.gt(maxr))
130             {
131                 maxr = r;
132             }
133         }
134         return maxr;
135     }
136 
137     /**
138      * Return the minimum value of two relative scalars.
139      * @param r1 the first scalar
140      * @param r2 the second scalar
141      * @return the minimum value of two relative scalars
142      */
143     public static Mass min(final Mass r1, final Mass r2)
144     {
145         return r1.lt(r2) ? r1 : r2;
146     }
147 
148     /**
149      * Return the minimum value of more than two relative scalars.
150      * @param r1 the first scalar
151      * @param r2 the second scalar
152      * @param rn the other scalars
153      * @return the minimum value of more than two relative scalars
154      */
155     public static Mass min(final Mass r1, final Mass r2, final Mass... rn)
156     {
157         Mass minr = r1.lt(r2) ? r1 : r2;
158         for (Mass r : rn)
159         {
160             if (r.lt(minr))
161             {
162                 minr = r;
163             }
164         }
165         return minr;
166     }
167 
168     /**
169      * Returns a Mass representation of a textual representation of a value with a unit. The String representation that can be
170      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
171      * but not required, between the value and the unit.
172      * @param text the textual representation to parse into a Mass
173      * @return the Scalar representation of the value in its unit
174      * @throws IllegalArgumentException when the text cannot be parsed
175      * @throws NullPointerException when the text argument is null
176      */
177     public static Mass valueOf(final String text)
178     {
179         Throw.whenNull(text, "Error parsing Mass: text to parse is null");
180         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Mass: empty text to parse");
181         try
182         {
183             NumberParser numberParser = new NumberParser().lenient().trailing();
184             double d = numberParser.parseDouble(text);
185             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
186             MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
187             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Mass", unitString);
188             return new Mass(d, unit);
189         }
190         catch (Exception exception)
191         {
192             throw new IllegalArgumentException(
193                     "Error parsing Mass from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
194                     exception);
195         }
196     }
197 
198     /**
199      * Returns a Mass based on a value and the textual representation of the unit, which can be localized.
200      * @param value the value to use
201      * @param unitString the textual representation of the unit
202      * @return the Scalar representation of the value in its unit
203      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
204      * @throws NullPointerException when the unitString argument is null
205      */
206     public static Mass of(final double value, final String unitString)
207     {
208         Throw.whenNull(unitString, "Error parsing Mass: unitString is null");
209         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Mass: empty unitString");
210         MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
211         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Mass with unit %s", unitString);
212         return new Mass(value, unit);
213     }
214 
215     @Override
216     public String toStringSIPrefixed(final int smallestPower, final int biggestPower)
217     {
218         if (!Double.isFinite(this.si))
219         {
220             return toString(getDisplayUnit().getStandardUnit());
221         }
222         // PK: I can't think of an easier way to figure out what the exponent will be; rounding of the mantissa to the available
223         // width makes this hard; This feels like an expensive way.
224         String check = String.format(this.si >= 0 ? "%10.8E" : "%10.7E", this.si);
225         int exponent = Integer.parseInt(check.substring(check.indexOf("E") + 1));
226         if (exponent < -27 || exponent < smallestPower || exponent > 21 + 2 || exponent > biggestPower)
227         {
228             // Out of SI prefix range; do not scale.
229             return String.format(this.si >= 0 ? "%10.4E" : "%10.3E", this.si) + " "
230                     + getDisplayUnit().getStandardUnit().getId();
231         }
232         Integer roundedExponent = (int) Math.ceil((exponent - 2.0) / 3) * 3 + 3;
233         // System.out.print(String.format("exponent=%d; roundedExponent=%d ", exponent, roundedExponent));
234         String key = SIPrefixes.FACTORS.get(roundedExponent).getDefaultTextualPrefix() + "g";
235         MassUnit displayUnit = getDisplayUnit().getQuantity().getUnitByAbbreviation(key);
236         return toString(displayUnit);
237     }
238 
239     /**
240      * Calculate the division of Mass and Mass, which results in a Dimensionless scalar.
241      * @param v scalar
242      * @return scalar as a division of Mass and Mass
243      */
244     public final Dimensionless divide(final Mass v)
245     {
246         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
247     }
248 
249     /**
250      * Calculate the division of Mass and FlowMass, which results in a Duration scalar.
251      * @param v scalar
252      * @return scalar as a division of Mass and FlowMass
253      */
254     public final Duration divide(final FlowMass v)
255     {
256         return new Duration(this.si / v.si, DurationUnit.SI);
257     }
258 
259     /**
260      * Calculate the division of Mass and Duration, which results in a FlowMass scalar.
261      * @param v scalar
262      * @return scalar as a division of Mass and Duration
263      */
264     public final FlowMass divide(final Duration v)
265     {
266         return new FlowMass(this.si / v.si, FlowMassUnit.SI);
267     }
268 
269     /**
270      * Calculate the multiplication of Mass and Acceleration, which results in a Force scalar.
271      * @param v scalar
272      * @return scalar as a multiplication of Mass and Acceleration
273      */
274     public final Force times(final Acceleration v)
275     {
276         return new Force(this.si * v.si, ForceUnit.SI);
277     }
278 
279     /**
280      * Calculate the multiplication of Mass and Frequency, which results in a FlowMass scalar.
281      * @param v scalar
282      * @return scalar as a multiplication of Mass and Frequency
283      */
284     public final FlowMass times(final Frequency v)
285     {
286         return new FlowMass(this.si * v.si, FlowMassUnit.SI);
287     }
288 
289     /**
290      * Calculate the division of Mass and Density, which results in a Volume scalar.
291      * @param v scalar
292      * @return scalar as a division of Mass and Density
293      */
294     public final Volume divide(final Density v)
295     {
296         return new Volume(this.si / v.si, VolumeUnit.SI);
297     }
298 
299     /**
300      * Calculate the division of Mass and Volume, which results in a Density scalar.
301      * @param v scalar
302      * @return scalar as a division of Mass and Volume
303      */
304     public final Density divide(final Volume v)
305     {
306         return new Density(this.si / v.si, DensityUnit.SI);
307     }
308 
309     /**
310      * Calculate the multiplication of Mass and Speed, which results in a Momentum scalar.
311      * @param v scalar
312      * @return scalar as a multiplication of Mass and Speed
313      */
314     public final Momentum times(final Speed v)
315     {
316         return new Momentum(this.si * v.si, MomentumUnit.SI);
317     }
318 
319     @Override
320     public SIScalar reciprocal()
321     {
322         return SIScalar.divide(Dimensionless.ONE, this);
323     }
324 
325     /**
326      * Multiply two scalars that result in a scalar of type Mass.
327      * @param scalar1 the first scalar
328      * @param scalar2 the second scalar
329      * @return the multiplication of both scalars as an instance of Mass
330      */
331     public static Mass multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
332     {
333         Throw.whenNull(scalar1, "scalar1 cannot be null");
334         Throw.whenNull(scalar2, "scalar2 cannot be null");
335         Throw.when(
336                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
337                         .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(MassUnit.BASE.getSiDimensions()),
338                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Mass",
339                 scalar1.toDisplayString(), scalar2.toDisplayString());
340         return new Mass(scalar1.si * scalar2.si, MassUnit.SI);
341     }
342 
343     /**
344      * Divide two scalars that result in a scalar of type Mass.
345      * @param scalar1 the first scalar
346      * @param scalar2 the second scalar
347      * @return the division of scalar1 by scalar2 as an instance of Mass
348      */
349     public static Mass divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
350     {
351         Throw.whenNull(scalar1, "scalar1 cannot be null");
352         Throw.whenNull(scalar2, "scalar2 cannot be null");
353         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
354                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(MassUnit.BASE.getSiDimensions()),
355                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Mass",
356                 scalar1.toDisplayString(), scalar2.toDisplayString());
357         return new Mass(scalar1.si / scalar2.si, MassUnit.SI);
358     }
359 
360 }