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