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