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