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