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