View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.AccelerationUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.unit.FlowVolumeUnit;
9   import org.djunits.unit.ForceUnit;
10  import org.djunits.unit.FrequencyUnit;
11  import org.djunits.unit.LengthUnit;
12  import org.djunits.unit.PowerUnit;
13  import org.djunits.unit.SpeedUnit;
14  import org.djunits.unit.Unit;
15  
16  /**
17   * Easy access methods for the Speed FloatScalar, which is relative by definition. An example is Speed. Instead of:
18   * 
19   * <pre>
20   * FloatScalar.Rel&lt;SpeedUnit&gt; value = new FloatScalar.Rel&lt;SpeedUnit&gt;(100.0, SpeedUnit.SI);
21   * </pre>
22   * 
23   * we can now write:
24   * 
25   * <pre>
26   * FloatSpeed value = new FloatSpeed(100.0, SpeedUnit.SI);
27   * </pre>
28   * 
29   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
30   * used are compatible.
31   * <p>
32   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
33   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
34   * <p>
35   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
36   * initial version Sep 5, 2015 <br>
37   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
38   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
39   */
40  public class FloatSpeed extends AbstractFloatScalarRel<SpeedUnit, FloatSpeed>
41  {
42      /** */
43      private static final long serialVersionUID = 20150901L;
44  
45      /** constant with value zero. */
46      public static final FloatSpeed ZERO = new FloatSpeed(0.0f, SpeedUnit.SI);
47  
48      /** constant with value NaN. */
49      @SuppressWarnings("checkstyle:constantname")
50      public static final FloatSpeed NaN = new FloatSpeed(Float.NaN, SpeedUnit.SI);
51  
52      /** constant with value POSITIVE_INFINITY. */
53      public static final FloatSpeed POSITIVE_INFINITY = new FloatSpeed(Float.POSITIVE_INFINITY, SpeedUnit.SI);
54  
55      /** constant with value NEGATIVE_INFINITY. */
56      public static final FloatSpeed NEGATIVE_INFINITY = new FloatSpeed(Float.NEGATIVE_INFINITY, SpeedUnit.SI);
57  
58      /** constant with value MAX_VALUE. */
59      public static final FloatSpeed POS_MAXVALUE = new FloatSpeed(Float.MAX_VALUE, SpeedUnit.SI);
60  
61      /** constant with value -MAX_VALUE. */
62      public static final FloatSpeed NEG_MAXVALUE = new FloatSpeed(-Float.MAX_VALUE, SpeedUnit.SI);
63  
64      /**
65       * Construct FloatSpeed scalar.
66       * @param value float value
67       * @param unit unit for the float value
68       */
69      public FloatSpeed(final float value, final SpeedUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct FloatSpeed scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public FloatSpeed(final FloatSpeed value)
79      {
80          super(value);
81      }
82  
83      /**
84       * Construct FloatSpeed scalar using a double value.
85       * @param value double value
86       * @param unit unit for the resulting float value
87       */
88      public FloatSpeed(final double value, final SpeedUnit unit)
89      {
90          super((float) value, unit);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public final FloatSpeed instantiateRel(final float value, final SpeedUnit unit)
96      {
97          return new FloatSpeed(value, unit);
98      }
99  
100     /**
101      * Construct FloatSpeed scalar.
102      * @param value float value in SI units
103      * @return the new scalar with the SI value
104      */
105     public static final FloatSpeed createSI(final float value)
106     {
107         return new FloatSpeed(value, SpeedUnit.SI);
108     }
109 
110     /**
111      * Interpolate between two values.
112      * @param zero the low value
113      * @param one the high value
114      * @param ratio the ratio between 0 and 1, inclusive
115      * @return a Scalar at the ratio between
116      */
117     public static FloatSpeed interpolate(final FloatSpeed zero, final FloatSpeed one, final float ratio)
118     {
119         return new FloatSpeed(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
120     }
121 
122     /**
123      * Return the maximum value of two relative scalars.
124      * @param r1 the first scalar
125      * @param r2 the second scalar
126      * @return the maximum value of two relative scalars
127      */
128     public static FloatSpeed max(final FloatSpeed r1, final FloatSpeed r2)
129     {
130         return (r1.gt(r2)) ? r1 : r2;
131     }
132 
133     /**
134      * Return the maximum value of more than two relative scalars.
135      * @param r1 the first scalar
136      * @param r2 the second scalar
137      * @param rn the other scalars
138      * @return the maximum value of more than two relative scalars
139      */
140     public static FloatSpeed max(final FloatSpeed r1, final FloatSpeed r2, final FloatSpeed... rn)
141     {
142         FloatSpeed maxr = (r1.gt(r2)) ? r1 : r2;
143         for (FloatSpeed r : rn)
144         {
145             if (r.gt(maxr))
146             {
147                 maxr = r;
148             }
149         }
150         return maxr;
151     }
152 
153     /**
154      * Return the minimum value of two relative scalars.
155      * @param r1 the first scalar
156      * @param r2 the second scalar
157      * @return the minimum value of two relative scalars
158      */
159     public static FloatSpeed min(final FloatSpeed r1, final FloatSpeed r2)
160     {
161         return (r1.lt(r2)) ? r1 : r2;
162     }
163 
164     /**
165      * Return the minimum value of more than two relative scalars.
166      * @param r1 the first scalar
167      * @param r2 the second scalar
168      * @param rn the other scalars
169      * @return the minimum value of more than two relative scalars
170      */
171     public static FloatSpeed min(final FloatSpeed r1, final FloatSpeed r2, final FloatSpeed... rn)
172     {
173         FloatSpeed minr = (r1.lt(r2)) ? r1 : r2;
174         for (FloatSpeed r : rn)
175         {
176             if (r.lt(minr))
177             {
178                 minr = r;
179             }
180         }
181         return minr;
182     }
183 
184     /**
185      * Returns a FloatSpeed representation of a textual representation of a value with a unit. The String representation that
186      * can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but
187      * not necessary, between the value and the unit.
188      * @param text String; the textual representation to parse into a FloatSpeed
189      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
190      * @throws IllegalArgumentException when the text cannot be parsed
191      */
192     public static FloatSpeed valueOf(final String text) throws IllegalArgumentException
193     {
194         if (text == null || text.length() == 0)
195         {
196             throw new IllegalArgumentException("Error parsing FloatSpeed -- null or empty argument");
197         }
198         Matcher matcher = NUMBER_PATTERN.matcher(text);
199         if (matcher.find())
200         {
201             int index = matcher.end();
202             try
203             {
204                 String unitString = text.substring(index).trim();
205                 String valueString = text.substring(0, index).trim();
206                 for (SpeedUnit unit : Unit.getUnits(SpeedUnit.class))
207                 {
208                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
209                     {
210                         float f = Float.parseFloat(valueString);
211                         return new FloatSpeed(f, unit);
212                     }
213                 }
214             }
215             catch (Exception exception)
216             {
217                 throw new IllegalArgumentException("Error parsing FloatSpeed from " + text, exception);
218             }
219         }
220         throw new IllegalArgumentException("Error parsing FloatSpeed from " + text);
221     }
222 
223     /**
224      * Calculate the division of FloatSpeed and FloatSpeed, which results in a FloatDimensionless scalar.
225      * @param v FloatSpeed scalar
226      * @return FloatDimensionless scalar as a division of FloatSpeed and FloatSpeed
227      */
228     public final FloatDimensionless divideBy(final FloatSpeed v)
229     {
230         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
231     }
232 
233     /**
234      * Calculate the multiplication of FloatSpeed and FloatArea, which results in a FloatFlowVolume scalar.
235      * @param v FloatSpeed scalar
236      * @return FloatFlowVolume scalar as a multiplication of FloatSpeed and FloatArea
237      */
238     public final FloatFlowVolume multiplyBy(final FloatArea v)
239     {
240         return new FloatFlowVolume(this.si * v.si, FlowVolumeUnit.SI);
241     }
242 
243     /**
244      * Calculate the multiplication of FloatSpeed and FloatForce, which results in a FloatPower scalar.
245      * @param v FloatSpeed scalar
246      * @return FloatPower scalar as a multiplication of FloatSpeed and FloatForce
247      */
248     public final FloatPower multiplyBy(final FloatForce v)
249     {
250         return new FloatPower(this.si * v.si, PowerUnit.SI);
251     }
252 
253     /**
254      * Calculate the multiplication of FloatSpeed and FloatFrequency, which results in a FloatAcceleration scalar.
255      * @param v FloatSpeed scalar
256      * @return FloatAcceleration scalar as a multiplication of FloatSpeed and FloatFrequency
257      */
258     public final FloatAcceleration multiplyBy(final FloatFrequency v)
259     {
260         return new FloatAcceleration(this.si * v.si, AccelerationUnit.SI);
261     }
262 
263     /**
264      * Calculate the division of FloatSpeed and FloatLength, which results in a FloatFrequency scalar.
265      * @param v FloatSpeed scalar
266      * @return FloatFrequency scalar as a division of FloatSpeed and FloatLength
267      */
268     public final FloatFrequency divideBy(final FloatLength v)
269     {
270         return new FloatFrequency(this.si / v.si, FrequencyUnit.SI);
271     }
272 
273     /**
274      * Calculate the division of FloatSpeed and FloatFrequency, which results in a FloatLength scalar.
275      * @param v FloatSpeed scalar
276      * @return FloatLength scalar as a division of FloatSpeed and FloatFrequency
277      */
278     public final FloatLength divideBy(final FloatFrequency v)
279     {
280         return new FloatLength(this.si / v.si, LengthUnit.SI);
281     }
282 
283     /**
284      * Calculate the multiplication of FloatSpeed and FloatLinearDensity, which results in a FloatFrequency scalar.
285      * @param v FloatSpeed scalar
286      * @return FloatFrequency scalar as a multiplication of FloatSpeed and FloatLinearDensity
287      */
288     public final FloatFrequency multiplyBy(final FloatLinearDensity v)
289     {
290         return new FloatFrequency(this.si * v.si, FrequencyUnit.SI);
291     }
292 
293     /**
294      * Calculate the multiplication of FloatSpeed and FloatDuration, which results in a FloatLength scalar.
295      * @param v FloatSpeed scalar
296      * @return FloatLength scalar as a multiplication of FloatSpeed and FloatDuration
297      */
298     public final FloatLength multiplyBy(final FloatDuration v)
299     {
300         return new FloatLength(this.si * v.si, LengthUnit.SI);
301     }
302 
303     /**
304      * Calculate the division of FloatSpeed and FloatDuration, which results in a FloatAcceleration scalar.
305      * @param v FloatSpeed scalar
306      * @return FloatAcceleration scalar as a division of FloatSpeed and FloatDuration
307      */
308     public final FloatAcceleration divideBy(final FloatDuration v)
309     {
310         return new FloatAcceleration(this.si / v.si, AccelerationUnit.SI);
311     }
312 
313     /**
314      * Calculate the division of FloatSpeed and FloatAcceleration, which results in a FloatDuration scalar.
315      * @param v FloatSpeed scalar
316      * @return FloatDuration scalar as a division of FloatSpeed and FloatAcceleration
317      */
318     public final FloatDuration divideBy(final FloatAcceleration v)
319     {
320         return new FloatDuration(this.si / v.si, DurationUnit.SI);
321     }
322 
323     /**
324      * Calculate the multiplication of FloatSpeed and FloatFlowMass, which results in a FloatForce scalar.
325      * @param v FloatSpeed scalar
326      * @return FloatForce scalar as a multiplication of FloatSpeed and FloatFlowMass
327      */
328     public final FloatForce multiplyBy(final FloatFlowMass v)
329     {
330         return new FloatForce(this.si * v.si, ForceUnit.SI);
331     }
332 
333 }