View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
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.value.vdouble.scalar.base.DoubleScalarRel;
10  import org.djutils.base.NumberParser;
11  import org.djutils.exceptions.Throw;
12  
13  import jakarta.annotation.Generated;
14  
15  /**
16   * Easy access methods for the Pressure DoubleScalar, which is relative by definition.
17   * <p>
18   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
20   * </p>
21   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
23   */
24  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
25  public class Pressure extends DoubleScalarRel<PressureUnit, Pressure>
26  {
27      /** */
28      private static final long serialVersionUID = 20150905L;
29  
30      /** Constant with value zero. */
31      public static final Pressure ZERO = new Pressure(0.0, PressureUnit.SI);
32  
33      /** Constant with value one. */
34      public static final Pressure ONE = new Pressure(1.0, PressureUnit.SI);
35  
36      /** Constant with value NaN. */
37      @SuppressWarnings("checkstyle:constantname")
38      public static final Pressure NaN = new Pressure(Double.NaN, PressureUnit.SI);
39  
40      /** Constant with value POSITIVE_INFINITY. */
41      public static final Pressure POSITIVE_INFINITY = new Pressure(Double.POSITIVE_INFINITY, PressureUnit.SI);
42  
43      /** Constant with value NEGATIVE_INFINITY. */
44      public static final Pressure NEGATIVE_INFINITY = new Pressure(Double.NEGATIVE_INFINITY, PressureUnit.SI);
45  
46      /** Constant with value MAX_VALUE. */
47      public static final Pressure POS_MAXVALUE = new Pressure(Double.MAX_VALUE, PressureUnit.SI);
48  
49      /** Constant with value -MAX_VALUE. */
50      public static final Pressure NEG_MAXVALUE = new Pressure(-Double.MAX_VALUE, PressureUnit.SI);
51  
52      /**
53       * Construct Pressure scalar with a unit.
54       * @param value the double value, expressed in the given unit
55       * @param unit unit for the double value
56       */
57      public Pressure(final double value, final PressureUnit unit)
58      {
59          super(value, unit);
60      }
61  
62      /**
63       * Construct Pressure scalar.
64       * @param value Scalar from which to construct this instance
65       */
66      public Pressure(final Pressure value)
67      {
68          super(value);
69      }
70  
71      @Override
72      public final Pressure instantiateRel(final double value, final PressureUnit unit)
73      {
74          return new Pressure(value, unit);
75      }
76  
77      /**
78       * Construct Pressure scalar based on an SI value.
79       * @param value the double value in SI units
80       * @return the new scalar with the SI value
81       */
82      public static final Pressure ofSI(final double value)
83      {
84          return new Pressure(value, PressureUnit.SI);
85      }
86  
87      /**
88       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
89       * @param zero the value at a ratio of zero
90       * @param one the value at a ratio of one
91       * @param ratio the ratio between 0 and 1, inclusive
92       * @return a Pressure at the given ratio between 0 and 1
93       */
94      public static Pressure interpolate(final Pressure zero, final Pressure one, final double ratio)
95      {
96          Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
97                  "ratio for interpolation should be between 0 and 1, but is %f", ratio);
98          return new Pressure(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
99                  zero.getDisplayUnit());
100     }
101 
102     /**
103      * Return the maximum value of two relative scalars.
104      * @param r1 the first scalar
105      * @param r2 the second scalar
106      * @return the maximum value of two relative scalars
107      */
108     public static Pressure max(final Pressure r1, final Pressure r2)
109     {
110         return r1.gt(r2) ? r1 : r2;
111     }
112 
113     /**
114      * Return the maximum value of more than two relative scalars.
115      * @param r1 the first scalar
116      * @param r2 the second scalar
117      * @param rn the other scalars
118      * @return the maximum value of more than two relative scalars
119      */
120     public static Pressure max(final Pressure r1, final Pressure r2, final Pressure... rn)
121     {
122         Pressure maxr = r1.gt(r2) ? r1 : r2;
123         for (Pressure r : rn)
124         {
125             if (r.gt(maxr))
126             {
127                 maxr = r;
128             }
129         }
130         return maxr;
131     }
132 
133     /**
134      * Return the minimum value of two relative scalars.
135      * @param r1 the first scalar
136      * @param r2 the second scalar
137      * @return the minimum value of two relative scalars
138      */
139     public static Pressure min(final Pressure r1, final Pressure r2)
140     {
141         return r1.lt(r2) ? r1 : r2;
142     }
143 
144     /**
145      * Return the minimum value of more than two relative scalars.
146      * @param r1 the first scalar
147      * @param r2 the second scalar
148      * @param rn the other scalars
149      * @return the minimum value of more than two relative scalars
150      */
151     public static Pressure min(final Pressure r1, final Pressure r2, final Pressure... rn)
152     {
153         Pressure minr = r1.lt(r2) ? r1 : r2;
154         for (Pressure r : rn)
155         {
156             if (r.lt(minr))
157             {
158                 minr = r;
159             }
160         }
161         return minr;
162     }
163 
164     /**
165      * Returns a Pressure representation of a textual representation of a value with a unit. The String representation that can
166      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
167      * allowed, but not required, between the value and the unit.
168      * @param text the textual representation to parse into a Pressure
169      * @return the Scalar representation of the value in its unit
170      * @throws IllegalArgumentException when the text cannot be parsed
171      * @throws NullPointerException when the text argument is null
172      */
173     public static Pressure valueOf(final String text)
174     {
175         Throw.whenNull(text, "Error parsing Pressure: text to parse is null");
176         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Pressure: empty text to parse");
177         try
178         {
179             NumberParser numberParser = new NumberParser().lenient().trailing();
180             double d = numberParser.parseDouble(text);
181             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
182             PressureUnit unit = PressureUnit.BASE.getUnitByAbbreviation(unitString);
183             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Pressure", unitString);
184             return new Pressure(d, unit);
185         }
186         catch (Exception exception)
187         {
188             throw new IllegalArgumentException(
189                     "Error parsing Pressure from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
190                     exception);
191         }
192     }
193 
194     /**
195      * Returns a Pressure based on a value and the textual representation of the unit, which can be localized.
196      * @param value the value to use
197      * @param unitString the textual representation of the unit
198      * @return the Scalar representation of the value in its unit
199      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
200      * @throws NullPointerException when the unitString argument is null
201      */
202     public static Pressure of(final double value, final String unitString)
203     {
204         Throw.whenNull(unitString, "Error parsing Pressure: unitString is null");
205         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Pressure: empty unitString");
206         PressureUnit unit = PressureUnit.BASE.getUnitByAbbreviation(unitString);
207         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Pressure with unit %s", unitString);
208         return new Pressure(value, unit);
209     }
210 
211     /**
212      * Calculate the division of Pressure and Pressure, which results in a Dimensionless scalar.
213      * @param v scalar
214      * @return scalar as a division of Pressure and Pressure
215      */
216     public final Dimensionless divide(final Pressure v)
217     {
218         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
219     }
220 
221     /**
222      * Calculate the multiplication of Pressure and Area, which results in a Force scalar.
223      * @param v scalar
224      * @return scalar as a multiplication of Pressure and Area
225      */
226     public final Force times(final Area v)
227     {
228         return new Force(this.si * v.si, ForceUnit.SI);
229     }
230 
231     /**
232      * Calculate the multiplication of Pressure and Volume, which results in a Energy scalar.
233      * @param v scalar
234      * @return scalar as a multiplication of Pressure and Volume
235      */
236     public final Energy times(final Volume v)
237     {
238         return new Energy(this.si * v.si, EnergyUnit.SI);
239     }
240 
241     @Override
242     public SIScalar reciprocal()
243     {
244         return SIScalar.divide(Dimensionless.ONE, this);
245     }
246 
247     /**
248      * Multiply two scalars that result in a scalar of type Pressure.
249      * @param scalar1 the first scalar
250      * @param scalar2 the second scalar
251      * @return the multiplication of both scalars as an instance of Pressure
252      */
253     public static Pressure multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
254     {
255         Throw.whenNull(scalar1, "scalar1 cannot be null");
256         Throw.whenNull(scalar2, "scalar2 cannot be null");
257         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
258                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(PressureUnit.BASE.getSiDimensions()),
259                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Pressure",
260                 scalar1.toDisplayString(), scalar2.toDisplayString());
261         return new Pressure(scalar1.si * scalar2.si, PressureUnit.SI);
262     }
263 
264     /**
265      * Divide two scalars that result in a scalar of type Pressure.
266      * @param scalar1 the first scalar
267      * @param scalar2 the second scalar
268      * @return the division of scalar1 by scalar2 as an instance of Pressure
269      */
270     public static Pressure divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
271     {
272         Throw.whenNull(scalar1, "scalar1 cannot be null");
273         Throw.whenNull(scalar2, "scalar2 cannot be null");
274         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
275                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(PressureUnit.BASE.getSiDimensions()),
276                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Pressure",
277                 scalar1.toDisplayString(), scalar2.toDisplayString());
278         return new Pressure(scalar1.si / scalar2.si, PressureUnit.SI);
279     }
280 
281 }