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