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