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