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.DimensionlessUnit;
7   import org.djunits.unit.TemperatureUnit;
8   import org.djunits.unit.Unit;
9   
10  /**
11   * Easy access methods for the %Type% FloatScalar. Instead of:
12   * 
13   * <pre>
14   * FloatScalar.Rel&lt;TemperatureUnit&gt; value = new FloatScalar.Rel&lt;TemperatureUnit&gt;(100.0, TemperatureUnit.SI);
15   * </pre>
16   * 
17   * we can now write:
18   * 
19   * <pre>
20   * FloatTemperature value = new FloatTemperature(100.0, TemperatureUnit.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-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, 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 FloatTemperature extends AbstractFloatScalarRel<TemperatureUnit, FloatTemperature>
36  {
37      /** */
38      private static final long serialVersionUID = 20150901L;
39  
40      /** constant with value zero. */
41      public static final FloatTemperature ZERO = new FloatTemperature(0.0f, TemperatureUnit.SI);
42  
43      /** constant with value NaN. */
44      @SuppressWarnings("checkstyle:constantname")
45      public static final FloatTemperature NaN = new FloatTemperature(Float.NaN, TemperatureUnit.SI);
46  
47      /** constant with value POSITIVE_INFINITY. */
48      public static final FloatTemperature POSITIVE_INFINITY = new FloatTemperature(Float.POSITIVE_INFINITY, TemperatureUnit.SI);
49  
50      /** constant with value NEGATIVE_INFINITY. */
51      public static final FloatTemperature NEGATIVE_INFINITY = new FloatTemperature(Float.NEGATIVE_INFINITY, TemperatureUnit.SI);
52  
53      /** constant with value MAX_VALUE. */
54      public static final FloatTemperature POS_MAXVALUE = new FloatTemperature(Float.MAX_VALUE, TemperatureUnit.SI);
55  
56      /** constant with value -MAX_VALUE. */
57      public static final FloatTemperature NEG_MAXVALUE = new FloatTemperature(-Float.MAX_VALUE, TemperatureUnit.SI);
58  
59      /**
60       * Construct FloatTemperature scalar.
61       * @param value float value
62       * @param unit unit for the float value
63       */
64      public FloatTemperature(final float value, final TemperatureUnit unit)
65      {
66          super(value, unit);
67      }
68  
69      /**
70       * Construct FloatTemperature scalar.
71       * @param value Scalar from which to construct this instance
72       */
73      public FloatTemperature(final FloatTemperature value)
74      {
75          super(value);
76      }
77  
78      /**
79       * Construct FloatTemperature scalar using a double value.
80       * @param value double value
81       * @param unit unit for the resulting float value
82       */
83      public FloatTemperature(final double value, final TemperatureUnit unit)
84      {
85          super((float) value, unit);
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public final FloatTemperature instantiateRel(final float value, final TemperatureUnit unit)
91      {
92          return new FloatTemperature(value, unit);
93      }
94  
95      /**
96       * Construct FloatTemperature scalar.
97       * @param value float value in SI units
98       * @return the new scalar with the SI value
99       */
100     public static final FloatTemperature createSI(final float value)
101     {
102         return new FloatTemperature(value, TemperatureUnit.SI);
103     }
104 
105     /**
106      * Construct a new Absolute Immutable FloatScalar of the right type. Each extending class must implement this method.
107      * @param value the float value
108      * @param unit the unit
109      * @return A a new absolute instance of the FloatScalar of the right type
110      */
111     public final FloatAbsoluteTemperature instantiateAbs(final float value, final AbsoluteTemperatureUnit unit)
112     {
113         return new FloatAbsoluteTemperature(value, unit);
114     }
115 
116     /**
117      * Interpolate between two values.
118      * @param zero the low value
119      * @param one the high value
120      * @param ratio the ratio between 0 and 1, inclusive
121      * @return a Scalar at the ratio between
122      */
123     public static FloatTemperature interpolate(final FloatTemperature zero, final FloatTemperature one, final float ratio)
124     {
125         return new FloatTemperature(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
126     }
127 
128     /**
129      * Relative scalar plus Absolute scalar = Absolute scalar.
130      * @param v the value to add
131      * @return sum of this value and v as a new object
132      */
133     public final FloatAbsoluteTemperature plus(final FloatAbsoluteTemperature v)
134     {
135         AbsoluteTemperatureUnit targetUnit = v.getUnit();
136         return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
137     }
138 
139     /**
140      * Return the maximum value of two relative scalars.
141      * @param r1 the first scalar
142      * @param r2 the second scalar
143      * @return the maximum value of two relative scalars
144      */
145     public static FloatTemperature max(final FloatTemperature r1, final FloatTemperature r2)
146     {
147         return (r1.gt(r2)) ? r1 : r2;
148     }
149 
150     /**
151      * Return the maximum value of more than two relative scalars.
152      * @param r1 the first scalar
153      * @param r2 the second scalar
154      * @param rn the other scalars
155      * @return the maximum value of more than two relative scalars
156      */
157     public static FloatTemperature max(final FloatTemperature r1, final FloatTemperature r2, final FloatTemperature... rn)
158     {
159         FloatTemperature maxr = (r1.gt(r2)) ? r1 : r2;
160         for (FloatTemperature r : rn)
161         {
162             if (r.gt(maxr))
163             {
164                 maxr = r;
165             }
166         }
167         return maxr;
168     }
169 
170     /**
171      * Return the minimum value of two relative scalars.
172      * @param r1 the first scalar
173      * @param r2 the second scalar
174      * @return the minimum value of two relative scalars
175      */
176     public static FloatTemperature min(final FloatTemperature r1, final FloatTemperature r2)
177     {
178         return (r1.lt(r2)) ? r1 : r2;
179     }
180 
181     /**
182      * Return the minimum value of more than two relative scalars.
183      * @param r1 the first scalar
184      * @param r2 the second scalar
185      * @param rn the other scalars
186      * @return the minimum value of more than two relative scalars
187      */
188     public static FloatTemperature min(final FloatTemperature r1, final FloatTemperature r2, final FloatTemperature... rn)
189     {
190         FloatTemperature minr = (r1.lt(r2)) ? r1 : r2;
191         for (FloatTemperature r : rn)
192         {
193             if (r.lt(minr))
194             {
195                 minr = r;
196             }
197         }
198         return minr;
199     }
200 
201     /**
202      * Returns a FloatTemperature representation of a textual representation of a value with a unit. The String representation
203      * that can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are
204      * allowed, but not necessary, between the value and the unit.
205      * @param text String; the textual representation to parse into a FloatTemperature
206      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
207      * @throws IllegalArgumentException when the text cannot be parsed
208      */
209     public static FloatTemperature valueOf(final String text) throws IllegalArgumentException
210     {
211         if (text == null || text.length() == 0)
212         {
213             throw new IllegalArgumentException("Error parsing FloatTemperature -- null or empty argument");
214         }
215         Matcher matcher = NUMBER_PATTERN.matcher(text);
216         if (matcher.find())
217         {
218             int index = matcher.end();
219             try
220             {
221                 String unitString = text.substring(index).trim();
222                 String valueString = text.substring(0, index).trim();
223                 for (TemperatureUnit unit : Unit.getUnits(TemperatureUnit.class))
224                 {
225                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
226                     {
227                         float f = Float.parseFloat(valueString);
228                         return new FloatTemperature(f, unit);
229                     }
230                 }
231             }
232             catch (Exception exception)
233             {
234                 throw new IllegalArgumentException("Error parsing FloatTemperature from " + text, exception);
235             }
236         }
237         throw new IllegalArgumentException("Error parsing FloatTemperature from " + text);
238     }
239 
240     /**
241      * Calculate the division of FloatTemperature and FloatTemperature, which results in a FloatDimensionless scalar.
242      * @param v FloatTemperature scalar
243      * @return FloatDimensionless scalar as a division of FloatTemperature and FloatTemperature
244      */
245     public final FloatDimensionless divideBy(final FloatTemperature v)
246     {
247         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
248     }
249 
250 }