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