View Javadoc
1   package org.djunits.value.vdouble.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 DoubleScalar, which is relative by definition. Instead of:
17   * 
18   * <pre>
19   * DoubleScalar.Rel&lt;PowerUnit&gt; value = new DoubleScalar.Rel&lt;PowerUnit&gt;(100.0, PowerUnit.SI);
20   * </pre>
21   * 
22   * we can now write:
23   * 
24   * <pre>
25   * Power value = new Power(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 Power extends AbstractDoubleScalarRel<PowerUnit, Power>
40  {
41      /** */
42      private static final long serialVersionUID = 20150905L;
43  
44      /** constant with value zero. */
45      public static final Power ZERO = new Power(0.0, PowerUnit.SI);
46  
47      /** constant with value NaN. */
48      @SuppressWarnings("checkstyle:constantname")
49      public static final Power NaN = new Power(Double.NaN, PowerUnit.SI);
50  
51      /** constant with value POSITIVE_INFINITY. */
52      public static final Power POSITIVE_INFINITY = new Power(Double.POSITIVE_INFINITY, PowerUnit.SI);
53  
54      /** constant with value NEGATIVE_INFINITY. */
55      public static final Power NEGATIVE_INFINITY = new Power(Double.NEGATIVE_INFINITY, PowerUnit.SI);
56  
57      /** constant with value MAX_VALUE. */
58      public static final Power POS_MAXVALUE = new Power(Double.MAX_VALUE, PowerUnit.SI);
59  
60      /** constant with value -MAX_VALUE. */
61      public static final Power NEG_MAXVALUE = new Power(-Double.MAX_VALUE, PowerUnit.SI);
62  
63      /**
64       * Construct Power scalar.
65       * @param value double value
66       * @param unit unit for the double value
67       */
68      public Power(final double value, final PowerUnit unit)
69      {
70          super(value, unit);
71      }
72  
73      /**
74       * Construct Power scalar.
75       * @param value Scalar from which to construct this instance
76       */
77      public Power(final Power value)
78      {
79          super(value);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final Power instantiateRel(final double value, final PowerUnit unit)
85      {
86          return new Power(value, unit);
87      }
88  
89      /**
90       * Construct Power scalar.
91       * @param value double value in SI units
92       * @return the new scalar with the SI value
93       */
94      public static final Power createSI(final double value)
95      {
96          return new Power(value, PowerUnit.SI);
97      }
98  
99      /**
100      * Interpolate between two values.
101      * @param zero the low value
102      * @param one the high value
103      * @param ratio the ratio between 0 and 1, inclusive
104      * @return a Scalar at the ratio between
105      */
106     public static Power interpolate(final Power zero, final Power one, final double ratio)
107     {
108         return new Power(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
109     }
110 
111     /**
112      * Return the maximum value of two relative scalars.
113      * @param r1 the first scalar
114      * @param r2 the second scalar
115      * @return the maximum value of two relative scalars
116      */
117     public static Power max(final Power r1, final Power r2)
118     {
119         return (r1.gt(r2)) ? r1 : r2;
120     }
121 
122     /**
123      * Return the maximum value of more than two relative scalars.
124      * @param r1 the first scalar
125      * @param r2 the second scalar
126      * @param rn the other scalars
127      * @return the maximum value of more than two relative scalars
128      */
129     public static Power max(final Power r1, final Power r2, final Power... rn)
130     {
131         Power maxr = (r1.gt(r2)) ? r1 : r2;
132         for (Power r : rn)
133         {
134             if (r.gt(maxr))
135             {
136                 maxr = r;
137             }
138         }
139         return maxr;
140     }
141 
142     /**
143      * Return the minimum value of two relative scalars.
144      * @param r1 the first scalar
145      * @param r2 the second scalar
146      * @return the minimum value of two relative scalars
147      */
148     public static Power min(final Power r1, final Power r2)
149     {
150         return (r1.lt(r2)) ? r1 : r2;
151     }
152 
153     /**
154      * Return the minimum value of more than two relative scalars.
155      * @param r1 the first scalar
156      * @param r2 the second scalar
157      * @param rn the other scalars
158      * @return the minimum value of more than two relative scalars
159      */
160     public static Power min(final Power r1, final Power r2, final Power... rn)
161     {
162         Power minr = (r1.lt(r2)) ? r1 : r2;
163         for (Power r : rn)
164         {
165             if (r.lt(minr))
166             {
167                 minr = r;
168             }
169         }
170         return minr;
171     }
172 
173     /**
174      * Returns a Power representation of a textual representation of a value with a unit. The String representation that can be
175      * parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
176      * necessary, between the value and the unit.
177      * @param text String; the textual representation to parse into a Power
178      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
179      * @throws IllegalArgumentException when the text cannot be parsed
180      */
181     public static Power valueOf(final String text) throws IllegalArgumentException
182     {
183         if (text == null || text.length() == 0)
184         {
185             throw new IllegalArgumentException("Error parsing Power -- null or empty argument");
186         }
187         Matcher matcher = NUMBER_PATTERN.matcher(text);
188         if (matcher.find())
189         {
190             int index = matcher.end();
191             try
192             {
193                 String unitString = text.substring(index).trim();
194                 String valueString = text.substring(0, index).trim();
195                 for (PowerUnit unit : Unit.getUnits(PowerUnit.class))
196                 {
197                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
198                     {
199                         double d = Double.parseDouble(valueString);
200                         return new Power(d, unit);
201                     }
202                 }
203             }
204             catch (Exception exception)
205             {
206                 throw new IllegalArgumentException("Error parsing Power from " + text, exception);
207             }
208         }
209         throw new IllegalArgumentException("Error parsing Power from " + text);
210     }
211 
212     /**
213      * Calculate the division of Power and Power, which results in a Dimensionless scalar.
214      * @param v Power scalar
215      * @return Dimensionless scalar as a division of Power and Power
216      */
217     public final Dimensionless divideBy(final Power v)
218     {
219         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
220     }
221 
222     /**
223      * Calculate the multiplication of Power and Duration, which results in a Energy scalar.
224      * @param v Power scalar
225      * @return Energy scalar as a multiplication of Power and Duration
226      */
227     public final Energy multiplyBy(final Duration v)
228     {
229         return new Energy(this.si * v.si, EnergyUnit.SI);
230     }
231 
232     /**
233      * Calculate the division of Power and Frequency, which results in a Energy scalar.
234      * @param v Power scalar
235      * @return Energy scalar as a division of Power and Frequency
236      */
237     public final Energy divideBy(final Frequency v)
238     {
239         return new Energy(this.si / v.si, EnergyUnit.SI);
240     }
241 
242     /**
243      * Calculate the division of Power and Energy, which results in a Frequency scalar.
244      * @param v Power scalar
245      * @return Frequency scalar as a division of Power and Energy
246      */
247     public final Frequency divideBy(final Energy v)
248     {
249         return new Frequency(this.si / v.si, FrequencyUnit.SI);
250     }
251 
252     /**
253      * Calculate the division of Power and Speed, which results in a Force scalar.
254      * @param v Power scalar
255      * @return Force scalar as a division of Power and Speed
256      */
257     public final Force divideBy(final Speed v)
258     {
259         return new Force(this.si / v.si, ForceUnit.SI);
260     }
261 
262     /**
263      * Calculate the division of Power and Force, which results in a Speed scalar.
264      * @param v Power scalar
265      * @return Speed scalar as a division of Power and Force
266      */
267     public final Speed divideBy(final Force v)
268     {
269         return new Speed(this.si / v.si, SpeedUnit.SI);
270     }
271 
272     /**
273      * Calculate the division of Power and ElectricalPotential, which results in a ElectricalCurrent scalar.
274      * @param v Power scalar
275      * @return ElectricalCurrent scalar as a division of Power and ElectricalPotential
276      */
277     public final ElectricalCurrent divideBy(final ElectricalPotential v)
278     {
279         return new ElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
280     }
281 
282     /**
283      * Calculate the division of Power and ElectricalCurrent, which results in a ElectricalPotential scalar.
284      * @param v Power scalar
285      * @return ElectricalPotential scalar as a division of Power and ElectricalCurrent
286      */
287     public final ElectricalPotential divideBy(final ElectricalCurrent v)
288     {
289         return new ElectricalPotential(this.si / v.si, ElectricalPotentialUnit.SI);
290     }
291 
292 }