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