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.AccelerationUnit;
9   import org.djunits.unit.DimensionlessUnit;
10  import org.djunits.unit.DurationUnit;
11  import org.djunits.unit.EnergyUnit;
12  import org.djunits.unit.FlowVolumeUnit;
13  import org.djunits.unit.ForceUnit;
14  import org.djunits.unit.FrequencyUnit;
15  import org.djunits.unit.LengthUnit;
16  import org.djunits.unit.MomentumUnit;
17  import org.djunits.unit.PowerUnit;
18  import org.djunits.unit.SpeedUnit;
19  import org.djunits.value.util.ValueUtil;
20  import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRel;
21  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
22  
23  /**
24   * Easy access methods for the Speed DoubleScalar, which is relative by definition.
25   * <p>
26   * Copyright (c) 2013-2022 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 = "2022-03-14T11:14:15.180987200Z")
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 Speedble/scalar/Speed.html#Speed">Speed ZERO = new Speed(0.0, SpeedUnit.SI);
40  
41      /** Constant with value one. */
42      public static final Speeduble/scalar/Speed.html#Speed">Speed ONE = new Speed(1.0, SpeedUnit.SI);
43  
44      /** Constant with value NaN. */
45      @SuppressWarnings("checkstyle:constantname")
46      public static final Speeduble/scalar/Speed.html#Speed">Speed NaN = new Speed(Double.NaN, SpeedUnit.SI);
47  
48      /** Constant with value POSITIVE_INFINITY. */
49      public static final Speedeed.html#Speed">Speed POSITIVE_INFINITY = new Speed(Double.POSITIVE_INFINITY, SpeedUnit.SI);
50  
51      /** Constant with value NEGATIVE_INFINITY. */
52      public static final Speedeed.html#Speed">Speed NEGATIVE_INFINITY = new Speed(Double.NEGATIVE_INFINITY, SpeedUnit.SI);
53  
54      /** Constant with value MAX_VALUE. */
55      public static final Speedar/Speed.html#Speed">Speed POS_MAXVALUE = new Speed(Double.MAX_VALUE, SpeedUnit.SI);
56  
57      /** Constant with value -MAX_VALUE. */
58      public static final Speedar/Speed.html#Speed">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/vdouble/scalar/Speed.html#Speed">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 Speedle/scalar/Speed.html#Speed">Speedlar/Speed.html#Speed">Speed interpolate(final Speedle/scalar/Speed.html#Speed">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 Speeduble/scalar/Speed.html#Speed">Speeduble/scalar/Speed.html#Speed">Speed max(final Speeduble/scalar/Speed.html#Speed">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 Speeduble/scalar/Speed.html#Speed">Speeduble/scalar/Speed.html#Speed">Speed max(final Speeduble/scalar/Speed.html#Speed">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 Speeduble/scalar/Speed.html#Speed">Speeduble/scalar/Speed.html#Speed">Speed min(final Speeduble/scalar/Speed.html#Speed">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 Speeduble/scalar/Speed.html#Speed">Speeduble/scalar/Speed.html#Speed">Speed min(final Speeduble/scalar/Speed.html#Speed">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 the official abbreviation of the unit. Spaces are allowed, but not
173      * 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         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
184         if (matcher.find())
185         {
186             int index = matcher.end();
187             String unitString = text.substring(index).trim();
188             String valueString = text.substring(0, index).trim();
189             SpeedUnit unit = SpeedUnit.BASE.getUnitByAbbreviation(unitString);
190             if (unit != null)
191             {
192                 double d = Double.parseDouble(valueString);
193                 return new Speed(d, unit);
194             }
195         }
196         throw new IllegalArgumentException("Error parsing Speed from " + text);
197     }
198 
199     /**
200      * Returns a Speed based on a value and the textual representation of the unit.
201      * @param value double; the value to use
202      * @param unitString String; the textual representation of the unit
203      * @return Speed; the Scalar representation of the value in its unit
204      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
205      * @throws NullPointerException when the unitString argument is null
206      */
207     public static Speed of(final double value, final String unitString)
208     {
209         Throw.whenNull(unitString, "Error parsing Speed: unitString is null");
210         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Speed: empty unitString");
211         SpeedUnit unit = SpeedUnit.BASE.getUnitByAbbreviation(unitString);
212         if (unit != null)
213         {
214             return new Speed(value, unit);
215         }
216         throw new IllegalArgumentException("Error parsing Speed with unit " + unitString);
217     }
218 
219     /**
220      * Calculate the division of Speed and Speed, which results in a Dimensionless scalar.
221      * @param v Speed; scalar
222      * @return Dimensionless; scalar as a division of Speed and Speed
223      */
224     public final Dimensionless divide(final Speed v)
225     {
226         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
227     }
228 
229     /**
230      * Calculate the multiplication of Speed and Area, which results in a FlowVolume scalar.
231      * @param v Speed; scalar
232      * @return FlowVolume; scalar as a multiplication of Speed and Area
233      */
234     public final FlowVolume times(final Area v)
235     {
236         return new FlowVolume(this.si * v.si, FlowVolumeUnit.SI);
237     }
238 
239     /**
240      * Calculate the multiplication of Speed and Force, which results in a Power scalar.
241      * @param v Speed; scalar
242      * @return Power; scalar as a multiplication of Speed and Force
243      */
244     public final Power times(final Force v)
245     {
246         return new Power(this.si * v.si, PowerUnit.SI);
247     }
248 
249     /**
250      * Calculate the multiplication of Speed and Frequency, which results in a Acceleration scalar.
251      * @param v Speed; scalar
252      * @return Acceleration; scalar as a multiplication of Speed and Frequency
253      */
254     public final Acceleration times(final Frequency v)
255     {
256         return new Acceleration(this.si * v.si, AccelerationUnit.SI);
257     }
258 
259     /**
260      * Calculate the division of Speed and Length, which results in a Frequency scalar.
261      * @param v Speed; scalar
262      * @return Frequency; scalar as a division of Speed and Length
263      */
264     public final Frequency divide(final Length v)
265     {
266         return new Frequency(this.si / v.si, FrequencyUnit.SI);
267     }
268 
269     /**
270      * Calculate the division of Speed and Frequency, which results in a Length scalar.
271      * @param v Speed; scalar
272      * @return Length; scalar as a division of Speed and Frequency
273      */
274     public final Length divide(final Frequency v)
275     {
276         return new Length(this.si / v.si, LengthUnit.SI);
277     }
278 
279     /**
280      * Calculate the multiplication of Speed and LinearDensity, which results in a Frequency scalar.
281      * @param v Speed; scalar
282      * @return Frequency; scalar as a multiplication of Speed and LinearDensity
283      */
284     public final Frequency times(final LinearDensity v)
285     {
286         return new Frequency(this.si * v.si, FrequencyUnit.SI);
287     }
288 
289     /**
290      * Calculate the multiplication of Speed and Duration, which results in a Length scalar.
291      * @param v Speed; scalar
292      * @return Length; scalar as a multiplication of Speed and Duration
293      */
294     public final Length times(final Duration v)
295     {
296         return new Length(this.si * v.si, LengthUnit.SI);
297     }
298 
299     /**
300      * Calculate the division of Speed and Duration, which results in a Acceleration scalar.
301      * @param v Speed; scalar
302      * @return Acceleration; scalar as a division of Speed and Duration
303      */
304     public final Acceleration divide(final Duration v)
305     {
306         return new Acceleration(this.si / v.si, AccelerationUnit.SI);
307     }
308 
309     /**
310      * Calculate the division of Speed and Acceleration, which results in a Duration scalar.
311      * @param v Speed; scalar
312      * @return Duration; scalar as a division of Speed and Acceleration
313      */
314     public final Duration divide(final Acceleration v)
315     {
316         return new Duration(this.si / v.si, DurationUnit.SI);
317     }
318 
319     /**
320      * Calculate the multiplication of Speed and FlowMass, which results in a Force scalar.
321      * @param v Speed; scalar
322      * @return Force; scalar as a multiplication of Speed and FlowMass
323      */
324     public final Force times(final FlowMass v)
325     {
326         return new Force(this.si * v.si, ForceUnit.SI);
327     }
328 
329     /**
330      * Calculate the multiplication of Speed and Mass, which results in a Momentum scalar.
331      * @param v Speed; scalar
332      * @return Momentum; scalar as a multiplication of Speed and Mass
333      */
334     public final Momentum times(final Mass v)
335     {
336         return new Momentum(this.si * v.si, MomentumUnit.SI);
337     }
338 
339     /**
340      * Calculate the multiplication of Speed and Momentum, which results in a Energy scalar.
341      * @param v Speed; scalar
342      * @return Energy; scalar as a multiplication of Speed and Momentum
343      */
344     public final Energy times(final Momentum v)
345     {
346         return new Energy(this.si * v.si, EnergyUnit.SI);
347     }
348 
349     /** {@inheritDoc} */
350     @Override
351     public SIScalar reciprocal()
352     {
353         return DoubleScalar.divide(Dimensionless.ONE, this);
354     }
355 
356 }