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