View Javadoc
1   package org.djunits.value.vfloat.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 AbsoluteTemperature FloatScalar. Instead of:
11   * 
12   * <pre>
13   * FloatScalar.Abs&lt;AbsoluteTemperatureUnit&gt; value =
14   *         new FloatScalar.Abs&lt;AbsoluteTemperatureUnit&gt;(100.0, AbsoluteTemperatureUnit.SI);
15   * </pre>
16   * 
17   * we can now write:
18   * 
19   * <pre>
20   * FloatAbsoluteTemperature value = new FloatAbsoluteTemperature(100.0, AbsoluteTemperatureUnit.SI);
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: 2019-02-27 23:44:43 +0100 (Wed, 27 Feb 2019) $, @version $Revision: 333 $, 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 FloatAbsoluteTemperature
36          extends AbstractFloatScalarAbs<AbsoluteTemperatureUnit, FloatAbsoluteTemperature, TemperatureUnit, FloatTemperature>
37  {
38      /** */
39      private static final long serialVersionUID = 20150901L;
40  
41      /** constant with value zero. */
42      public static final FloatAbsoluteTemperature ZERO = new FloatAbsoluteTemperature(0.0f, AbsoluteTemperatureUnit.BASE);
43  
44      /**
45       * Construct FloatAbsoluteTemperature scalar.
46       * @param value float value
47       * @param unit unit for the float value
48       */
49      public FloatAbsoluteTemperature(final float value, final AbsoluteTemperatureUnit unit)
50      {
51          super(value, unit);
52      }
53  
54      /**
55       * Construct FloatAbsoluteTemperature scalar using a double value.
56       * @param value double value
57       * @param unit unit for the resulting float value
58       */
59      public FloatAbsoluteTemperature(final double value, final AbsoluteTemperatureUnit unit)
60      {
61          super((float) value, unit);
62      }
63  
64      /**
65       * Construct FloatAbsoluteTemperature scalar.
66       * @param value Scalar from which to construct this instance
67       */
68      public FloatAbsoluteTemperature(final FloatAbsoluteTemperature value)
69      {
70          super(value);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final FloatAbsoluteTemperature instantiateAbs(final float value, final AbsoluteTemperatureUnit unit)
76      {
77          return new FloatAbsoluteTemperature(value, unit);
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final FloatTemperature instantiateRel(final float value, final TemperatureUnit unit)
83      {
84          return new FloatTemperature(value, unit);
85      }
86  
87      /**
88       * Construct FloatAbsoluteTemperature scalar.
89       * @param value float value in BASE units
90       * @return the new scalar with the BASE value
91       */
92      public static final FloatAbsoluteTemperature createSI(final float value)
93      {
94          return new FloatAbsoluteTemperature(value, AbsoluteTemperatureUnit.BASE);
95      }
96  
97      /**
98       * Interpolate between two values.
99       * @param zero the low value
100      * @param one the high value
101      * @param ratio the ratio between 0 and 1, inclusive
102      * @return a Scalar at the ratio between
103      */
104     public static FloatAbsoluteTemperature interpolate(final FloatAbsoluteTemperature zero, final FloatAbsoluteTemperature one,
105             final float ratio)
106     {
107         return new FloatAbsoluteTemperature(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio,
108                 zero.getUnit());
109     }
110 
111     /**
112      * Return the maximum value of two absolute scalars.
113      * @param a1 the first scalar
114      * @param a2 the second scalar
115      * @return the maximum value of two absolute scalars
116      */
117     public static FloatAbsoluteTemperature max(final FloatAbsoluteTemperature a1, final FloatAbsoluteTemperature a2)
118     {
119         return (a1.gt(a2)) ? a1 : a2;
120     }
121 
122     /**
123      * Return the maximum value of more than two absolute scalars.
124      * @param a1 the first scalar
125      * @param a2 the second scalar
126      * @param an the other scalars
127      * @return the maximum value of more than two absolute scalars
128      */
129     public static FloatAbsoluteTemperature max(final FloatAbsoluteTemperature a1, final FloatAbsoluteTemperature a2,
130             final FloatAbsoluteTemperature... an)
131     {
132         FloatAbsoluteTemperature maxa = (a1.gt(a2)) ? a1 : a2;
133         for (FloatAbsoluteTemperature a : an)
134         {
135             if (a.gt(maxa))
136             {
137                 maxa = a;
138             }
139         }
140         return maxa;
141     }
142 
143     /**
144      * Return the minimum value of two absolute scalars.
145      * @param a1 the first scalar
146      * @param a2 the second scalar
147      * @return the minimum value of two absolute scalars
148      */
149     public static FloatAbsoluteTemperature min(final FloatAbsoluteTemperature a1, final FloatAbsoluteTemperature a2)
150     {
151         return (a1.lt(a2)) ? a1 : a2;
152     }
153 
154     /**
155      * Return the minimum value of more than two absolute scalars.
156      * @param a1 the first scalar
157      * @param a2 the second scalar
158      * @param an the other scalars
159      * @return the minimum value of more than two absolute scalars
160      */
161     public static FloatAbsoluteTemperature min(final FloatAbsoluteTemperature a1, final FloatAbsoluteTemperature a2,
162             final FloatAbsoluteTemperature... an)
163     {
164         FloatAbsoluteTemperature mina = (a1.lt(a2)) ? a1 : a2;
165         for (FloatAbsoluteTemperature a : an)
166         {
167             if (a.lt(mina))
168             {
169                 mina = a;
170             }
171         }
172         return mina;
173     }
174 
175     /**
176      * Returns a FloatAbsoluteTemperature representation of a textual representation of a value with a unit. The String
177      * representation that can be parsed is the double value in the unit, followed by the official abbreviation of the unit.
178      * Spaces are allowed, but not necessary, between the value and the unit.
179      * @param text String; the textual representation to parse into a FloatAbsoluteTemperature
180      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
181      * @throws IllegalArgumentException when the text cannot be parsed
182      */
183     public static FloatAbsoluteTemperature valueOf(final String text) throws IllegalArgumentException
184     {
185         if (text == null || text.length() == 0)
186         {
187             throw new IllegalArgumentException("Error parsing FloatAbsoluteTemperature -- null or empty argument");
188         }
189         Matcher matcher = NUMBER_PATTERN.matcher(text);
190         if (matcher.find())
191         {
192             int index = matcher.end();
193             try
194             {
195                 String unitString = text.substring(index).trim();
196                 String valueString = text.substring(0, index).trim();
197                 for (AbsoluteTemperatureUnit unit : Unit.getUnits(AbsoluteTemperatureUnit.class))
198                 {
199                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
200                     {
201                         float f = Float.parseFloat(valueString);
202                         return new FloatAbsoluteTemperature(f, unit);
203                     }
204                 }
205             }
206             catch (Exception exception)
207             {
208                 throw new IllegalArgumentException("Error parsing FloatAbsoluteTemperature from " + text, exception);
209             }
210         }
211         throw new IllegalArgumentException("Error parsing FloatAbsoluteTemperature from " + text);
212     }
213 
214 }