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