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.DoubleScalarRel;
17  import org.djutils.base.NumberParser;
18  import org.djutils.exceptions.Throw;
19  
20  import jakarta.annotation.Generated;
21  
22  /**
23   * Easy access methods for the Speed DoubleScalar, which is relative by definition.
24   * <p>
25   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. 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 = "2025-09-06T15:16:28.380798Z")
32  public class Speed extends DoubleScalarRel<SpeedUnit, Speed>
33  {
34      /** */
35      private static final long serialVersionUID = 20150905L;
36  
37      /** Constant with value zero. */
38      public static final Speed ZERO = new Speed(0.0, SpeedUnit.SI);
39  
40      /** Constant with value one. */
41      public static final Speed ONE = new Speed(1.0, SpeedUnit.SI);
42  
43      /** Constant with value NaN. */
44      @SuppressWarnings("checkstyle:constantname")
45      public static final Speed NaN = new Speed(Double.NaN, SpeedUnit.SI);
46  
47      /** Constant with value POSITIVE_INFINITY. */
48      public static final Speed POSITIVE_INFINITY = new Speed(Double.POSITIVE_INFINITY, SpeedUnit.SI);
49  
50      /** Constant with value NEGATIVE_INFINITY. */
51      public static final Speed NEGATIVE_INFINITY = new Speed(Double.NEGATIVE_INFINITY, SpeedUnit.SI);
52  
53      /** Constant with value MAX_VALUE. */
54      public static final Speed POS_MAXVALUE = new Speed(Double.MAX_VALUE, SpeedUnit.SI);
55  
56      /** Constant with value -MAX_VALUE. */
57      public static final Speed NEG_MAXVALUE = new Speed(-Double.MAX_VALUE, SpeedUnit.SI);
58  
59      /**
60       * Construct Speed scalar with a unit.
61       * @param value the double value, expressed in the given unit
62       * @param unit unit for the double value
63       */
64      public Speed(final double value, final SpeedUnit unit)
65      {
66          super(value, unit);
67      }
68  
69      /**
70       * Construct Speed scalar.
71       * @param value Scalar from which to construct this instance
72       */
73      public Speed(final Speed value)
74      {
75          super(value);
76      }
77  
78      @Override
79      public final Speed instantiateRel(final double value, final SpeedUnit unit)
80      {
81          return new Speed(value, unit);
82      }
83  
84      /**
85       * Construct Speed scalar based on an SI value.
86       * @param value the double value in SI units
87       * @return the new scalar with the SI value
88       */
89      public static final Speed ofSI(final double value)
90      {
91          return new Speed(value, SpeedUnit.SI);
92      }
93  
94      /**
95       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
96       * @param zero the value at a ratio of zero
97       * @param one the value at a ratio of one
98       * @param ratio the ratio between 0 and 1, inclusive
99       * @return a Speed at the given ratio between 0 and 1
100      */
101     public static Speed interpolate(final Speed zero, final Speed one, final double ratio)
102     {
103         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
104                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
105         return new Speed(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
106     }
107 
108     /**
109      * Return the maximum value of two relative scalars.
110      * @param r1 the first scalar
111      * @param r2 the second scalar
112      * @return the maximum value of two relative scalars
113      */
114     public static Speed max(final Speed r1, final Speed r2)
115     {
116         return r1.gt(r2) ? r1 : r2;
117     }
118 
119     /**
120      * Return the maximum value of more than two relative scalars.
121      * @param r1 the first scalar
122      * @param r2 the second scalar
123      * @param rn the other scalars
124      * @return the maximum value of more than two relative scalars
125      */
126     public static Speed max(final Speed r1, final Speed r2, final Speed... rn)
127     {
128         Speed maxr = r1.gt(r2) ? r1 : r2;
129         for (Speed r : rn)
130         {
131             if (r.gt(maxr))
132             {
133                 maxr = r;
134             }
135         }
136         return maxr;
137     }
138 
139     /**
140      * Return the minimum value of two relative scalars.
141      * @param r1 the first scalar
142      * @param r2 the second scalar
143      * @return the minimum value of two relative scalars
144      */
145     public static Speed min(final Speed r1, final Speed r2)
146     {
147         return r1.lt(r2) ? r1 : r2;
148     }
149 
150     /**
151      * Return the minimum value of more than two relative scalars.
152      * @param r1 the first scalar
153      * @param r2 the second scalar
154      * @param rn the other scalars
155      * @return the minimum value of more than two relative scalars
156      */
157     public static Speed min(final Speed r1, final Speed r2, final Speed... rn)
158     {
159         Speed minr = r1.lt(r2) ? r1 : r2;
160         for (Speed r : rn)
161         {
162             if (r.lt(minr))
163             {
164                 minr = r;
165             }
166         }
167         return minr;
168     }
169 
170     /**
171      * Returns a Speed representation of a textual representation of a value with a unit. The String representation that can be
172      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
173      * but not required, between the value and the unit.
174      * @param text the textual representation to parse into a Speed
175      * @return the Scalar representation of the value in its unit
176      * @throws IllegalArgumentException when the text cannot be parsed
177      * @throws NullPointerException when the text argument is null
178      */
179     public static Speed valueOf(final String text)
180     {
181         Throw.whenNull(text, "Error parsing Speed: text to parse is null");
182         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Speed: empty text to parse");
183         try
184         {
185             NumberParser numberParser = new NumberParser().lenient().trailing();
186             double d = numberParser.parseDouble(text);
187             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
188             SpeedUnit unit = SpeedUnit.BASE.getUnitByAbbreviation(unitString);
189             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Speed", unitString);
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 the value to use
203      * @param unitString the textual representation of the unit
204      * @return 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         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Speed with unit %s", unitString);
214         return new Speed(value, unit);
215     }
216 
217     /**
218      * Calculate the division of Speed and Speed, which results in a Dimensionless scalar.
219      * @param v scalar
220      * @return scalar as a division of Speed and Speed
221      */
222     public final Dimensionless divide(final Speed v)
223     {
224         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
225     }
226 
227     /**
228      * Calculate the multiplication of Speed and Area, which results in a FlowVolume scalar.
229      * @param v scalar
230      * @return scalar as a multiplication of Speed and Area
231      */
232     public final FlowVolume times(final Area v)
233     {
234         return new FlowVolume(this.si * v.si, FlowVolumeUnit.SI);
235     }
236 
237     /**
238      * Calculate the multiplication of Speed and Force, which results in a Power scalar.
239      * @param v scalar
240      * @return scalar as a multiplication of Speed and Force
241      */
242     public final Power times(final Force v)
243     {
244         return new Power(this.si * v.si, PowerUnit.SI);
245     }
246 
247     /**
248      * Calculate the multiplication of Speed and Frequency, which results in a Acceleration scalar.
249      * @param v scalar
250      * @return scalar as a multiplication of Speed and Frequency
251      */
252     public final Acceleration times(final Frequency v)
253     {
254         return new Acceleration(this.si * v.si, AccelerationUnit.SI);
255     }
256 
257     /**
258      * Calculate the division of Speed and Length, which results in a Frequency scalar.
259      * @param v scalar
260      * @return scalar as a division of Speed and Length
261      */
262     public final Frequency divide(final Length v)
263     {
264         return new Frequency(this.si / v.si, FrequencyUnit.SI);
265     }
266 
267     /**
268      * Calculate the division of Speed and Frequency, which results in a Length scalar.
269      * @param v scalar
270      * @return scalar as a division of Speed and Frequency
271      */
272     public final Length divide(final Frequency v)
273     {
274         return new Length(this.si / v.si, LengthUnit.SI);
275     }
276 
277     /**
278      * Calculate the multiplication of Speed and LinearDensity, which results in a Frequency scalar.
279      * @param v scalar
280      * @return scalar as a multiplication of Speed and LinearDensity
281      */
282     public final Frequency times(final LinearDensity v)
283     {
284         return new Frequency(this.si * v.si, FrequencyUnit.SI);
285     }
286 
287     /**
288      * Calculate the multiplication of Speed and Duration, which results in a Length scalar.
289      * @param v scalar
290      * @return scalar as a multiplication of Speed and Duration
291      */
292     public final Length times(final Duration v)
293     {
294         return new Length(this.si * v.si, LengthUnit.SI);
295     }
296 
297     /**
298      * Calculate the division of Speed and Duration, which results in a Acceleration scalar.
299      * @param v scalar
300      * @return scalar as a division of Speed and Duration
301      */
302     public final Acceleration divide(final Duration v)
303     {
304         return new Acceleration(this.si / v.si, AccelerationUnit.SI);
305     }
306 
307     /**
308      * Calculate the division of Speed and Acceleration, which results in a Duration scalar.
309      * @param v scalar
310      * @return scalar as a division of Speed and Acceleration
311      */
312     public final Duration divide(final Acceleration v)
313     {
314         return new Duration(this.si / v.si, DurationUnit.SI);
315     }
316 
317     /**
318      * Calculate the multiplication of Speed and FlowMass, which results in a Force scalar.
319      * @param v scalar
320      * @return scalar as a multiplication of Speed and FlowMass
321      */
322     public final Force times(final FlowMass v)
323     {
324         return new Force(this.si * v.si, ForceUnit.SI);
325     }
326 
327     /**
328      * Calculate the multiplication of Speed and Mass, which results in a Momentum scalar.
329      * @param v scalar
330      * @return scalar as a multiplication of Speed and Mass
331      */
332     public final Momentum times(final Mass v)
333     {
334         return new Momentum(this.si * v.si, MomentumUnit.SI);
335     }
336 
337     /**
338      * Calculate the multiplication of Speed and Momentum, which results in a Energy scalar.
339      * @param v scalar
340      * @return scalar as a multiplication of Speed and Momentum
341      */
342     public final Energy times(final Momentum v)
343     {
344         return new Energy(this.si * v.si, EnergyUnit.SI);
345     }
346 
347     @Override
348     public SIScalar reciprocal()
349     {
350         return SIScalar.divide(Dimensionless.ONE, this);
351     }
352 
353     /**
354      * Multiply two scalars that result in a scalar of type Speed.
355      * @param scalar1 the first scalar
356      * @param scalar2 the second scalar
357      * @return the multiplication of both scalars as an instance of Speed
358      */
359     public static Speed multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
360     {
361         Throw.whenNull(scalar1, "scalar1 cannot be null");
362         Throw.whenNull(scalar2, "scalar2 cannot be null");
363         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
364                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(SpeedUnit.BASE.getSiDimensions()),
365                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Speed",
366                 scalar1.toDisplayString(), scalar2.toDisplayString());
367         return new Speed(scalar1.si * scalar2.si, SpeedUnit.SI);
368     }
369 
370     /**
371      * Divide two scalars that result in a scalar of type Speed.
372      * @param scalar1 the first scalar
373      * @param scalar2 the second scalar
374      * @return the division of scalar1 by scalar2 as an instance of Speed
375      */
376     public static Speed divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
377     {
378         Throw.whenNull(scalar1, "scalar1 cannot be null");
379         Throw.whenNull(scalar2, "scalar2 cannot be null");
380         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
381                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(SpeedUnit.BASE.getSiDimensions()),
382                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Speed",
383                 scalar1.toDisplayString(), scalar2.toDisplayString());
384         return new Speed(scalar1.si / scalar2.si, SpeedUnit.SI);
385     }
386 
387 }