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