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