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