View Javadoc
1   package org.djunits.value.vfloat.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.vfloat.scalar.base.FloatScalarRel;
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 FloatPower FloatScalar, 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 FloatPower extends FloatScalarRel<PowerUnit, FloatPower>
32  {
33      /** */
34      private static final long serialVersionUID = 20150901L;
35  
36      /** Constant with value zero. */
37      public static final FloatPower ZERO = new FloatPower(0.0f, PowerUnit.SI);
38  
39      /** Constant with value one. */
40      public static final FloatPower ONE = new FloatPower(1.0f, PowerUnit.SI);
41  
42      /** Constant with value NaN. */
43      @SuppressWarnings("checkstyle:constantname")
44      public static final FloatPower NaN = new FloatPower(Float.NaN, PowerUnit.SI);
45  
46      /** Constant with value POSITIVE_INFINITY. */
47      public static final FloatPower POSITIVE_INFINITY = new FloatPower(Float.POSITIVE_INFINITY, PowerUnit.SI);
48  
49      /** Constant with value NEGATIVE_INFINITY. */
50      public static final FloatPower NEGATIVE_INFINITY = new FloatPower(Float.NEGATIVE_INFINITY, PowerUnit.SI);
51  
52      /** Constant with value MAX_VALUE. */
53      public static final FloatPower POS_MAXVALUE = new FloatPower(Float.MAX_VALUE, PowerUnit.SI);
54  
55      /** Constant with value -MAX_VALUE. */
56      public static final FloatPower NEG_MAXVALUE = new FloatPower(-Float.MAX_VALUE, PowerUnit.SI);
57  
58      /**
59       * Construct FloatPower scalar with a unit.
60       * @param value the float value, expressed in the given unit
61       * @param unit unit for the float value
62       */
63      public FloatPower(final float value, final PowerUnit unit)
64      {
65          super(value, unit);
66      }
67  
68      /**
69       * Construct FloatPower scalar.
70       * @param value Scalar from which to construct this instance
71       */
72      public FloatPower(final FloatPower value)
73      {
74          super(value);
75      }
76  
77      /**
78       * Construct FloatPower scalar with a unit using a double value.
79       * @param value the double value, expressed in the given unit
80       * @param unit unit for the resulting float value
81       */
82      public FloatPower(final double value, final PowerUnit unit)
83      {
84          super((float) value, unit);
85      }
86  
87      @Override
88      public final FloatPower instantiateRel(final float value, final PowerUnit unit)
89      {
90          return new FloatPower(value, unit);
91      }
92  
93      /**
94       * Construct FloatPower scalar based on an SI value.
95       * @param value the float value in SI units
96       * @return the new scalar with the SI value
97       */
98      public static final FloatPower ofSI(final float value)
99      {
100         return new FloatPower(value, PowerUnit.SI);
101     }
102 
103     /**
104      * Interpolate between two values. Note that the first value does not have to be smaller than the second.
105      * @param zero the value at a ratio of zero
106      * @param one the value at a ratio of one
107      * @param ratio the ratio between 0 and 1, inclusive
108      * @return a FloatPower at the given ratio between 0 and 1
109      */
110     public static FloatPower interpolate(final FloatPower zero, final FloatPower one, final float ratio)
111     {
112         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
113                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
114         return new FloatPower(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
115                 zero.getDisplayUnit());
116     }
117 
118     /**
119      * Return the maximum value of two relative scalars.
120      * @param r1 the first scalar
121      * @param r2 the second scalar
122      * @return the maximum value of two relative scalars
123      */
124     public static FloatPower max(final FloatPower r1, final FloatPower r2)
125     {
126         return r1.gt(r2) ? r1 : r2;
127     }
128 
129     /**
130      * Return the maximum value of more than two relative scalars.
131      * @param r1 the first scalar
132      * @param r2 the second scalar
133      * @param rn the other scalars
134      * @return the maximum value of more than two relative scalars
135      */
136     public static FloatPower max(final FloatPower r1, final FloatPower r2, final FloatPower... rn)
137     {
138         FloatPower maxr = r1.gt(r2) ? r1 : r2;
139         for (FloatPower r : rn)
140         {
141             if (r.gt(maxr))
142             {
143                 maxr = r;
144             }
145         }
146         return maxr;
147     }
148 
149     /**
150      * Return the minimum value of two relative scalars.
151      * @param r1 the first scalar
152      * @param r2 the second scalar
153      * @return the minimum value of two relative scalars
154      */
155     public static FloatPower min(final FloatPower r1, final FloatPower r2)
156     {
157         return r1.lt(r2) ? r1 : r2;
158     }
159 
160     /**
161      * Return the minimum value of more than two relative scalars.
162      * @param r1 the first scalar
163      * @param r2 the second scalar
164      * @param rn the other scalars
165      * @return the minimum value of more than two relative scalars
166      */
167     public static FloatPower min(final FloatPower r1, final FloatPower r2, final FloatPower... rn)
168     {
169         FloatPower minr = r1.lt(r2) ? r1 : r2;
170         for (FloatPower r : rn)
171         {
172             if (r.lt(minr))
173             {
174                 minr = r;
175             }
176         }
177         return minr;
178     }
179 
180     /**
181      * Returns a FloatPower representation of a textual representation of a value with a unit. The String representation that
182      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
183      * allowed, but not required, between the value and the unit.
184      * @param text the textual representation to parse into a FloatPower
185      * @return the Scalar representation of the value in its unit
186      * @throws IllegalArgumentException when the text cannot be parsed
187      * @throws NullPointerException when the text argument is null
188      */
189     public static FloatPower valueOf(final String text)
190     {
191         Throw.whenNull(text, "Error parsing FloatPower: text to parse is null");
192         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatPower: empty text to parse");
193         try
194         {
195             NumberParser numberParser = new NumberParser().lenient().trailing();
196             float f = numberParser.parseFloat(text);
197             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
198             PowerUnit unit = PowerUnit.BASE.getUnitByAbbreviation(unitString);
199             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Power", unitString);
200             return new FloatPower(f, unit);
201         }
202         catch (Exception exception)
203         {
204             throw new IllegalArgumentException(
205                     "Error parsing FloatPower from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
206                     exception);
207         }
208     }
209 
210     /**
211      * Returns a FloatPower based on a value and the textual representation of the unit, which can be localized.
212      * @param value the value to use
213      * @param unitString the textual representation of the unit
214      * @return the Scalar representation of the value in its unit
215      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
216      * @throws NullPointerException when the unitString argument is null
217      */
218     public static FloatPower of(final float value, final String unitString)
219     {
220         Throw.whenNull(unitString, "Error parsing FloatPower: unitString is null");
221         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatPower: empty unitString");
222         PowerUnit unit = PowerUnit.BASE.getUnitByAbbreviation(unitString);
223         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing FloatPower with unit %s", unitString);
224         return new FloatPower(value, unit);
225     }
226 
227     /**
228      * Calculate the division of FloatPower and FloatPower, which results in a FloatDimensionless scalar.
229      * @param v scalar
230      * @return scalar as a division of FloatPower and FloatPower
231      */
232     public final FloatDimensionless divide(final FloatPower v)
233     {
234         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
235     }
236 
237     /**
238      * Calculate the multiplication of FloatPower and FloatDuration, which results in a FloatEnergy scalar.
239      * @param v scalar
240      * @return scalar as a multiplication of FloatPower and FloatDuration
241      */
242     public final FloatEnergy times(final FloatDuration v)
243     {
244         return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
245     }
246 
247     /**
248      * Calculate the division of FloatPower and FloatFrequency, which results in a FloatEnergy scalar.
249      * @param v scalar
250      * @return scalar as a division of FloatPower and FloatFrequency
251      */
252     public final FloatEnergy divide(final FloatFrequency v)
253     {
254         return new FloatEnergy(this.si / v.si, EnergyUnit.SI);
255     }
256 
257     /**
258      * Calculate the division of FloatPower and FloatEnergy, which results in a FloatFrequency scalar.
259      * @param v scalar
260      * @return scalar as a division of FloatPower and FloatEnergy
261      */
262     public final FloatFrequency divide(final FloatEnergy v)
263     {
264         return new FloatFrequency(this.si / v.si, FrequencyUnit.SI);
265     }
266 
267     /**
268      * Calculate the division of FloatPower and FloatSpeed, which results in a FloatForce scalar.
269      * @param v scalar
270      * @return scalar as a division of FloatPower and FloatSpeed
271      */
272     public final FloatForce divide(final FloatSpeed v)
273     {
274         return new FloatForce(this.si / v.si, ForceUnit.SI);
275     }
276 
277     /**
278      * Calculate the division of FloatPower and FloatForce, which results in a FloatSpeed scalar.
279      * @param v scalar
280      * @return scalar as a division of FloatPower and FloatForce
281      */
282     public final FloatSpeed divide(final FloatForce v)
283     {
284         return new FloatSpeed(this.si / v.si, SpeedUnit.SI);
285     }
286 
287     /**
288      * Calculate the division of FloatPower and FloatElectricalPotential, which results in a FloatElectricalCurrent scalar.
289      * @param v scalar
290      * @return scalar as a division of FloatPower and FloatElectricalPotential
291      */
292     public final FloatElectricalCurrent divide(final FloatElectricalPotential v)
293     {
294         return new FloatElectricalCurrent(this.si / v.si, ElectricalCurrentUnit.SI);
295     }
296 
297     /**
298      * Calculate the division of FloatPower and FloatElectricalCurrent, which results in a FloatElectricalPotential scalar.
299      * @param v scalar
300      * @return scalar as a division of FloatPower and FloatElectricalCurrent
301      */
302     public final FloatElectricalPotential divide(final FloatElectricalCurrent v)
303     {
304         return new FloatElectricalPotential(this.si / v.si, ElectricalPotentialUnit.SI);
305     }
306 
307     /**
308      * Calculate the division of FloatPower and FloatAcceleration, which results in a FloatMomentum scalar.
309      * @param v scalar
310      * @return scalar as a division of FloatPower and FloatAcceleration
311      */
312     public final FloatMomentum divide(final FloatAcceleration v)
313     {
314         return new FloatMomentum(this.si / v.si, MomentumUnit.SI);
315     }
316 
317     /**
318      * Calculate the division of FloatPower and FloatMomentum, which results in a FloatAcceleration scalar.
319      * @param v scalar
320      * @return scalar as a division of FloatPower and FloatMomentum
321      */
322     public final FloatAcceleration divide(final FloatMomentum v)
323     {
324         return new FloatAcceleration(this.si / v.si, AccelerationUnit.SI);
325     }
326 
327     @Override
328     public FloatSIScalar reciprocal()
329     {
330         return FloatSIScalar.divide(FloatDimensionless.ONE, this);
331     }
332 
333     /**
334      * Multiply two scalars that result in a scalar of type FloatPower.
335      * @param scalar1 the first scalar
336      * @param scalar2 the second scalar
337      * @return the multiplication of both scalars as an instance of FloatPower
338      */
339     public static FloatPower multiply(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
340     {
341         Throw.whenNull(scalar1, "scalar1 cannot be null");
342         Throw.whenNull(scalar2, "scalar2 cannot be null");
343         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
344                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(PowerUnit.BASE.getSiDimensions()),
345                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type FloatPower",
346                 scalar1.toDisplayString(), scalar2.toDisplayString());
347         return new FloatPower(scalar1.si * scalar2.si, PowerUnit.SI);
348     }
349 
350     /**
351      * Divide two scalars that result in a scalar of type FloatPower.
352      * @param scalar1 the first scalar
353      * @param scalar2 the second scalar
354      * @return the division of scalar1 by scalar2 as an instance of FloatPower
355      */
356     public static FloatPower divide(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
357     {
358         Throw.whenNull(scalar1, "scalar1 cannot be null");
359         Throw.whenNull(scalar2, "scalar2 cannot be null");
360         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
361                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(PowerUnit.BASE.getSiDimensions()),
362                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type FloatPower",
363                 scalar1.toDisplayString(), scalar2.toDisplayString());
364         return new FloatPower(scalar1.si / scalar2.si, PowerUnit.SI);
365     }
366 
367 }