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