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