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