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.AbstractFloatScalarRel;
10  import org.djunits.value.vfloat.scalar.base.FloatScalar;
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-2023 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-04-30T13:59:27.633664900Z")
26  public class FloatDensity extends AbstractFloatScalarRel<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      /** {@inheritDoc} */
83      @Override
84      public final FloatDensity instantiateRel(final float value, final DensityUnit unit)
85      {
86          return new FloatDensity(value, unit);
87      }
88  
89      /**
90       * Construct FloatDensity scalar.
91       * @param value float; the float value in SI units
92       * @return the new scalar with the SI value
93       */
94      public static final FloatDensity instantiateSI(final float value)
95      {
96          return new FloatDensity(value, DensityUnit.SI);
97      }
98  
99      /**
100      * Interpolate between two values.
101      * @param zero the low value
102      * @param one the high value
103      * @param ratio double; the ratio between 0 and 1, inclusive
104      * @return a Scalar at the ratio between
105      */
106     public static FloatDensity interpolate(final FloatDensity zero, final FloatDensity one, final float ratio)
107     {
108         return new FloatDensity(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
109                 zero.getDisplayUnit());
110     }
111 
112     /**
113      * Return the maximum value of two relative scalars.
114      * @param r1 the first scalar
115      * @param r2 the second scalar
116      * @return the maximum value of two relative scalars
117      */
118     public static FloatDensity max(final FloatDensity r1, final FloatDensity r2)
119     {
120         return r1.gt(r2) ? r1 : r2;
121     }
122 
123     /**
124      * Return the maximum value of more than two relative scalars.
125      * @param r1 the first scalar
126      * @param r2 the second scalar
127      * @param rn the other scalars
128      * @return the maximum value of more than two relative scalars
129      */
130     public static FloatDensity max(final FloatDensity r1, final FloatDensity r2, final FloatDensity... rn)
131     {
132         FloatDensity maxr = r1.gt(r2) ? r1 : r2;
133         for (FloatDensity r : rn)
134         {
135             if (r.gt(maxr))
136             {
137                 maxr = r;
138             }
139         }
140         return maxr;
141     }
142 
143     /**
144      * Return the minimum value of two relative scalars.
145      * @param r1 the first scalar
146      * @param r2 the second scalar
147      * @return the minimum value of two relative scalars
148      */
149     public static FloatDensity min(final FloatDensity r1, final FloatDensity r2)
150     {
151         return r1.lt(r2) ? r1 : r2;
152     }
153 
154     /**
155      * Return the minimum value of more than two relative scalars.
156      * @param r1 the first scalar
157      * @param r2 the second scalar
158      * @param rn the other scalars
159      * @return the minimum value of more than two relative scalars
160      */
161     public static FloatDensity min(final FloatDensity r1, final FloatDensity r2, final FloatDensity... rn)
162     {
163         FloatDensity minr = r1.lt(r2) ? r1 : r2;
164         for (FloatDensity r : rn)
165         {
166             if (r.lt(minr))
167             {
168                 minr = r;
169             }
170         }
171         return minr;
172     }
173 
174     /**
175      * Returns a FloatDensity representation of a textual representation of a value with a unit. The String representation that
176      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
177      * allowed, but not required, between the value and the unit.
178      * @param text String; the textual representation to parse into a FloatDensity
179      * @return FloatDensity; the Scalar representation of the value in its unit
180      * @throws IllegalArgumentException when the text cannot be parsed
181      * @throws NullPointerException when the text argument is null
182      */
183     public static FloatDensity valueOf(final String text)
184     {
185         Throw.whenNull(text, "Error parsing FloatDensity: text to parse is null");
186         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatDensity: empty text to parse");
187         try
188         {
189             NumberParser numberParser = new NumberParser().lenient().trailing();
190             float f = numberParser.parseFloat(text);
191             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
192             DensityUnit unit = DensityUnit.BASE.getUnitByAbbreviation(unitString);
193             if (unit == null)
194                 throw new IllegalArgumentException("Unit " + unitString + " not found");
195             return new FloatDensity(f, unit);
196         }
197         catch (Exception exception)
198         {
199             throw new IllegalArgumentException(
200                     "Error parsing FloatDensity from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
201                     exception);
202         }
203     }
204 
205     /**
206      * Returns a FloatDensity based on a value and the textual representation of the unit, which can be localized.
207      * @param value double; the value to use
208      * @param unitString String; the textual representation of the unit
209      * @return FloatDensity; the Scalar representation of the value in its unit
210      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
211      * @throws NullPointerException when the unitString argument is null
212      */
213     public static FloatDensity of(final float value, final String unitString)
214     {
215         Throw.whenNull(unitString, "Error parsing FloatDensity: unitString is null");
216         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatDensity: empty unitString");
217         DensityUnit unit = DensityUnit.BASE.getUnitByAbbreviation(unitString);
218         if (unit != null)
219         {
220             return new FloatDensity(value, unit);
221         }
222         throw new IllegalArgumentException("Error parsing FloatDensity with unit " + unitString);
223     }
224 
225     /**
226      * Calculate the division of FloatDensity and FloatDensity, which results in a FloatDimensionless scalar.
227      * @param v FloatDensity; scalar
228      * @return FloatDimensionless; scalar as a division of FloatDensity and FloatDensity
229      */
230     public final FloatDimensionless divide(final FloatDensity v)
231     {
232         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
233     }
234 
235     /**
236      * Calculate the multiplication of FloatDensity and FloatVolume, which results in a FloatMass scalar.
237      * @param v FloatDensity; scalar
238      * @return FloatMass; scalar as a multiplication of FloatDensity and FloatVolume
239      */
240     public final FloatMass times(final FloatVolume v)
241     {
242         return new FloatMass(this.si * v.si, MassUnit.SI);
243     }
244 
245     /**
246      * Calculate the multiplication of FloatDensity and FloatFlowVolume, which results in a FloatFlowMass scalar.
247      * @param v FloatDensity; scalar
248      * @return FloatFlowMass; scalar as a multiplication of FloatDensity and FloatFlowVolume
249      */
250     public final FloatFlowMass times(final FloatFlowVolume v)
251     {
252         return new FloatFlowMass(this.si * v.si, FlowMassUnit.SI);
253     }
254 
255     /** {@inheritDoc} */
256     @Override
257     public FloatSIScalar reciprocal()
258     {
259         return FloatScalar.divide(FloatDimensionless.ONE, this);
260     }
261 
262 }