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.ElectricalCurrentUnit;
7   import org.djunits.unit.ElectricalPotentialUnit;
8   import org.djunits.unit.EnergyUnit;
9   import org.djunits.unit.ForceUnit;
10  import org.djunits.unit.FrequencyUnit;
11  import org.djunits.unit.PowerUnit;
12  import org.djunits.unit.SpeedUnit;
13  import org.djunits.unit.Unit;
14  
15  /**
16   * Easy access methods for the Power FloatScalar, which is relative by definition. An example is Speed. Instead of:
17   * 
18   * <pre>
19   * FloatScalar.Rel&lt;PowerUnit&gt; value = new FloatScalar.Rel&lt;PowerUnit&gt;(100.0, PowerUnit.SI);
20   * </pre>
21   * 
22   * we can now write:
23   * 
24   * <pre>
25   * FloatPower value = new FloatPower(100.0, PowerUnit.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 FloatPower extends AbstractFloatScalarRel<PowerUnit, FloatPower>
40  {
41      /** */
42      private static final long serialVersionUID = 20150901L;
43  
44      /** constant with value zero. */
45      public static final FloatPower ZERO = new FloatPower(0.0f, PowerUnit.SI);
46  
47      /** constant with value NaN. */
48      @SuppressWarnings("checkstyle:constantname")
49      public static final FloatPower NaN = new FloatPower(Float.NaN, PowerUnit.SI);
50  
51      /** constant with value POSITIVE_INFINITY. */
52      public static final FloatPower POSITIVE_INFINITY = new FloatPower(Float.POSITIVE_INFINITY, PowerUnit.SI);
53  
54      /** constant with value NEGATIVE_INFINITY. */
55      public static final FloatPower NEGATIVE_INFINITY = new FloatPower(Float.NEGATIVE_INFINITY, PowerUnit.SI);
56  
57      /** constant with value MAX_VALUE. */
58      public static final FloatPower POS_MAXVALUE = new FloatPower(Float.MAX_VALUE, PowerUnit.SI);
59  
60      /** constant with value -MAX_VALUE. */
61      public static final FloatPower NEG_MAXVALUE = new FloatPower(-Float.MAX_VALUE, PowerUnit.SI);
62  
63      /**
64       * Construct FloatPower scalar.
65       * @param value float value
66       * @param unit unit for the float value
67       */
68      public FloatPower(final float value, final PowerUnit unit)
69      {
70          super(value, unit);
71      }
72  
73      /**
74       * Construct FloatPower scalar.
75       * @param value Scalar from which to construct this instance
76       */
77      public FloatPower(final FloatPower value)
78      {
79          super(value);
80      }
81  
82      /**
83       * Construct FloatPower scalar using a double value.
84       * @param value double value
85       * @param unit unit for the resulting float value
86       */
87      public FloatPower(final double value, final PowerUnit unit)
88      {
89          super((float) value, unit);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public final FloatPower instantiateRel(final float value, final PowerUnit unit)
95      {
96          return new FloatPower(value, unit);
97      }
98  
99      /**
100      * Construct FloatPower scalar.
101      * @param value float value in SI units
102      * @return the new scalar with the SI value
103      */
104     public static final FloatPower createSI(final float value)
105     {
106         return new FloatPower(value, PowerUnit.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 FloatPower interpolate(final FloatPower zero, final FloatPower one, final float ratio)
117     {
118         return new FloatPower(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 FloatPower max(final FloatPower r1, final FloatPower 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 FloatPower max(final FloatPower r1, final FloatPower r2, final FloatPower... rn)
140     {
141         FloatPower maxr = (r1.gt(r2)) ? r1 : r2;
142         for (FloatPower 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 FloatPower min(final FloatPower r1, final FloatPower 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 FloatPower min(final FloatPower r1, final FloatPower r2, final FloatPower... rn)
171     {
172         FloatPower minr = (r1.lt(r2)) ? r1 : r2;
173         for (FloatPower r : rn)
174         {
175             if (r.lt(minr))
176             {
177                 minr = r;
178             }
179         }
180         return minr;
181     }
182 
183     /**
184      * Returns a FloatPower 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 FloatPower
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 FloatPower valueOf(final String text) throws IllegalArgumentException
192     {
193         if (text == null || text.length() == 0)
194         {
195             throw new IllegalArgumentException("Error parsing FloatPower -- 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 (PowerUnit unit : Unit.getUnits(PowerUnit.class))
206                 {
207                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
208                     {
209                         float f = Float.parseFloat(valueString);
210                         return new FloatPower(f, unit);
211                     }
212                 }
213             }
214             catch (Exception exception)
215             {
216                 throw new IllegalArgumentException("Error parsing FloatPower from " + text, exception);
217             }
218         }
219         throw new IllegalArgumentException("Error parsing FloatPower from " + text);
220     }
221 
222     /**
223      * Calculate the division of FloatPower and FloatPower, which results in a FloatDimensionless scalar.
224      * @param v FloatPower scalar
225      * @return FloatDimensionless scalar as a division of FloatPower and FloatPower
226      */
227     public final FloatDimensionless divideBy(final FloatPower v)
228     {
229         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
230     }
231 
232     /**
233      * Calculate the multiplication of FloatPower and FloatDuration, which results in a FloatEnergy scalar.
234      * @param v FloatPower scalar
235      * @return FloatEnergy scalar as a multiplication of FloatPower and FloatDuration
236      */
237     public final FloatEnergy multiplyBy(final FloatDuration v)
238     {
239         return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
240     }
241 
242     /**
243      * Calculate the division of FloatPower and FloatFrequency, which results in a FloatEnergy scalar.
244      * @param v FloatPower scalar
245      * @return FloatEnergy scalar as a division of FloatPower and FloatFrequency
246      */
247     public final FloatEnergy divideBy(final FloatFrequency v)
248     {
249         return new FloatEnergy(this.si / v.si, EnergyUnit.SI);
250     }
251 
252     /**
253      * Calculate the division of FloatPower and FloatEnergy, which results in a FloatFrequency scalar.
254      * @param v FloatPower scalar
255      * @return FloatFrequency scalar as a division of FloatPower and FloatEnergy
256      */
257     public final FloatFrequency divideBy(final FloatEnergy v)
258     {
259         return new FloatFrequency(this.si / v.si, FrequencyUnit.SI);
260     }
261 
262     /**
263      * Calculate the division of FloatPower and FloatSpeed, which results in a FloatForce scalar.
264      * @param v FloatPower scalar
265      * @return FloatForce scalar as a division of FloatPower and FloatSpeed
266      */
267     public final FloatForce divideBy(final FloatSpeed v)
268     {
269         return new FloatForce(this.si / v.si, ForceUnit.SI);
270     }
271 
272     /**
273      * Calculate the division of FloatPower and FloatForce, which results in a FloatSpeed scalar.
274      * @param v FloatPower scalar
275      * @return FloatSpeed scalar as a division of FloatPower and FloatForce
276      */
277     public final FloatSpeed divideBy(final FloatForce v)
278     {
279         return new FloatSpeed(this.si / v.si, SpeedUnit.SI);
280     }
281 
282     /**
283      * Calculate the division of FloatPower and FloatElectricalPotential, which results in a FloatElectricalCurrent scalar.
284      * @param v FloatPower scalar
285      * @return FloatElectricalCurrent scalar as a division of FloatPower and FloatElectricalPotential
286      */
287     public final FloatElectricalCurrent divideBy(final FloatElectricalPotential v)
288     {
289         return new FloatElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
290     }
291 
292     /**
293      * Calculate the division of FloatPower and FloatElectricalCurrent, which results in a FloatElectricalPotential scalar.
294      * @param v FloatPower scalar
295      * @return FloatElectricalPotential scalar as a division of FloatPower and FloatElectricalCurrent
296      */
297     public final FloatElectricalPotential divideBy(final FloatElectricalCurrent v)
298     {
299         return new FloatElectricalPotential(this.si / v.si, ElectricalPotentialUnit.SI);
300     }
301 
302 }