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