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.AreaUnit;
7   import org.djunits.unit.DimensionlessUnit;
8   import org.djunits.unit.EnergyUnit;
9   import org.djunits.unit.ForceUnit;
10  import org.djunits.unit.LinearDensityUnit;
11  import org.djunits.unit.MassUnit;
12  import org.djunits.unit.PowerUnit;
13  import org.djunits.unit.PressureUnit;
14  import org.djunits.unit.Unit;
15  
16  /**
17   * Easy access methods for the Force FloatScalar, which is relative by definition. An example is Speed. Instead of:
18   * 
19   * <pre>
20   * FloatScalar.Rel&lt;ForceUnit&gt; value = new FloatScalar.Rel&lt;ForceUnit&gt;(100.0, ForceUnit.SI);
21   * </pre>
22   * 
23   * we can now write:
24   * 
25   * <pre>
26   * FloatForce value = new FloatForce(100.0, ForceUnit.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 FloatForce extends AbstractFloatScalarRel<ForceUnit, FloatForce>
41  {
42      /** */
43      private static final long serialVersionUID = 20150901L;
44  
45      /** constant with value zero. */
46      public static final FloatForce ZERO = new FloatForce(0.0f, ForceUnit.SI);
47  
48      /** constant with value NaN. */
49      @SuppressWarnings("checkstyle:constantname")
50      public static final FloatForce NaN = new FloatForce(Float.NaN, ForceUnit.SI);
51  
52      /** constant with value POSITIVE_INFINITY. */
53      public static final FloatForce POSITIVE_INFINITY = new FloatForce(Float.POSITIVE_INFINITY, ForceUnit.SI);
54  
55      /** constant with value NEGATIVE_INFINITY. */
56      public static final FloatForce NEGATIVE_INFINITY = new FloatForce(Float.NEGATIVE_INFINITY, ForceUnit.SI);
57  
58      /** constant with value MAX_VALUE. */
59      public static final FloatForce POS_MAXVALUE = new FloatForce(Float.MAX_VALUE, ForceUnit.SI);
60  
61      /** constant with value -MAX_VALUE. */
62      public static final FloatForce NEG_MAXVALUE = new FloatForce(-Float.MAX_VALUE, ForceUnit.SI);
63  
64      /**
65       * Construct FloatForce scalar.
66       * @param value float value
67       * @param unit unit for the float value
68       */
69      public FloatForce(final float value, final ForceUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct FloatForce scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public FloatForce(final FloatForce value)
79      {
80          super(value);
81      }
82  
83      /**
84       * Construct FloatForce scalar using a double value.
85       * @param value double value
86       * @param unit unit for the resulting float value
87       */
88      public FloatForce(final double value, final ForceUnit unit)
89      {
90          super((float) value, unit);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public final FloatForce instantiateRel(final float value, final ForceUnit unit)
96      {
97          return new FloatForce(value, unit);
98      }
99  
100     /**
101      * Construct FloatForce scalar.
102      * @param value float value in SI units
103      * @return the new scalar with the SI value
104      */
105     public static final FloatForce createSI(final float value)
106     {
107         return new FloatForce(value, ForceUnit.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 FloatForce interpolate(final FloatForce zero, final FloatForce one, final float ratio)
118     {
119         return new FloatForce(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 FloatForce max(final FloatForce r1, final FloatForce 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 FloatForce max(final FloatForce r1, final FloatForce r2, final FloatForce... rn)
141     {
142         FloatForce maxr = (r1.gt(r2)) ? r1 : r2;
143         for (FloatForce 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 FloatForce min(final FloatForce r1, final FloatForce 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 FloatForce min(final FloatForce r1, final FloatForce r2, final FloatForce... rn)
172     {
173         FloatForce minr = (r1.lt(r2)) ? r1 : r2;
174         for (FloatForce r : rn)
175         {
176             if (r.lt(minr))
177             {
178                 minr = r;
179             }
180         }
181         return minr;
182     }
183 
184     /**
185      * Returns a FloatForce 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 FloatForce
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 FloatForce valueOf(final String text) throws IllegalArgumentException
193     {
194         if (text == null || text.length() == 0)
195         {
196             throw new IllegalArgumentException("Error parsing FloatForce -- 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 (ForceUnit unit : Unit.getUnits(ForceUnit.class))
207                 {
208                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
209                     {
210                         float f = Float.parseFloat(valueString);
211                         return new FloatForce(f, unit);
212                     }
213                 }
214             }
215             catch (Exception exception)
216             {
217                 throw new IllegalArgumentException("Error parsing FloatForce from " + text, exception);
218             }
219         }
220         throw new IllegalArgumentException("Error parsing FloatForce from " + text);
221     }
222 
223     /**
224      * Calculate the division of FloatForce and FloatForce, which results in a FloatDimensionless scalar.
225      * @param v FloatForce scalar
226      * @return FloatDimensionless scalar as a division of FloatForce and FloatForce
227      */
228     public final FloatDimensionless divideBy(final FloatForce v)
229     {
230         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
231     }
232 
233     /**
234      * Calculate the multiplication of FloatForce and FloatLength, which results in a FloatEnergy scalar.
235      * @param v FloatForce scalar
236      * @return FloatEnergy scalar as a multiplication of FloatForce and FloatLength
237      */
238     public final FloatEnergy multiplyBy(final FloatLength v)
239     {
240         return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
241     }
242 
243     /**
244      * Calculate the division of FloatForce and FloatLinearDensity, which results in a FloatEnergy scalar.
245      * @param v FloatForce scalar
246      * @return FloatEnergy scalar as a division of FloatForce and FloatLinearDensity
247      */
248     public final FloatEnergy divideBy(final FloatLinearDensity v)
249     {
250         return new FloatEnergy(this.si / v.si, EnergyUnit.SI);
251     }
252 
253     /**
254      * Calculate the division of FloatForce and FloatEnergy, which results in a FloatLinearDensity scalar.
255      * @param v FloatForce scalar
256      * @return FloatLinearDensity scalar as a division of FloatForce and FloatEnergy
257      */
258     public final FloatLinearDensity divideBy(final FloatEnergy v)
259     {
260         return new FloatLinearDensity(this.si / v.si, LinearDensityUnit.SI);
261     }
262 
263     /**
264      * Calculate the multiplication of FloatForce and FloatSpeed, which results in a FloatPower scalar.
265      * @param v FloatForce scalar
266      * @return FloatPower scalar as a multiplication of FloatForce and FloatSpeed
267      */
268     public final FloatPower multiplyBy(final FloatSpeed v)
269     {
270         return new FloatPower(this.si * v.si, PowerUnit.SI);
271     }
272 
273     /**
274      * Calculate the division of FloatForce and FloatMass, which results in a FloatAcceleration scalar.
275      * @param v FloatForce scalar
276      * @return FloatAcceleration scalar as a division of FloatForce and FloatMass
277      */
278     public final FloatAcceleration divideBy(final FloatMass v)
279     {
280         return new FloatAcceleration(this.si / v.si, AccelerationUnit.SI);
281     }
282 
283     /**
284      * Calculate the division of FloatForce and FloatAcceleration, which results in a FloatMass scalar.
285      * @param v FloatForce scalar
286      * @return FloatMass scalar as a division of FloatForce and FloatAcceleration
287      */
288     public final FloatMass divideBy(final FloatAcceleration v)
289     {
290         return new FloatMass(this.si / v.si, MassUnit.SI);
291     }
292 
293     /**
294      * Calculate the division of FloatForce and FloatArea, which results in a FloatPressure scalar.
295      * @param v FloatForce scalar
296      * @return FloatPressure scalar as a division of FloatForce and FloatArea
297      */
298     public final FloatPressure divideBy(final FloatArea v)
299     {
300         return new FloatPressure(this.si / v.si, PressureUnit.SI);
301     }
302 
303     /**
304      * Calculate the division of FloatForce and FloatPressure, which results in a FloatArea scalar.
305      * @param v FloatForce scalar
306      * @return FloatArea scalar as a division of FloatForce and FloatPressure
307      */
308     public final FloatArea divideBy(final FloatPressure v)
309     {
310         return new FloatArea(this.si / v.si, AreaUnit.SI);
311     }
312 
313 }