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.DoubleScalarRelWithAbs;
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-2024 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-07-23T14:06:38.224104100Z")
32  public class Length extends DoubleScalarRelWithAbs<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      @Override
79      public final Length instantiateRel(final double value, final LengthUnit unit)
80      {
81          return new Length(value, unit);
82      }
83  
84      @Override
85      public final Position instantiateAbs(final double value, final PositionUnit unit)
86      {
87          return new Position(value, unit);
88      }
89  
90      /**
91       * Construct Length scalar.
92       * @param value double; the double value in SI units
93       * @return Length; the new scalar with the SI value
94       */
95      public static final Length instantiateSI(final double value)
96      {
97          return new Length(value, LengthUnit.SI);
98      }
99  
100     /**
101      * Interpolate between two values.
102      * @param zero Length; the low value
103      * @param one Length; the high value
104      * @param ratio double; the ratio between 0 and 1, inclusive
105      * @return Length; a Scalar at the ratio between
106      */
107     public static Length interpolate(final Length zero, final Length one, final double ratio)
108     {
109         return new Length(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
110     }
111 
112     /**
113      * Return the maximum value of two relative scalars.
114      * @param r1 Length; the first scalar
115      * @param r2 Length; the second scalar
116      * @return Length; the maximum value of two relative scalars
117      */
118     public static Length max(final Length r1, final Length r2)
119     {
120         return r1.gt(r2) ? r1 : r2;
121     }
122 
123     /**
124      * Return the maximum value of more than two relative scalars.
125      * @param r1 Length; the first scalar
126      * @param r2 Length; the second scalar
127      * @param rn Length...; the other scalars
128      * @return Length; the maximum value of more than two relative scalars
129      */
130     public static Length max(final Length r1, final Length r2, final Length... rn)
131     {
132         Length maxr = r1.gt(r2) ? r1 : r2;
133         for (Length r : rn)
134         {
135             if (r.gt(maxr))
136             {
137                 maxr = r;
138             }
139         }
140         return maxr;
141     }
142 
143     /**
144      * Return the minimum value of two relative scalars.
145      * @param r1 Length; the first scalar
146      * @param r2 Length; the second scalar
147      * @return Length; the minimum value of two relative scalars
148      */
149     public static Length min(final Length r1, final Length r2)
150     {
151         return r1.lt(r2) ? r1 : r2;
152     }
153 
154     /**
155      * Return the minimum value of more than two relative scalars.
156      * @param r1 Length; the first scalar
157      * @param r2 Length; the second scalar
158      * @param rn Length...; the other scalars
159      * @return Length; the minimum value of more than two relative scalars
160      */
161     public static Length min(final Length r1, final Length r2, final Length... rn)
162     {
163         Length minr = r1.lt(r2) ? r1 : r2;
164         for (Length r : rn)
165         {
166             if (r.lt(minr))
167             {
168                 minr = r;
169             }
170         }
171         return minr;
172     }
173 
174     /**
175      * Returns a Length representation of a textual representation of a value with a unit. The String representation that can be
176      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
177      * but not required, between the value and the unit.
178      * @param text String; the textual representation to parse into a Length
179      * @return Length; the Scalar representation of the value in its unit
180      * @throws IllegalArgumentException when the text cannot be parsed
181      * @throws NullPointerException when the text argument is null
182      */
183     public static Length valueOf(final String text)
184     {
185         Throw.whenNull(text, "Error parsing Length: text to parse is null");
186         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Length: empty text to parse");
187         try
188         {
189             NumberParser numberParser = new NumberParser().lenient().trailing();
190             double d = numberParser.parseDouble(text);
191             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
192             LengthUnit unit = LengthUnit.BASE.getUnitByAbbreviation(unitString);
193             if (unit == null)
194                 throw new IllegalArgumentException("Unit " + unitString + " not found");
195             return new Length(d, unit);
196         }
197         catch (Exception exception)
198         {
199             throw new IllegalArgumentException(
200                     "Error parsing Length from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
201                     exception);
202         }
203     }
204 
205     /**
206      * Returns a Length based on a value and the textual representation of the unit, which can be localized.
207      * @param value double; the value to use
208      * @param unitString String; the textual representation of the unit
209      * @return Length; the Scalar representation of the value in its unit
210      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
211      * @throws NullPointerException when the unitString argument is null
212      */
213     public static Length of(final double value, final String unitString)
214     {
215         Throw.whenNull(unitString, "Error parsing Length: unitString is null");
216         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Length: empty unitString");
217         LengthUnit unit = LengthUnit.BASE.getUnitByAbbreviation(unitString);
218         if (unit != null)
219         {
220             return new Length(value, unit);
221         }
222         throw new IllegalArgumentException("Error parsing Length with unit " + unitString);
223     }
224 
225     /**
226      * Calculate the division of Length and Length, which results in a Dimensionless scalar.
227      * @param v Length; scalar
228      * @return Dimensionless; scalar as a division of Length and Length
229      */
230     public final Dimensionless divide(final Length v)
231     {
232         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
233     }
234 
235     /**
236      * Calculate the multiplication of Length and LinearDensity, which results in a Dimensionless scalar.
237      * @param v Length; scalar
238      * @return Dimensionless; scalar as a multiplication of Length and LinearDensity
239      */
240     public final Dimensionless times(final LinearDensity v)
241     {
242         return new Dimensionless(this.si * v.si, DimensionlessUnit.SI);
243     }
244 
245     /**
246      * Calculate the multiplication of Length and Length, which results in a Area scalar.
247      * @param v Length; scalar
248      * @return Area; scalar as a multiplication of Length and Length
249      */
250     public final Area times(final Length v)
251     {
252         return new Area(this.si * v.si, AreaUnit.SI);
253     }
254 
255     /**
256      * Calculate the division of Length and LinearDensity, which results in a Area scalar.
257      * @param v Length; scalar
258      * @return Area; scalar as a division of Length and LinearDensity
259      */
260     public final Area divide(final LinearDensity v)
261     {
262         return new Area(this.si / v.si, AreaUnit.SI);
263     }
264 
265     /**
266      * Calculate the division of Length and Area, which results in a LinearDensity scalar.
267      * @param v Length; scalar
268      * @return LinearDensity; scalar as a division of Length and Area
269      */
270     public final LinearDensity divide(final Area v)
271     {
272         return new LinearDensity(this.si / v.si, LinearDensityUnit.SI);
273     }
274 
275     /**
276      * Calculate the multiplication of Length and Area, which results in a Volume scalar.
277      * @param v Length; scalar
278      * @return Volume; scalar as a multiplication of Length and Area
279      */
280     public final Volume times(final Area v)
281     {
282         return new Volume(this.si * v.si, VolumeUnit.SI);
283     }
284 
285     /**
286      * Calculate the multiplication of Length and Force, which results in a Energy scalar.
287      * @param v Length; scalar
288      * @return Energy; scalar as a multiplication of Length and Force
289      */
290     public final Energy times(final Force v)
291     {
292         return new Energy(this.si * v.si, EnergyUnit.SI);
293     }
294 
295     /**
296      * Calculate the multiplication of Length and Frequency, which results in a Speed scalar.
297      * @param v Length; scalar
298      * @return Speed; scalar as a multiplication of Length and Frequency
299      */
300     public final Speed times(final Frequency v)
301     {
302         return new Speed(this.si * v.si, SpeedUnit.SI);
303     }
304 
305     /**
306      * Calculate the division of Length and Duration, which results in a Speed scalar.
307      * @param v Length; scalar
308      * @return Speed; scalar as a division of Length and Duration
309      */
310     public final Speed divide(final Duration v)
311     {
312         return new Speed(this.si / v.si, SpeedUnit.SI);
313     }
314 
315     /**
316      * Calculate the division of Length and Speed, which results in a Duration scalar.
317      * @param v Length; scalar
318      * @return Duration; scalar as a division of Length and Speed
319      */
320     public final Duration divide(final Speed v)
321     {
322         return new Duration(this.si / v.si, DurationUnit.SI);
323     }
324 
325     /**
326      * Calculate the multiplication of Length and FlowMass, which results in a Momentum scalar.
327      * @param v Length; scalar
328      * @return Momentum; scalar as a multiplication of Length and FlowMass
329      */
330     public final Momentum times(final FlowMass v)
331     {
332         return new Momentum(this.si * v.si, MomentumUnit.SI);
333     }
334 
335     @Override
336     public LinearDensity reciprocal()
337     {
338         return LinearDensity.instantiateSI(1.0 / this.si);
339     }
340 
341 }