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.ForceUnit;
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.unit.LinearDensityUnit;
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 Area FloatScalar, which is relative by definition. An example is Speed. Instead of:
17   * 
18   * <pre>
19   * FloatScalar.Rel&lt;AreaUnit&gt; value = new FloatScalar.Rel&lt;AreaUnit&gt;(100.0, AreaUnit.SI);
20   * </pre>
21   * 
22   * we can now write:
23   * 
24   * <pre>
25   * FloatArea value = new FloatArea(100.0, AreaUnit.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 FloatArea extends AbstractFloatScalarRel<AreaUnit, FloatArea>
40  {
41      /** */
42      private static final long serialVersionUID = 20150901L;
43  
44      /** constant with value zero. */
45      public static final FloatArea ZERO = new FloatArea(0.0f, AreaUnit.SI);
46  
47      /** constant with value NaN. */
48      @SuppressWarnings("checkstyle:constantname")
49      public static final FloatArea NaN = new FloatArea(Float.NaN, AreaUnit.SI);
50  
51      /** constant with value POSITIVE_INFINITY. */
52      public static final FloatArea POSITIVE_INFINITY = new FloatArea(Float.POSITIVE_INFINITY, AreaUnit.SI);
53  
54      /** constant with value NEGATIVE_INFINITY. */
55      public static final FloatArea NEGATIVE_INFINITY = new FloatArea(Float.NEGATIVE_INFINITY, AreaUnit.SI);
56  
57      /** constant with value MAX_VALUE. */
58      public static final FloatArea POS_MAXVALUE = new FloatArea(Float.MAX_VALUE, AreaUnit.SI);
59  
60      /** constant with value -MAX_VALUE. */
61      public static final FloatArea NEG_MAXVALUE = new FloatArea(-Float.MAX_VALUE, AreaUnit.SI);
62  
63      /**
64       * Construct FloatArea scalar.
65       * @param value float value
66       * @param unit unit for the float value
67       */
68      public FloatArea(final float value, final AreaUnit unit)
69      {
70          super(value, unit);
71      }
72  
73      /**
74       * Construct FloatArea scalar.
75       * @param value Scalar from which to construct this instance
76       */
77      public FloatArea(final FloatArea value)
78      {
79          super(value);
80      }
81  
82      /**
83       * Construct FloatArea scalar using a double value.
84       * @param value double value
85       * @param unit unit for the resulting float value
86       */
87      public FloatArea(final double value, final AreaUnit unit)
88      {
89          super((float) value, unit);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public final FloatArea instantiateRel(final float value, final AreaUnit unit)
95      {
96          return new FloatArea(value, unit);
97      }
98  
99      /**
100      * Construct FloatArea scalar.
101      * @param value float value in SI units
102      * @return the new scalar with the SI value
103      */
104     public static final FloatArea createSI(final float value)
105     {
106         return new FloatArea(value, AreaUnit.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 FloatArea interpolate(final FloatArea zero, final FloatArea one, final float ratio)
117     {
118         return new FloatArea(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 FloatArea max(final FloatArea r1, final FloatArea 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 FloatArea max(final FloatArea r1, final FloatArea r2, final FloatArea... rn)
140     {
141         FloatArea maxr = (r1.gt(r2)) ? r1 : r2;
142         for (FloatArea 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 FloatArea min(final FloatArea r1, final FloatArea 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 FloatArea min(final FloatArea r1, final FloatArea r2, final FloatArea... rn)
171     {
172         FloatArea minr = (r1.lt(r2)) ? r1 : r2;
173         for (FloatArea r : rn)
174         {
175             if (r.lt(minr))
176             {
177                 minr = r;
178             }
179         }
180         return minr;
181     }
182 
183     /**
184      * Returns a FloatArea 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 FloatArea
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 FloatArea valueOf(final String text) throws IllegalArgumentException
192     {
193         if (text == null || text.length() == 0)
194         {
195             throw new IllegalArgumentException("Error parsing FloatArea -- 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 (AreaUnit unit : Unit.getUnits(AreaUnit.class))
206                 {
207                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
208                     {
209                         float f = Float.parseFloat(valueString);
210                         return new FloatArea(f, unit);
211                     }
212                 }
213             }
214             catch (Exception exception)
215             {
216                 throw new IllegalArgumentException("Error parsing FloatArea from " + text, exception);
217             }
218         }
219         throw new IllegalArgumentException("Error parsing FloatArea from " + text);
220     }
221 
222     /**
223      * Calculate the division of FloatArea and FloatArea, which results in a FloatDimensionless scalar.
224      * @param v FloatArea scalar
225      * @return FloatDimensionless scalar as a division of FloatArea and FloatArea
226      */
227     public final FloatDimensionless divideBy(final FloatArea v)
228     {
229         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
230     }
231 
232     /**
233      * Calculate the multiplication of FloatArea and FloatLength, which results in a FloatVolume scalar.
234      * @param v FloatArea scalar
235      * @return FloatVolume scalar as a multiplication of FloatArea and FloatLength
236      */
237     public final FloatVolume multiplyBy(final FloatLength v)
238     {
239         return new FloatVolume(this.si * v.si, VolumeUnit.SI);
240     }
241 
242     /**
243      * Calculate the division of FloatArea and FloatLinearDensity, which results in a FloatVolume scalar.
244      * @param v FloatArea scalar
245      * @return FloatVolume scalar as a division of FloatArea and FloatLinearDensity
246      */
247     public final FloatVolume divideBy(final FloatLinearDensity v)
248     {
249         return new FloatVolume(this.si / v.si, VolumeUnit.SI);
250     }
251 
252     /**
253      * Calculate the division of FloatArea and FloatVolume, which results in a FloatLinearDensity scalar.
254      * @param v FloatArea scalar
255      * @return FloatLinearDensity scalar as a division of FloatArea and FloatVolume
256      */
257     public final FloatLinearDensity divideBy(final FloatVolume v)
258     {
259         return new FloatLinearDensity(this.si / v.si, LinearDensityUnit.SI);
260     }
261 
262     /**
263      * Calculate the division of FloatArea and FloatLength, which results in a FloatLength scalar.
264      * @param v FloatArea scalar
265      * @return FloatLength scalar as a division of FloatArea and FloatLength
266      */
267     public final FloatLength divideBy(final FloatLength v)
268     {
269         return new FloatLength(this.si / v.si, LengthUnit.SI);
270     }
271 
272     /**
273      * Calculate the multiplication of FloatArea and FloatLinearDensity, which results in a FloatLength scalar.
274      * @param v FloatArea scalar
275      * @return FloatLength scalar as a multiplication of FloatArea and FloatLinearDensity
276      */
277     public final FloatLength multiplyBy(final FloatLinearDensity v)
278     {
279         return new FloatLength(this.si * v.si, LengthUnit.SI);
280     }
281 
282     /**
283      * Calculate the multiplication of FloatArea and FloatSpeed, which results in a FloatFlowVolume scalar.
284      * @param v FloatArea scalar
285      * @return FloatFlowVolume scalar as a multiplication of FloatArea and FloatSpeed
286      */
287     public final FloatFlowVolume multiplyBy(final FloatSpeed v)
288     {
289         return new FloatFlowVolume(this.si * v.si, FlowVolumeUnit.SI);
290     }
291 
292     /**
293      * Calculate the multiplication of FloatArea and FloatPressure, which results in a FloatForce scalar.
294      * @param v FloatArea scalar
295      * @return FloatForce scalar as a multiplication of FloatArea and FloatPressure
296      */
297     public final FloatForce multiplyBy(final FloatPressure v)
298     {
299         return new FloatForce(this.si * v.si, ForceUnit.SI);
300     }
301 
302     /**
303      * Calculate the multiplication of FloatArea and FloatMoneyPerArea, which results in a FloatMoney scalar.
304      * @param v FloatArea scalar
305      * @return FloatMoney scalar as a multiplication of FloatArea and FloatMoneyPerArea
306      */
307     public final FloatMoney multiplyBy(final FloatMoneyPerArea v)
308     {
309         return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
310     }
311 
312 }