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