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