View Javadoc
1   package org.djunits.value.vdouble.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.vdouble.scalar.base.DoubleScalarRel;
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 LinearDensity DoubleScalar, 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 LinearDensity extends DoubleScalarRel<LinearDensityUnit, LinearDensity>
27  {
28      /** */
29      private static final long serialVersionUID = 20150905L;
30  
31      /** Constant with value zero. */
32      public static final LinearDensity ZERO = new LinearDensity(0.0, LinearDensityUnit.SI);
33  
34      /** Constant with value one. */
35      public static final LinearDensity ONE = new LinearDensity(1.0, LinearDensityUnit.SI);
36  
37      /** Constant with value NaN. */
38      @SuppressWarnings("checkstyle:constantname")
39      public static final LinearDensity NaN = new LinearDensity(Double.NaN, LinearDensityUnit.SI);
40  
41      /** Constant with value POSITIVE_INFINITY. */
42      public static final LinearDensity POSITIVE_INFINITY = new LinearDensity(Double.POSITIVE_INFINITY, LinearDensityUnit.SI);
43  
44      /** Constant with value NEGATIVE_INFINITY. */
45      public static final LinearDensity NEGATIVE_INFINITY = new LinearDensity(Double.NEGATIVE_INFINITY, LinearDensityUnit.SI);
46  
47      /** Constant with value MAX_VALUE. */
48      public static final LinearDensity POS_MAXVALUE = new LinearDensity(Double.MAX_VALUE, LinearDensityUnit.SI);
49  
50      /** Constant with value -MAX_VALUE. */
51      public static final LinearDensity NEG_MAXVALUE = new LinearDensity(-Double.MAX_VALUE, LinearDensityUnit.SI);
52  
53      /**
54       * Construct LinearDensity scalar with a unit.
55       * @param value the double value, expressed in the given unit
56       * @param unit unit for the double value
57       */
58      public LinearDensity(final double value, final LinearDensityUnit unit)
59      {
60          super(value, unit);
61      }
62  
63      /**
64       * Construct LinearDensity scalar.
65       * @param value Scalar from which to construct this instance
66       */
67      public LinearDensity(final LinearDensity value)
68      {
69          super(value);
70      }
71  
72      @Override
73      public final LinearDensity instantiateRel(final double value, final LinearDensityUnit unit)
74      {
75          return new LinearDensity(value, unit);
76      }
77  
78      /**
79       * Construct LinearDensity scalar based on an SI value.
80       * @param value the double value in SI units
81       * @return the new scalar with the SI value
82       */
83      public static final LinearDensity ofSI(final double value)
84      {
85          return new LinearDensity(value, LinearDensityUnit.SI);
86      }
87  
88      /**
89       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
90       * @param zero the value at a ratio of zero
91       * @param one the value at a ratio of one
92       * @param ratio the ratio between 0 and 1, inclusive
93       * @return a LinearDensity at the given ratio between 0 and 1
94       */
95      public static LinearDensity interpolate(final LinearDensity zero, final LinearDensity one, final double ratio)
96      {
97          Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
98                  "ratio for interpolation should be between 0 and 1, but is %f", ratio);
99          return new LinearDensity(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
100                 zero.getDisplayUnit());
101     }
102 
103     /**
104      * Return the maximum value of two relative scalars.
105      * @param r1 the first scalar
106      * @param r2 the second scalar
107      * @return the maximum value of two relative scalars
108      */
109     public static LinearDensity max(final LinearDensity r1, final LinearDensity r2)
110     {
111         return r1.gt(r2) ? r1 : r2;
112     }
113 
114     /**
115      * Return the maximum value of more than two relative scalars.
116      * @param r1 the first scalar
117      * @param r2 the second scalar
118      * @param rn the other scalars
119      * @return the maximum value of more than two relative scalars
120      */
121     public static LinearDensity max(final LinearDensity r1, final LinearDensity r2, final LinearDensity... rn)
122     {
123         LinearDensity maxr = r1.gt(r2) ? r1 : r2;
124         for (LinearDensity r : rn)
125         {
126             if (r.gt(maxr))
127             {
128                 maxr = r;
129             }
130         }
131         return maxr;
132     }
133 
134     /**
135      * Return the minimum value of two relative scalars.
136      * @param r1 the first scalar
137      * @param r2 the second scalar
138      * @return the minimum value of two relative scalars
139      */
140     public static LinearDensity min(final LinearDensity r1, final LinearDensity r2)
141     {
142         return r1.lt(r2) ? r1 : r2;
143     }
144 
145     /**
146      * Return the minimum value of more than two relative scalars.
147      * @param r1 the first scalar
148      * @param r2 the second scalar
149      * @param rn the other scalars
150      * @return the minimum value of more than two relative scalars
151      */
152     public static LinearDensity min(final LinearDensity r1, final LinearDensity r2, final LinearDensity... rn)
153     {
154         LinearDensity minr = r1.lt(r2) ? r1 : r2;
155         for (LinearDensity r : rn)
156         {
157             if (r.lt(minr))
158             {
159                 minr = r;
160             }
161         }
162         return minr;
163     }
164 
165     /**
166      * Returns a LinearDensity representation of a textual representation of a value with a unit. The String representation that
167      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
168      * allowed, but not required, between the value and the unit.
169      * @param text the textual representation to parse into a LinearDensity
170      * @return the Scalar representation of the value in its unit
171      * @throws IllegalArgumentException when the text cannot be parsed
172      * @throws NullPointerException when the text argument is null
173      */
174     public static LinearDensity valueOf(final String text)
175     {
176         Throw.whenNull(text, "Error parsing LinearDensity: text to parse is null");
177         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing LinearDensity: empty text to parse");
178         try
179         {
180             NumberParser numberParser = new NumberParser().lenient().trailing();
181             double d = numberParser.parseDouble(text);
182             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
183             LinearDensityUnit unit = LinearDensityUnit.BASE.getUnitByAbbreviation(unitString);
184             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity LinearDensity",
185                     unitString);
186             return new LinearDensity(d, unit);
187         }
188         catch (Exception exception)
189         {
190             throw new IllegalArgumentException(
191                     "Error parsing LinearDensity from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
192                     exception);
193         }
194     }
195 
196     /**
197      * Returns a LinearDensity based on a value and the textual representation of the unit, which can be localized.
198      * @param value the value to use
199      * @param unitString the textual representation of the unit
200      * @return the Scalar representation of the value in its unit
201      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
202      * @throws NullPointerException when the unitString argument is null
203      */
204     public static LinearDensity of(final double value, final String unitString)
205     {
206         Throw.whenNull(unitString, "Error parsing LinearDensity: unitString is null");
207         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing LinearDensity: empty unitString");
208         LinearDensityUnit unit = LinearDensityUnit.BASE.getUnitByAbbreviation(unitString);
209         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing LinearDensity with unit %s", unitString);
210         return new LinearDensity(value, unit);
211     }
212 
213     /**
214      * Calculate the division of LinearDensity and LinearDensity, which results in a Dimensionless scalar.
215      * @param v scalar
216      * @return scalar as a division of LinearDensity and LinearDensity
217      */
218     public final Dimensionless divide(final LinearDensity v)
219     {
220         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
221     }
222 
223     /**
224      * Calculate the multiplication of LinearDensity and Length, which results in a Dimensionless scalar.
225      * @param v scalar
226      * @return scalar as a multiplication of LinearDensity and Length
227      */
228     public final Dimensionless times(final Length v)
229     {
230         return new Dimensionless(this.si * v.si, DimensionlessUnit.SI);
231     }
232 
233     /**
234      * Calculate the multiplication of LinearDensity and Area, which results in a Length scalar.
235      * @param v scalar
236      * @return scalar as a multiplication of LinearDensity and Area
237      */
238     public final Length times(final Area v)
239     {
240         return new Length(this.si * v.si, LengthUnit.SI);
241     }
242 
243     /**
244      * Calculate the multiplication of LinearDensity and Energy, which results in a Force scalar.
245      * @param v scalar
246      * @return scalar as a multiplication of LinearDensity and Energy
247      */
248     public final Force times(final Energy v)
249     {
250         return new Force(this.si * v.si, ForceUnit.SI);
251     }
252 
253     /**
254      * Calculate the multiplication of LinearDensity and Speed, which results in a Frequency scalar.
255      * @param v scalar
256      * @return scalar as a multiplication of LinearDensity and Speed
257      */
258     public final Frequency times(final Speed v)
259     {
260         return new Frequency(this.si * v.si, FrequencyUnit.SI);
261     }
262 
263     @Override
264     public Length reciprocal()
265     {
266         return Length.ofSI(1.0 / this.si);
267     }
268 
269     /**
270      * Multiply two scalars that result in a scalar of type LinearDensity.
271      * @param scalar1 the first scalar
272      * @param scalar2 the second scalar
273      * @return the multiplication of both scalars as an instance of LinearDensity
274      */
275     public static LinearDensity multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
276     {
277         Throw.whenNull(scalar1, "scalar1 cannot be null");
278         Throw.whenNull(scalar2, "scalar2 cannot be null");
279         Throw.when(
280                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
281                         .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
282                         .equals(LinearDensityUnit.BASE.getSiDimensions()),
283                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type LinearDensity",
284                 scalar1.toDisplayString(), scalar2.toDisplayString());
285         return new LinearDensity(scalar1.si * scalar2.si, LinearDensityUnit.SI);
286     }
287 
288     /**
289      * Divide two scalars that result in a scalar of type LinearDensity.
290      * @param scalar1 the first scalar
291      * @param scalar2 the second scalar
292      * @return the division of scalar1 by scalar2 as an instance of LinearDensity
293      */
294     public static LinearDensity divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
295     {
296         Throw.whenNull(scalar1, "scalar1 cannot be null");
297         Throw.whenNull(scalar2, "scalar2 cannot be null");
298         Throw.when(
299                 !scalar1.getDisplayUnit().getQuantity().getSiDimensions()
300                         .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions())
301                         .equals(LinearDensityUnit.BASE.getSiDimensions()),
302                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type LinearDensity",
303                 scalar1.toDisplayString(), scalar2.toDisplayString());
304         return new LinearDensity(scalar1.si / scalar2.si, LinearDensityUnit.SI);
305     }
306 
307 }