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