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