View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.AccelerationUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.ElectricalCurrentUnit;
8   import org.djunits.unit.ElectricalPotentialUnit;
9   import org.djunits.unit.EnergyUnit;
10  import org.djunits.unit.ForceUnit;
11  import org.djunits.unit.FrequencyUnit;
12  import org.djunits.unit.MomentumUnit;
13  import org.djunits.unit.PowerUnit;
14  import org.djunits.unit.SpeedUnit;
15  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
16  import org.djutils.base.NumberParser;
17  import org.djutils.exceptions.Throw;
18  
19  import jakarta.annotation.Generated;
20  
21  /**
22   * Easy access methods for the Power DoubleScalar, which is relative by definition.
23   * <p>
24   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
26   * </p>
27   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
29   */
30  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
31  public class Power extends DoubleScalarRel<PowerUnit, Power>
32  {
33      /** */
34      private static final long serialVersionUID = 20150905L;
35  
36      /** Constant with value zero. */
37      public static final Power ZERO = new Power(0.0, PowerUnit.SI);
38  
39      /** Constant with value one. */
40      public static final Power ONE = new Power(1.0, PowerUnit.SI);
41  
42      /** Constant with value NaN. */
43      @SuppressWarnings("checkstyle:constantname")
44      public static final Power NaN = new Power(Double.NaN, PowerUnit.SI);
45  
46      /** Constant with value POSITIVE_INFINITY. */
47      public static final Power POSITIVE_INFINITY = new Power(Double.POSITIVE_INFINITY, PowerUnit.SI);
48  
49      /** Constant with value NEGATIVE_INFINITY. */
50      public static final Power NEGATIVE_INFINITY = new Power(Double.NEGATIVE_INFINITY, PowerUnit.SI);
51  
52      /** Constant with value MAX_VALUE. */
53      public static final Power POS_MAXVALUE = new Power(Double.MAX_VALUE, PowerUnit.SI);
54  
55      /** Constant with value -MAX_VALUE. */
56      public static final Power NEG_MAXVALUE = new Power(-Double.MAX_VALUE, PowerUnit.SI);
57  
58      /**
59       * Construct Power scalar with a unit.
60       * @param value the double value, expressed in the given unit
61       * @param unit unit for the double value
62       */
63      public Power(final double value, final PowerUnit unit)
64      {
65          super(value, unit);
66      }
67  
68      /**
69       * Construct Power scalar.
70       * @param value Scalar from which to construct this instance
71       */
72      public Power(final Power value)
73      {
74          super(value);
75      }
76  
77      @Override
78      public final Power instantiateRel(final double value, final PowerUnit unit)
79      {
80          return new Power(value, unit);
81      }
82  
83      /**
84       * Construct Power scalar based on an SI value.
85       * @param value the double value in SI units
86       * @return the new scalar with the SI value
87       */
88      public static final Power ofSI(final double value)
89      {
90          return new Power(value, PowerUnit.SI);
91      }
92  
93      /**
94       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
95       * @param zero the value at a ratio of zero
96       * @param one the value at a ratio of one
97       * @param ratio the ratio between 0 and 1, inclusive
98       * @return a Power at the given ratio between 0 and 1
99       */
100     public static Power interpolate(final Power zero, final Power one, final double ratio)
101     {
102         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
103                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
104         return new Power(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
105     }
106 
107     /**
108      * Return the maximum value of two relative scalars.
109      * @param r1 the first scalar
110      * @param r2 the second scalar
111      * @return the maximum value of two relative scalars
112      */
113     public static Power max(final Power r1, final Power r2)
114     {
115         return r1.gt(r2) ? r1 : r2;
116     }
117 
118     /**
119      * Return the maximum value of more than two relative scalars.
120      * @param r1 the first scalar
121      * @param r2 the second scalar
122      * @param rn the other scalars
123      * @return the maximum value of more than two relative scalars
124      */
125     public static Power max(final Power r1, final Power r2, final Power... rn)
126     {
127         Power maxr = r1.gt(r2) ? r1 : r2;
128         for (Power r : rn)
129         {
130             if (r.gt(maxr))
131             {
132                 maxr = r;
133             }
134         }
135         return maxr;
136     }
137 
138     /**
139      * Return the minimum value of two relative scalars.
140      * @param r1 the first scalar
141      * @param r2 the second scalar
142      * @return the minimum value of two relative scalars
143      */
144     public static Power min(final Power r1, final Power r2)
145     {
146         return r1.lt(r2) ? r1 : r2;
147     }
148 
149     /**
150      * Return the minimum value of more than two relative scalars.
151      * @param r1 the first scalar
152      * @param r2 the second scalar
153      * @param rn the other scalars
154      * @return the minimum value of more than two relative scalars
155      */
156     public static Power min(final Power r1, final Power r2, final Power... rn)
157     {
158         Power minr = r1.lt(r2) ? r1 : r2;
159         for (Power r : rn)
160         {
161             if (r.lt(minr))
162             {
163                 minr = r;
164             }
165         }
166         return minr;
167     }
168 
169     /**
170      * Returns a Power representation of a textual representation of a value with a unit. The String representation that can be
171      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
172      * but not required, between the value and the unit.
173      * @param text the textual representation to parse into a Power
174      * @return the Scalar representation of the value in its unit
175      * @throws IllegalArgumentException when the text cannot be parsed
176      * @throws NullPointerException when the text argument is null
177      */
178     public static Power valueOf(final String text)
179     {
180         Throw.whenNull(text, "Error parsing Power: text to parse is null");
181         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Power: empty text to parse");
182         try
183         {
184             NumberParser numberParser = new NumberParser().lenient().trailing();
185             double d = numberParser.parseDouble(text);
186             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
187             PowerUnit unit = PowerUnit.BASE.getUnitByAbbreviation(unitString);
188             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Power", unitString);
189             return new Power(d, unit);
190         }
191         catch (Exception exception)
192         {
193             throw new IllegalArgumentException(
194                     "Error parsing Power from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
195                     exception);
196         }
197     }
198 
199     /**
200      * Returns a Power based on a value and the textual representation of the unit, which can be localized.
201      * @param value the value to use
202      * @param unitString the textual representation of the unit
203      * @return the Scalar representation of the value in its unit
204      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
205      * @throws NullPointerException when the unitString argument is null
206      */
207     public static Power of(final double value, final String unitString)
208     {
209         Throw.whenNull(unitString, "Error parsing Power: unitString is null");
210         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Power: empty unitString");
211         PowerUnit unit = PowerUnit.BASE.getUnitByAbbreviation(unitString);
212         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Power with unit %s", unitString);
213         return new Power(value, unit);
214     }
215 
216     /**
217      * Calculate the division of Power and Power, which results in a Dimensionless scalar.
218      * @param v scalar
219      * @return scalar as a division of Power and Power
220      */
221     public final Dimensionless divide(final Power v)
222     {
223         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
224     }
225 
226     /**
227      * Calculate the multiplication of Power and Duration, which results in a Energy scalar.
228      * @param v scalar
229      * @return scalar as a multiplication of Power and Duration
230      */
231     public final Energy times(final Duration v)
232     {
233         return new Energy(this.si * v.si, EnergyUnit.SI);
234     }
235 
236     /**
237      * Calculate the division of Power and Frequency, which results in a Energy scalar.
238      * @param v scalar
239      * @return scalar as a division of Power and Frequency
240      */
241     public final Energy divide(final Frequency v)
242     {
243         return new Energy(this.si / v.si, EnergyUnit.SI);
244     }
245 
246     /**
247      * Calculate the division of Power and Energy, which results in a Frequency scalar.
248      * @param v scalar
249      * @return scalar as a division of Power and Energy
250      */
251     public final Frequency divide(final Energy v)
252     {
253         return new Frequency(this.si / v.si, FrequencyUnit.SI);
254     }
255 
256     /**
257      * Calculate the division of Power and Speed, which results in a Force scalar.
258      * @param v scalar
259      * @return scalar as a division of Power and Speed
260      */
261     public final Force divide(final Speed v)
262     {
263         return new Force(this.si / v.si, ForceUnit.SI);
264     }
265 
266     /**
267      * Calculate the division of Power and Force, which results in a Speed scalar.
268      * @param v scalar
269      * @return scalar as a division of Power and Force
270      */
271     public final Speed divide(final Force v)
272     {
273         return new Speed(this.si / v.si, SpeedUnit.SI);
274     }
275 
276     /**
277      * Calculate the division of Power and ElectricalPotential, which results in a ElectricalCurrent scalar.
278      * @param v scalar
279      * @return scalar as a division of Power and ElectricalPotential
280      */
281     public final ElectricalCurrent divide(final ElectricalPotential v)
282     {
283         return new ElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
284     }
285 
286     /**
287      * Calculate the division of Power and ElectricalCurrent, which results in a ElectricalPotential scalar.
288      * @param v scalar
289      * @return scalar as a division of Power and ElectricalCurrent
290      */
291     public final ElectricalPotential divide(final ElectricalCurrent v)
292     {
293         return new ElectricalPotential(this.si / v.si, ElectricalPotentialUnit.SI);
294     }
295 
296     /**
297      * Calculate the division of Power and Acceleration, which results in a Momentum scalar.
298      * @param v scalar
299      * @return scalar as a division of Power and Acceleration
300      */
301     public final Momentum divide(final Acceleration v)
302     {
303         return new Momentum(this.si / v.si, MomentumUnit.SI);
304     }
305 
306     /**
307      * Calculate the division of Power and Momentum, which results in a Acceleration scalar.
308      * @param v scalar
309      * @return scalar as a division of Power and Momentum
310      */
311     public final Acceleration divide(final Momentum v)
312     {
313         return new Acceleration(this.si / v.si, AccelerationUnit.SI);
314     }
315 
316     @Override
317     public SIScalar reciprocal()
318     {
319         return SIScalar.divide(Dimensionless.ONE, this);
320     }
321 
322     /**
323      * Multiply two scalars that result in a scalar of type Power.
324      * @param scalar1 the first scalar
325      * @param scalar2 the second scalar
326      * @return the multiplication of both scalars as an instance of Power
327      */
328     public static Power multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
329     {
330         Throw.whenNull(scalar1, "scalar1 cannot be null");
331         Throw.whenNull(scalar2, "scalar2 cannot be null");
332         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
333                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(PowerUnit.BASE.getSiDimensions()),
334                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Power",
335                 scalar1.toDisplayString(), scalar2.toDisplayString());
336         return new Power(scalar1.si * scalar2.si, PowerUnit.SI);
337     }
338 
339     /**
340      * Divide two scalars that result in a scalar of type Power.
341      * @param scalar1 the first scalar
342      * @param scalar2 the second scalar
343      * @return the division of scalar1 by scalar2 as an instance of Power
344      */
345     public static Power divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
346     {
347         Throw.whenNull(scalar1, "scalar1 cannot be null");
348         Throw.whenNull(scalar2, "scalar2 cannot be null");
349         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
350                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(PowerUnit.BASE.getSiDimensions()),
351                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Power",
352                 scalar1.toDisplayString(), scalar2.toDisplayString());
353         return new Power(scalar1.si / scalar2.si, PowerUnit.SI);
354     }
355 
356 }