View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.DimensionlessUnit;
6   import org.djunits.unit.DurationUnit;
7   import org.djunits.unit.EnergyUnit;
8   import org.djunits.unit.ForceUnit;
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.unit.MoneyUnit;
11  import org.djunits.unit.PowerUnit;
12  import org.djunits.unit.PressureUnit;
13  import org.djunits.unit.Unit;
14  
15  /**
16   * Easy access methods for the Energy FloatScalar, which is relative by definition. An example is Speed. Instead of:
17   * 
18   * <pre>
19   * FloatScalar.Rel&lt;EnergyUnit&gt; value = new FloatScalar.Rel&lt;EnergyUnit&gt;(100.0, EnergyUnit.SI);
20   * </pre>
21   * 
22   * we can now write:
23   * 
24   * <pre>
25   * FloatEnergy value = new FloatEnergy(100.0, EnergyUnit.SI);
26   * </pre>
27   * 
28   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
29   * used are compatible.
30   * <p>
31   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
32   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
33   * <p>
34   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
35   * initial version Sep 5, 2015 <br>
36   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
37   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
38   */
39  public class FloatEnergy extends AbstractFloatScalarRel<EnergyUnit, FloatEnergy>
40  {
41      /** */
42      private static final long serialVersionUID = 20150901L;
43  
44      /** constant with value zero. */
45      public static final FloatEnergy ZERO = new FloatEnergy(0.0f, EnergyUnit.SI);
46  
47      /** constant with value NaN. */
48      @SuppressWarnings("checkstyle:constantname")
49      public static final FloatEnergy NaN = new FloatEnergy(Float.NaN, EnergyUnit.SI);
50  
51      /** constant with value POSITIVE_INFINITY. */
52      public static final FloatEnergy POSITIVE_INFINITY = new FloatEnergy(Float.POSITIVE_INFINITY, EnergyUnit.SI);
53  
54      /** constant with value NEGATIVE_INFINITY. */
55      public static final FloatEnergy NEGATIVE_INFINITY = new FloatEnergy(Float.NEGATIVE_INFINITY, EnergyUnit.SI);
56  
57      /** constant with value MAX_VALUE. */
58      public static final FloatEnergy POS_MAXVALUE = new FloatEnergy(Float.MAX_VALUE, EnergyUnit.SI);
59  
60      /** constant with value -MAX_VALUE. */
61      public static final FloatEnergy NEG_MAXVALUE = new FloatEnergy(-Float.MAX_VALUE, EnergyUnit.SI);
62  
63      /**
64       * Construct FloatEnergy scalar.
65       * @param value float value
66       * @param unit unit for the float value
67       */
68      public FloatEnergy(final float value, final EnergyUnit unit)
69      {
70          super(value, unit);
71      }
72  
73      /**
74       * Construct FloatEnergy scalar.
75       * @param value Scalar from which to construct this instance
76       */
77      public FloatEnergy(final FloatEnergy value)
78      {
79          super(value);
80      }
81  
82      /**
83       * Construct FloatEnergy scalar using a double value.
84       * @param value double value
85       * @param unit unit for the resulting float value
86       */
87      public FloatEnergy(final double value, final EnergyUnit unit)
88      {
89          super((float) value, unit);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public final FloatEnergy instantiateRel(final float value, final EnergyUnit unit)
95      {
96          return new FloatEnergy(value, unit);
97      }
98  
99      /**
100      * Construct FloatEnergy scalar.
101      * @param value float value in SI units
102      * @return the new scalar with the SI value
103      */
104     public static final FloatEnergy createSI(final float value)
105     {
106         return new FloatEnergy(value, EnergyUnit.SI);
107     }
108 
109     /**
110      * Interpolate between two values.
111      * @param zero the low value
112      * @param one the high value
113      * @param ratio the ratio between 0 and 1, inclusive
114      * @return a Scalar at the ratio between
115      */
116     public static FloatEnergy interpolate(final FloatEnergy zero, final FloatEnergy one, final float ratio)
117     {
118         return new FloatEnergy(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
119     }
120 
121     /**
122      * Return the maximum value of two relative scalars.
123      * @param r1 the first scalar
124      * @param r2 the second scalar
125      * @return the maximum value of two relative scalars
126      */
127     public static FloatEnergy max(final FloatEnergy r1, final FloatEnergy r2)
128     {
129         return (r1.gt(r2)) ? r1 : r2;
130     }
131 
132     /**
133      * Return the maximum value of more than two relative scalars.
134      * @param r1 the first scalar
135      * @param r2 the second scalar
136      * @param rn the other scalars
137      * @return the maximum value of more than two relative scalars
138      */
139     public static FloatEnergy max(final FloatEnergy r1, final FloatEnergy r2, final FloatEnergy... rn)
140     {
141         FloatEnergy maxr = (r1.gt(r2)) ? r1 : r2;
142         for (FloatEnergy r : rn)
143         {
144             if (r.gt(maxr))
145             {
146                 maxr = r;
147             }
148         }
149         return maxr;
150     }
151 
152     /**
153      * Return the minimum value of two relative scalars.
154      * @param r1 the first scalar
155      * @param r2 the second scalar
156      * @return the minimum value of two relative scalars
157      */
158     public static FloatEnergy min(final FloatEnergy r1, final FloatEnergy r2)
159     {
160         return (r1.lt(r2)) ? r1 : r2;
161     }
162 
163     /**
164      * Return the minimum value of more than two relative scalars.
165      * @param r1 the first scalar
166      * @param r2 the second scalar
167      * @param rn the other scalars
168      * @return the minimum value of more than two relative scalars
169      */
170     public static FloatEnergy min(final FloatEnergy r1, final FloatEnergy r2, final FloatEnergy... rn)
171     {
172         FloatEnergy minr = (r1.lt(r2)) ? r1 : r2;
173         for (FloatEnergy r : rn)
174         {
175             if (r.lt(minr))
176             {
177                 minr = r;
178             }
179         }
180         return minr;
181     }
182 
183     /**
184      * Returns a FloatEnergy representation of a textual representation of a value with a unit. The String representation that
185      * can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but
186      * not necessary, between the value and the unit.
187      * @param text String; the textual representation to parse into a FloatEnergy
188      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
189      * @throws IllegalArgumentException when the text cannot be parsed
190      */
191     public static FloatEnergy valueOf(final String text) throws IllegalArgumentException
192     {
193         if (text == null || text.length() == 0)
194         {
195             throw new IllegalArgumentException("Error parsing FloatEnergy -- null or empty argument");
196         }
197         Matcher matcher = NUMBER_PATTERN.matcher(text);
198         if (matcher.find())
199         {
200             int index = matcher.end();
201             try
202             {
203                 String unitString = text.substring(index).trim();
204                 String valueString = text.substring(0, index).trim();
205                 for (EnergyUnit unit : Unit.getUnits(EnergyUnit.class))
206                 {
207                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
208                     {
209                         float f = Float.parseFloat(valueString);
210                         return new FloatEnergy(f, unit);
211                     }
212                 }
213             }
214             catch (Exception exception)
215             {
216                 throw new IllegalArgumentException("Error parsing FloatEnergy from " + text, exception);
217             }
218         }
219         throw new IllegalArgumentException("Error parsing FloatEnergy from " + text);
220     }
221 
222     /**
223      * Calculate the division of FloatEnergy and FloatEnergy, which results in a FloatDimensionless scalar.
224      * @param v FloatEnergy scalar
225      * @return FloatDimensionless scalar as a division of FloatEnergy and FloatEnergy
226      */
227     public final FloatDimensionless divideBy(final FloatEnergy v)
228     {
229         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
230     }
231 
232     /**
233      * Calculate the division of FloatEnergy and FloatForce, which results in a FloatLength scalar.
234      * @param v FloatEnergy scalar
235      * @return FloatLength scalar as a division of FloatEnergy and FloatForce
236      */
237     public final FloatLength divideBy(final FloatForce v)
238     {
239         return new FloatLength(this.si / v.si, LengthUnit.SI);
240     }
241 
242     /**
243      * Calculate the division of FloatEnergy and FloatLength, which results in a FloatForce scalar.
244      * @param v FloatEnergy scalar
245      * @return FloatForce scalar as a division of FloatEnergy and FloatLength
246      */
247     public final FloatForce divideBy(final FloatLength v)
248     {
249         return new FloatForce(this.si / v.si, ForceUnit.SI);
250     }
251 
252     /**
253      * Calculate the multiplication of FloatEnergy and FloatLinearDensity, which results in a FloatForce scalar.
254      * @param v FloatEnergy scalar
255      * @return FloatForce scalar as a multiplication of FloatEnergy and FloatLinearDensity
256      */
257     public final FloatForce multiplyBy(final FloatLinearDensity v)
258     {
259         return new FloatForce(this.si * v.si, ForceUnit.SI);
260     }
261 
262     /**
263      * Calculate the division of FloatEnergy and FloatDuration, which results in a FloatPower scalar.
264      * @param v FloatEnergy scalar
265      * @return FloatPower scalar as a division of FloatEnergy and FloatDuration
266      */
267     public final FloatPower divideBy(final FloatDuration v)
268     {
269         return new FloatPower(this.si / v.si, PowerUnit.SI);
270     }
271 
272     /**
273      * Calculate the division of FloatEnergy and FloatPower, which results in a FloatDuration scalar.
274      * @param v FloatEnergy scalar
275      * @return FloatDuration scalar as a division of FloatEnergy and FloatPower
276      */
277     public final FloatDuration divideBy(final FloatPower v)
278     {
279         return new FloatDuration(this.si / v.si, DurationUnit.SI);
280     }
281 
282     /**
283      * Calculate the division of FloatEnergy and FloatVolume, which results in a FloatPressure scalar.
284      * @param v FloatEnergy scalar
285      * @return FloatPressure scalar as a division of FloatEnergy and FloatVolume
286      */
287     public final FloatPressure divideBy(final FloatVolume v)
288     {
289         return new FloatPressure(this.si / v.si, PressureUnit.SI);
290     }
291 
292     /**
293      * Calculate the multiplication of FloatEnergy and FloatFrequency, which results in a FloatPower scalar.
294      * @param v FloatEnergy scalar
295      * @return FloatPower scalar as a multiplication of FloatEnergy and FloatFrequency
296      */
297     public final FloatPower multiplyBy(final FloatFrequency v)
298     {
299         return new FloatPower(this.si * v.si, PowerUnit.SI);
300     }
301 
302     /**
303      * Calculate the multiplication of FloatEnergy and FloatMoneyPerEnergy, which results in a FloatMoney scalar.
304      * @param v FloatEnergy scalar
305      * @return FloatMoney scalar as a multiplication of FloatEnergy and FloatMoneyPerEnergy
306      */
307     public final FloatMoney multiplyBy(final FloatMoneyPerEnergy v)
308     {
309         return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
310     }
311 
312 }