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.AbstractFloatScalarRel;
15  import org.djunits.value.vfloat.scalar.base.FloatScalar;
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-2023 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-04-30T13:59:27.633664900Z")
31  public class FloatMass extends AbstractFloatScalarRel<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      /** {@inheritDoc} */
88      @Override
89      public final FloatMass instantiateRel(final float value, final MassUnit unit)
90      {
91          return new FloatMass(value, unit);
92      }
93  
94      /**
95       * Construct FloatMass scalar.
96       * @param value float; the float value in SI units
97       * @return the new scalar with the SI value
98       */
99      public static final FloatMass instantiateSI(final float value)
100     {
101         return new FloatMass(value, MassUnit.SI);
102     }
103 
104     /**
105      * Interpolate between two values.
106      * @param zero the low value
107      * @param one the high value
108      * @param ratio double; the ratio between 0 and 1, inclusive
109      * @return a Scalar at the ratio between
110      */
111     public static FloatMass interpolate(final FloatMass zero, final FloatMass one, final float ratio)
112     {
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 String; the textual representation to parse into a FloatMass
184      * @return FloatMass; 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             if (unit == null)
199                 throw new IllegalArgumentException("Unit " + unitString + " not found");
200             return new FloatMass(f, unit);
201         }
202         catch (Exception exception)
203         {
204             throw new IllegalArgumentException(
205                     "Error parsing FloatMass from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
206                     exception);
207         }
208     }
209 
210     /**
211      * Returns a FloatMass based on a value and the textual representation of the unit, which can be localized.
212      * @param value double; the value to use
213      * @param unitString String; the textual representation of the unit
214      * @return FloatMass; the Scalar representation of the value in its unit
215      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
216      * @throws NullPointerException when the unitString argument is null
217      */
218     public static FloatMass of(final float value, final String unitString)
219     {
220         Throw.whenNull(unitString, "Error parsing FloatMass: unitString is null");
221         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatMass: empty unitString");
222         MassUnit unit = MassUnit.BASE.getUnitByAbbreviation(unitString);
223         if (unit != null)
224         {
225             return new FloatMass(value, unit);
226         }
227         throw new IllegalArgumentException("Error parsing FloatMass with unit " + unitString);
228     }
229 
230     /** {@inheritDoc} */
231     @Override
232     public String toStringSIPrefixed(final int smallestPower, final int biggestPower)
233     {
234         if (!Float.isFinite(this.si))
235         {
236             return toString(getDisplayUnit().getStandardUnit());
237         }
238         // PK: I can't think of an easier way to figure out what the exponent will be; rounding of the mantissa to the available
239         // width makes this hard; This feels like an expensive way.
240         String check = String.format(this.si >= 0 ? "%10.8E" : "%10.7E", this.si);
241         int exponent = Integer.parseInt(check.substring(check.indexOf("E") + 1));
242         if (exponent < -27 || exponent < smallestPower || exponent > 21 + 2 || exponent > biggestPower)
243         {
244             // Out of SI prefix range; do not scale.
245             return String.format(this.si >= 0 ? "%10.4E" : "%10.3E", this.si) + " "
246                     + getDisplayUnit().getStandardUnit().getId();
247         }
248         Integer roundedExponent = (int) Math.ceil((exponent - 2.0) / 3) * 3 + 3;
249         // System.out.print(String.format("exponent=%d; roundedExponent=%d ", exponent, roundedExponent));
250         String key = SIPrefixes.FACTORS.get(roundedExponent).getDefaultTextualPrefix() + "g";
251         MassUnit displayUnit = getDisplayUnit().getQuantity().getUnitByAbbreviation(key);
252         return toString(displayUnit);
253     }
254 
255     /**
256      * Calculate the division of FloatMass and FloatMass, which results in a FloatDimensionless scalar.
257      * @param v FloatMass; scalar
258      * @return FloatDimensionless; scalar as a division of FloatMass and FloatMass
259      */
260     public final FloatDimensionless divide(final FloatMass v)
261     {
262         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
263     }
264 
265     /**
266      * Calculate the division of FloatMass and FloatFlowMass, which results in a FloatDuration scalar.
267      * @param v FloatMass; scalar
268      * @return FloatDuration; scalar as a division of FloatMass and FloatFlowMass
269      */
270     public final FloatDuration divide(final FloatFlowMass v)
271     {
272         return new FloatDuration(this.si / v.si, DurationUnit.SI);
273     }
274 
275     /**
276      * Calculate the division of FloatMass and FloatDuration, which results in a FloatFlowMass scalar.
277      * @param v FloatMass; scalar
278      * @return FloatFlowMass; scalar as a division of FloatMass and FloatDuration
279      */
280     public final FloatFlowMass divide(final FloatDuration v)
281     {
282         return new FloatFlowMass(this.si / v.si, FlowMassUnit.SI);
283     }
284 
285     /**
286      * Calculate the multiplication of FloatMass and FloatAcceleration, which results in a FloatForce scalar.
287      * @param v FloatMass; scalar
288      * @return FloatForce; scalar as a multiplication of FloatMass and FloatAcceleration
289      */
290     public final FloatForce times(final FloatAcceleration v)
291     {
292         return new FloatForce(this.si * v.si, ForceUnit.SI);
293     }
294 
295     /**
296      * Calculate the multiplication of FloatMass and FloatFrequency, which results in a FloatFlowMass scalar.
297      * @param v FloatMass; scalar
298      * @return FloatFlowMass; scalar as a multiplication of FloatMass and FloatFrequency
299      */
300     public final FloatFlowMass times(final FloatFrequency v)
301     {
302         return new FloatFlowMass(this.si * v.si, FlowMassUnit.SI);
303     }
304 
305     /**
306      * Calculate the division of FloatMass and FloatDensity, which results in a FloatVolume scalar.
307      * @param v FloatMass; scalar
308      * @return FloatVolume; scalar as a division of FloatMass and FloatDensity
309      */
310     public final FloatVolume divide(final FloatDensity v)
311     {
312         return new FloatVolume(this.si / v.si, VolumeUnit.SI);
313     }
314 
315     /**
316      * Calculate the division of FloatMass and FloatVolume, which results in a FloatDensity scalar.
317      * @param v FloatMass; scalar
318      * @return FloatDensity; scalar as a division of FloatMass and FloatVolume
319      */
320     public final FloatDensity divide(final FloatVolume v)
321     {
322         return new FloatDensity(this.si / v.si, DensityUnit.SI);
323     }
324 
325     /**
326      * Calculate the multiplication of FloatMass and FloatSpeed, which results in a FloatMomentum scalar.
327      * @param v FloatMass; scalar
328      * @return FloatMomentum; scalar as a multiplication of FloatMass and FloatSpeed
329      */
330     public final FloatMomentum times(final FloatSpeed v)
331     {
332         return new FloatMomentum(this.si * v.si, MomentumUnit.SI);
333     }
334 
335     /** {@inheritDoc} */
336     @Override
337     public FloatSIScalar reciprocal()
338     {
339         return FloatScalar.divide(FloatDimensionless.ONE, this);
340     }
341 
342 }