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.AbstractDoubleScalarRel;
17  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
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-2023 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-04-30T13:59:27.633664900Z")
33  public class Speed extends AbstractDoubleScalarRel<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      /** {@inheritDoc} */
80      @Override
81      public final Speed instantiateRel(final double value, final SpeedUnit unit)
82      {
83          return new Speed(value, unit);
84      }
85  
86      /**
87       * Construct Speed scalar.
88       * @param value double; the double value in SI units
89       * @return Speed; the new scalar with the SI value
90       */
91      public static final Speed instantiateSI(final double value)
92      {
93          return new Speed(value, SpeedUnit.SI);
94      }
95  
96      /**
97       * Interpolate between two values.
98       * @param zero Speed; the low value
99       * @param one Speed; the high value
100      * @param ratio double; the ratio between 0 and 1, inclusive
101      * @return Speed; a Scalar at the ratio between
102      */
103     public static Speed interpolate(final Speed zero, final Speed one, final double ratio)
104     {
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 Speed; the first scalar
111      * @param r2 Speed; the second scalar
112      * @return Speed; 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 Speed; the first scalar
122      * @param r2 Speed; the second scalar
123      * @param rn Speed...; the other scalars
124      * @return Speed; 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 Speed; the first scalar
142      * @param r2 Speed; the second scalar
143      * @return Speed; 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 Speed; the first scalar
153      * @param r2 Speed; the second scalar
154      * @param rn Speed...; the other scalars
155      * @return Speed; 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 String; the textual representation to parse into a Speed
175      * @return Speed; 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             if (unit == null)
190                 throw new IllegalArgumentException("Unit " + unitString + " not found");
191             return new Speed(d, unit);
192         }
193         catch (Exception exception)
194         {
195             throw new IllegalArgumentException(
196                     "Error parsing Speed from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
197                     exception);
198         }
199     }
200 
201     /**
202      * Returns a Speed based on a value and the textual representation of the unit, which can be localized.
203      * @param value double; the value to use
204      * @param unitString String; the textual representation of the unit
205      * @return Speed; the Scalar representation of the value in its unit
206      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
207      * @throws NullPointerException when the unitString argument is null
208      */
209     public static Speed of(final double value, final String unitString)
210     {
211         Throw.whenNull(unitString, "Error parsing Speed: unitString is null");
212         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Speed: empty unitString");
213         SpeedUnit unit = SpeedUnit.BASE.getUnitByAbbreviation(unitString);
214         if (unit != null)
215         {
216             return new Speed(value, unit);
217         }
218         throw new IllegalArgumentException("Error parsing Speed with unit " + unitString);
219     }
220 
221     /**
222      * Calculate the division of Speed and Speed, which results in a Dimensionless scalar.
223      * @param v Speed; scalar
224      * @return Dimensionless; scalar as a division of Speed and Speed
225      */
226     public final Dimensionless divide(final Speed v)
227     {
228         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
229     }
230 
231     /**
232      * Calculate the multiplication of Speed and Area, which results in a FlowVolume scalar.
233      * @param v Speed; scalar
234      * @return FlowVolume; scalar as a multiplication of Speed and Area
235      */
236     public final FlowVolume times(final Area v)
237     {
238         return new FlowVolume(this.si * v.si, FlowVolumeUnit.SI);
239     }
240 
241     /**
242      * Calculate the multiplication of Speed and Force, which results in a Power scalar.
243      * @param v Speed; scalar
244      * @return Power; scalar as a multiplication of Speed and Force
245      */
246     public final Power times(final Force v)
247     {
248         return new Power(this.si * v.si, PowerUnit.SI);
249     }
250 
251     /**
252      * Calculate the multiplication of Speed and Frequency, which results in a Acceleration scalar.
253      * @param v Speed; scalar
254      * @return Acceleration; scalar as a multiplication of Speed and Frequency
255      */
256     public final Acceleration times(final Frequency v)
257     {
258         return new Acceleration(this.si * v.si, AccelerationUnit.SI);
259     }
260 
261     /**
262      * Calculate the division of Speed and Length, which results in a Frequency scalar.
263      * @param v Speed; scalar
264      * @return Frequency; scalar as a division of Speed and Length
265      */
266     public final Frequency divide(final Length v)
267     {
268         return new Frequency(this.si / v.si, FrequencyUnit.SI);
269     }
270 
271     /**
272      * Calculate the division of Speed and Frequency, which results in a Length scalar.
273      * @param v Speed; scalar
274      * @return Length; scalar as a division of Speed and Frequency
275      */
276     public final Length divide(final Frequency v)
277     {
278         return new Length(this.si / v.si, LengthUnit.SI);
279     }
280 
281     /**
282      * Calculate the multiplication of Speed and LinearDensity, which results in a Frequency scalar.
283      * @param v Speed; scalar
284      * @return Frequency; scalar as a multiplication of Speed and LinearDensity
285      */
286     public final Frequency times(final LinearDensity v)
287     {
288         return new Frequency(this.si * v.si, FrequencyUnit.SI);
289     }
290 
291     /**
292      * Calculate the multiplication of Speed and Duration, which results in a Length scalar.
293      * @param v Speed; scalar
294      * @return Length; scalar as a multiplication of Speed and Duration
295      */
296     public final Length times(final Duration v)
297     {
298         return new Length(this.si * v.si, LengthUnit.SI);
299     }
300 
301     /**
302      * Calculate the division of Speed and Duration, which results in a Acceleration scalar.
303      * @param v Speed; scalar
304      * @return Acceleration; scalar as a division of Speed and Duration
305      */
306     public final Acceleration divide(final Duration v)
307     {
308         return new Acceleration(this.si / v.si, AccelerationUnit.SI);
309     }
310 
311     /**
312      * Calculate the division of Speed and Acceleration, which results in a Duration scalar.
313      * @param v Speed; scalar
314      * @return Duration; scalar as a division of Speed and Acceleration
315      */
316     public final Duration divide(final Acceleration v)
317     {
318         return new Duration(this.si / v.si, DurationUnit.SI);
319     }
320 
321     /**
322      * Calculate the multiplication of Speed and FlowMass, which results in a Force scalar.
323      * @param v Speed; scalar
324      * @return Force; scalar as a multiplication of Speed and FlowMass
325      */
326     public final Force times(final FlowMass v)
327     {
328         return new Force(this.si * v.si, ForceUnit.SI);
329     }
330 
331     /**
332      * Calculate the multiplication of Speed and Mass, which results in a Momentum scalar.
333      * @param v Speed; scalar
334      * @return Momentum; scalar as a multiplication of Speed and Mass
335      */
336     public final Momentum times(final Mass v)
337     {
338         return new Momentum(this.si * v.si, MomentumUnit.SI);
339     }
340 
341     /**
342      * Calculate the multiplication of Speed and Momentum, which results in a Energy scalar.
343      * @param v Speed; scalar
344      * @return Energy; scalar as a multiplication of Speed and Momentum
345      */
346     public final Energy times(final Momentum v)
347     {
348         return new Energy(this.si * v.si, EnergyUnit.SI);
349     }
350 
351     /** {@inheritDoc} */
352     @Override
353     public SIScalar reciprocal()
354     {
355         return DoubleScalar.divide(Dimensionless.ONE, this);
356     }
357 
358 }