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