View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.AbsoluteTemperatureUnit;
6   import org.djunits.unit.TemperatureUnit;
7   import org.djunits.unit.Unit;
8   
9   /**
10   * Easy access methods for the Absolute AbsoluteTemperature DoubleScalar. Instead of:
11   * 
12   * <pre>
13   * DoubleScalar.Abs&lt;AbsoluteTemperatureUnit&gt; value =
14   *         new DoubleScalar.Abs&lt;AbsoluteTemperatureUnit&gt;(100.0, AbsoluteTemperatureUnit.SI);
15   * </pre>
16   * 
17   * we can now write:
18   * 
19   * <pre>
20   * AbsoluteTemperature value = new AbsoluteTemperature(100.0, AbsoluteTemperatureUnit.BASE);
21   * </pre>
22   * 
23   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
24   * used are compatible.
25   * <p>
26   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
27   * All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * <p>
30   * $LastChangedDate: 2015-12-22 04:32:39 +0100 (Tue, 22 Dec 2015) $, @version $Revision: 180 $, by $Author: averbraeck $,
31   * initial version Sep 1, 2015 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   */
35  public class AbsoluteTemperature
36          extends AbstractDoubleScalarAbs<AbsoluteTemperatureUnit, AbsoluteTemperature, TemperatureUnit, Temperature>
37  {
38      /** */
39      private static final long serialVersionUID = 20150901L;
40  
41      /** constant with value zero. */
42      public static final AbsoluteTemperature ZERO = new AbsoluteTemperature(0.0, AbsoluteTemperatureUnit.BASE);
43  
44      /**
45       * Construct AbsoluteTemperature scalar.
46       * @param value double value
47       * @param unit unit for the double value
48       */
49      public AbsoluteTemperature(final double value, final AbsoluteTemperatureUnit unit)
50      {
51          super(value, unit);
52      }
53  
54      /**
55       * Construct AbsoluteTemperature scalar.
56       * @param value Scalar from which to construct this instance
57       */
58      public AbsoluteTemperature(final AbsoluteTemperature value)
59      {
60          super(value);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public final AbsoluteTemperature instantiateAbs(final double value, final AbsoluteTemperatureUnit unit)
66      {
67          return new AbsoluteTemperature(value, unit);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public final Temperature instantiateRel(final double value, final TemperatureUnit unit)
73      {
74          return new Temperature(value, unit);
75      }
76  
77      /**
78       * Construct %TypeAbsl% scalar.
79       * @param value double value in SI units
80       * @return the new scalar with the SI value
81       */
82      public static final AbsoluteTemperature createSI(final double value)
83      {
84          return new AbsoluteTemperature(value, AbsoluteTemperatureUnit.BASE);
85      }
86  
87      /**
88       * Interpolate between two values.
89       * @param zero the low value
90       * @param one the high value
91       * @param ratio the ratio between 0 and 1, inclusive
92       * @return a Scalar at the ratio between
93       */
94      public static AbsoluteTemperature interpolate(final AbsoluteTemperature zero, final AbsoluteTemperature one,
95              final double ratio)
96      {
97          return new AbsoluteTemperature(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
98      }
99  
100     /**
101      * Return the maximum value of two absolute scalars.
102      * @param a1 the first scalar
103      * @param a2 the second scalar
104      * @return the maximum value of two absolute scalars
105      */
106     public static AbsoluteTemperature max(final AbsoluteTemperature a1, final AbsoluteTemperature a2)
107     {
108         return (a1.gt(a2)) ? a1 : a2;
109     }
110 
111     /**
112      * Return the maximum value of more than two absolute scalars.
113      * @param a1 the first scalar
114      * @param a2 the second scalar
115      * @param an the other scalars
116      * @return the maximum value of more than two absolute scalars
117      */
118     public static AbsoluteTemperature max(final AbsoluteTemperature a1, final AbsoluteTemperature a2,
119             final AbsoluteTemperature... an)
120     {
121         AbsoluteTemperature maxa = (a1.gt(a2)) ? a1 : a2;
122         for (AbsoluteTemperature a : an)
123         {
124             if (a.gt(maxa))
125             {
126                 maxa = a;
127             }
128         }
129         return maxa;
130     }
131 
132     /**
133      * Return the minimum value of two absolute scalars.
134      * @param a1 the first scalar
135      * @param a2 the second scalar
136      * @return the minimum value of two absolute scalars
137      */
138     public static AbsoluteTemperature min(final AbsoluteTemperature a1, final AbsoluteTemperature a2)
139     {
140         return (a1.lt(a2)) ? a1 : a2;
141     }
142 
143     /**
144      * Return the minimum value of more than two absolute scalars.
145      * @param a1 the first scalar
146      * @param a2 the second scalar
147      * @param an the other scalars
148      * @return the minimum value of more than two absolute scalars
149      */
150     public static AbsoluteTemperature min(final AbsoluteTemperature a1, final AbsoluteTemperature a2,
151             final AbsoluteTemperature... an)
152     {
153         AbsoluteTemperature mina = (a1.lt(a2)) ? a1 : a2;
154         for (AbsoluteTemperature a : an)
155         {
156             if (a.lt(mina))
157             {
158                 mina = a;
159             }
160         }
161         return mina;
162     }
163 
164     /**
165      * Returns a AbsoluteTemperature representation of a textual representation of a value with a unit. The String
166      * representation that can be parsed is the double value in the unit, followed by the official abbreviation of the unit.
167      * Spaces are allowed, but not necessary, between the value and the unit.
168      * @param text String; the textual representation to parse into a AbsoluteTemperature
169      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
170      * @throws IllegalArgumentException when the text cannot be parsed
171      */
172     public static AbsoluteTemperature valueOf(final String text) throws IllegalArgumentException
173     {
174         if (text == null || text.length() == 0)
175         {
176             throw new IllegalArgumentException("Error parsing AbsoluteTemperature -- null or empty argument");
177         }
178         Matcher matcher = NUMBER_PATTERN.matcher(text);
179         if (matcher.find())
180         {
181             int index = matcher.end();
182             try
183             {
184                 String unitString = text.substring(index).trim();
185                 String valueString = text.substring(0, index).trim();
186                 for (AbsoluteTemperatureUnit unit : Unit.getUnits(AbsoluteTemperatureUnit.class))
187                 {
188                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
189                     {
190                         double d = Double.parseDouble(valueString);
191                         return new AbsoluteTemperature(d, unit);
192                     }
193                 }
194             }
195             catch (Exception exception)
196             {
197                 throw new IllegalArgumentException("Error parsing AbsoluteTemperature from " + text, exception);
198             }
199         }
200         throw new IllegalArgumentException("Error parsing AbsoluteTemperature from " + text);
201     }
202 
203 }