View Javadoc
1   package org.djunits.value.vdouble.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 DoubleScalar, which is relative by definition. Instead of:
18   * 
19   * <pre>
20   * DoubleScalar.Rel&lt;ForceUnit&gt; value = new DoubleScalar.Rel&lt;ForceUnit&gt;(100.0, ForceUnit.SI);
21   * </pre>
22   * 
23   * we can now write:
24   * 
25   * <pre>
26   * Force value = new Force(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 Force extends AbstractDoubleScalarRel<ForceUnit, Force>
41  {
42      /** */
43      private static final long serialVersionUID = 20150905L;
44  
45      /** constant with value zero. */
46      public static final Force ZERO = new Force(0.0, ForceUnit.SI);
47  
48      /** constant with value NaN. */
49      @SuppressWarnings("checkstyle:constantname")
50      public static final Force NaN = new Force(Double.NaN, ForceUnit.SI);
51  
52      /** constant with value POSITIVE_INFINITY. */
53      public static final Force POSITIVE_INFINITY = new Force(Double.POSITIVE_INFINITY, ForceUnit.SI);
54  
55      /** constant with value NEGATIVE_INFINITY. */
56      public static final Force NEGATIVE_INFINITY = new Force(Double.NEGATIVE_INFINITY, ForceUnit.SI);
57  
58      /** constant with value MAX_VALUE. */
59      public static final Force POS_MAXVALUE = new Force(Double.MAX_VALUE, ForceUnit.SI);
60  
61      /** constant with value -MAX_VALUE. */
62      public static final Force NEG_MAXVALUE = new Force(-Double.MAX_VALUE, ForceUnit.SI);
63  
64      /**
65       * Construct Force scalar.
66       * @param value double value
67       * @param unit unit for the double value
68       */
69      public Force(final double value, final ForceUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct Force scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public Force(final Force value)
79      {
80          super(value);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final Force instantiateRel(final double value, final ForceUnit unit)
86      {
87          return new Force(value, unit);
88      }
89  
90      /**
91       * Construct Force scalar.
92       * @param value double value in SI units
93       * @return the new scalar with the SI value
94       */
95      public static final Force createSI(final double value)
96      {
97          return new Force(value, ForceUnit.SI);
98      }
99  
100     /**
101      * Interpolate between two values.
102      * @param zero the low value
103      * @param one the high value
104      * @param ratio the ratio between 0 and 1, inclusive
105      * @return a Scalar at the ratio between
106      */
107     public static Force interpolate(final Force zero, final Force one, final double ratio)
108     {
109         return new Force(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
110     }
111 
112     /**
113      * Return the maximum value of two relative scalars.
114      * @param r1 the first scalar
115      * @param r2 the second scalar
116      * @return the maximum value of two relative scalars
117      */
118     public static Force max(final Force r1, final Force r2)
119     {
120         return (r1.gt(r2)) ? r1 : r2;
121     }
122 
123     /**
124      * Return the maximum value of more than two relative scalars.
125      * @param r1 the first scalar
126      * @param r2 the second scalar
127      * @param rn the other scalars
128      * @return the maximum value of more than two relative scalars
129      */
130     public static Force max(final Force r1, final Force r2, final Force... rn)
131     {
132         Force maxr = (r1.gt(r2)) ? r1 : r2;
133         for (Force r : rn)
134         {
135             if (r.gt(maxr))
136             {
137                 maxr = r;
138             }
139         }
140         return maxr;
141     }
142 
143     /**
144      * Return the minimum value of two relative scalars.
145      * @param r1 the first scalar
146      * @param r2 the second scalar
147      * @return the minimum value of two relative scalars
148      */
149     public static Force min(final Force r1, final Force r2)
150     {
151         return (r1.lt(r2)) ? r1 : r2;
152     }
153 
154     /**
155      * Return the minimum value of more than two relative scalars.
156      * @param r1 the first scalar
157      * @param r2 the second scalar
158      * @param rn the other scalars
159      * @return the minimum value of more than two relative scalars
160      */
161     public static Force min(final Force r1, final Force r2, final Force... rn)
162     {
163         Force minr = (r1.lt(r2)) ? r1 : r2;
164         for (Force r : rn)
165         {
166             if (r.lt(minr))
167             {
168                 minr = r;
169             }
170         }
171         return minr;
172     }
173 
174     /**
175      * Returns a Force representation of a textual representation of a value with a unit. The String representation that can be
176      * parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
177      * necessary, between the value and the unit.
178      * @param text String; the textual representation to parse into a Force
179      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
180      * @throws IllegalArgumentException when the text cannot be parsed
181      */
182     public static Force valueOf(final String text) throws IllegalArgumentException
183     {
184         if (text == null || text.length() == 0)
185         {
186             throw new IllegalArgumentException("Error parsing Force -- null or empty argument");
187         }
188         Matcher matcher = NUMBER_PATTERN.matcher(text);
189         if (matcher.find())
190         {
191             int index = matcher.end();
192             try
193             {
194                 String unitString = text.substring(index).trim();
195                 String valueString = text.substring(0, index).trim();
196                 for (ForceUnit unit : Unit.getUnits(ForceUnit.class))
197                 {
198                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
199                     {
200                         double d = Double.parseDouble(valueString);
201                         return new Force(d, unit);
202                     }
203                 }
204             }
205             catch (Exception exception)
206             {
207                 throw new IllegalArgumentException("Error parsing Force from " + text, exception);
208             }
209         }
210         throw new IllegalArgumentException("Error parsing Force from " + text);
211     }
212 
213     /**
214      * Calculate the division of Force and Force, which results in a Dimensionless scalar.
215      * @param v Force scalar
216      * @return Dimensionless scalar as a division of Force and Force
217      */
218     public final Dimensionless divideBy(final Force v)
219     {
220         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
221     }
222 
223     /**
224      * Calculate the multiplication of Force and Length, which results in a Energy scalar.
225      * @param v Force scalar
226      * @return Energy scalar as a multiplication of Force and Length
227      */
228     public final Energy multiplyBy(final Length v)
229     {
230         return new Energy(this.si * v.si, EnergyUnit.SI);
231     }
232 
233     /**
234      * Calculate the division of Force and LinearDensity, which results in a Energy scalar.
235      * @param v Force scalar
236      * @return Energy scalar as a division of Force and LinearDensity
237      */
238     public final Energy divideBy(final LinearDensity v)
239     {
240         return new Energy(this.si / v.si, EnergyUnit.SI);
241     }
242 
243     /**
244      * Calculate the division of Force and Energy, which results in a LinearDensity scalar.
245      * @param v Force scalar
246      * @return LinearDensity scalar as a division of Force and Energy
247      */
248     public final LinearDensity divideBy(final Energy v)
249     {
250         return new LinearDensity(this.si / v.si, LinearDensityUnit.SI);
251     }
252 
253     /**
254      * Calculate the multiplication of Force and Speed, which results in a Power scalar.
255      * @param v Force scalar
256      * @return Power scalar as a multiplication of Force and Speed
257      */
258     public final Power multiplyBy(final Speed v)
259     {
260         return new Power(this.si * v.si, PowerUnit.SI);
261     }
262 
263     /**
264      * Calculate the division of Force and Mass, which results in a Acceleration scalar.
265      * @param v Force scalar
266      * @return Acceleration scalar as a division of Force and Mass
267      */
268     public final Acceleration divideBy(final Mass v)
269     {
270         return new Acceleration(this.si / v.si, AccelerationUnit.SI);
271     }
272 
273     /**
274      * Calculate the division of Force and Acceleration, which results in a Mass scalar.
275      * @param v Force scalar
276      * @return Mass scalar as a division of Force and Acceleration
277      */
278     public final Mass divideBy(final Acceleration v)
279     {
280         return new Mass(this.si / v.si, MassUnit.SI);
281     }
282 
283     /**
284      * Calculate the division of Force and Area, which results in a Pressure scalar.
285      * @param v Force scalar
286      * @return Pressure scalar as a division of Force and Area
287      */
288     public final Pressure divideBy(final Area v)
289     {
290         return new Pressure(this.si / v.si, PressureUnit.SI);
291     }
292 
293     /**
294      * Calculate the division of Force and Pressure, which results in a Area scalar.
295      * @param v Force scalar
296      * @return Area scalar as a division of Force and Pressure
297      */
298     public final Area divideBy(final Pressure v)
299     {
300         return new Area(this.si / v.si, AreaUnit.SI);
301     }
302 
303 }