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