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