View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.Locale;
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.MomentumUnit;
11  import org.djunits.unit.PowerUnit;
12  import org.djunits.unit.PressureUnit;
13  import org.djunits.unit.SpeedUnit;
14  import org.djunits.unit.VolumeUnit;
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 FloatEnergy 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 FloatEnergy extends FloatScalarRel<EnergyUnit, FloatEnergy>
32  {
33      /** */
34      private static final long serialVersionUID = 20150901L;
35  
36      /** Constant with value zero. */
37      public static final FloatEnergy ZERO = new FloatEnergy(0.0f, EnergyUnit.SI);
38  
39      /** Constant with value one. */
40      public static final FloatEnergy ONE = new FloatEnergy(1.0f, EnergyUnit.SI);
41  
42      /** Constant with value NaN. */
43      @SuppressWarnings("checkstyle:constantname")
44      public static final FloatEnergy NaN = new FloatEnergy(Float.NaN, EnergyUnit.SI);
45  
46      /** Constant with value POSITIVE_INFINITY. */
47      public static final FloatEnergy POSITIVE_INFINITY = new FloatEnergy(Float.POSITIVE_INFINITY, EnergyUnit.SI);
48  
49      /** Constant with value NEGATIVE_INFINITY. */
50      public static final FloatEnergy NEGATIVE_INFINITY = new FloatEnergy(Float.NEGATIVE_INFINITY, EnergyUnit.SI);
51  
52      /** Constant with value MAX_VALUE. */
53      public static final FloatEnergy POS_MAXVALUE = new FloatEnergy(Float.MAX_VALUE, EnergyUnit.SI);
54  
55      /** Constant with value -MAX_VALUE. */
56      public static final FloatEnergy NEG_MAXVALUE = new FloatEnergy(-Float.MAX_VALUE, EnergyUnit.SI);
57  
58      /**
59       * Construct FloatEnergy 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 FloatEnergy(final float value, final EnergyUnit unit)
64      {
65          super(value, unit);
66      }
67  
68      /**
69       * Construct FloatEnergy scalar.
70       * @param value Scalar from which to construct this instance
71       */
72      public FloatEnergy(final FloatEnergy value)
73      {
74          super(value);
75      }
76  
77      /**
78       * Construct FloatEnergy 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 FloatEnergy(final double value, final EnergyUnit unit)
83      {
84          super((float) value, unit);
85      }
86  
87      @Override
88      public final FloatEnergy instantiateRel(final float value, final EnergyUnit unit)
89      {
90          return new FloatEnergy(value, unit);
91      }
92  
93      /**
94       * Construct FloatEnergy 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 FloatEnergy ofSI(final float value)
99      {
100         return new FloatEnergy(value, EnergyUnit.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 FloatEnergy at the given ratio between 0 and 1
109      */
110     public static FloatEnergy interpolate(final FloatEnergy zero, final FloatEnergy 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 FloatEnergy(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 FloatEnergy max(final FloatEnergy r1, final FloatEnergy 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 FloatEnergy max(final FloatEnergy r1, final FloatEnergy r2, final FloatEnergy... rn)
137     {
138         FloatEnergy maxr = r1.gt(r2) ? r1 : r2;
139         for (FloatEnergy 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 FloatEnergy min(final FloatEnergy r1, final FloatEnergy 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 FloatEnergy min(final FloatEnergy r1, final FloatEnergy r2, final FloatEnergy... rn)
168     {
169         FloatEnergy minr = r1.lt(r2) ? r1 : r2;
170         for (FloatEnergy r : rn)
171         {
172             if (r.lt(minr))
173             {
174                 minr = r;
175             }
176         }
177         return minr;
178     }
179 
180     /**
181      * Returns a FloatEnergy 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 FloatEnergy
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 FloatEnergy valueOf(final String text)
190     {
191         Throw.whenNull(text, "Error parsing FloatEnergy: text to parse is null");
192         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatEnergy: 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             EnergyUnit unit = EnergyUnit.BASE.getUnitByAbbreviation(unitString);
199             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Energy", unitString);
200             return new FloatEnergy(f, unit);
201         }
202         catch (Exception exception)
203         {
204             throw new IllegalArgumentException(
205                     "Error parsing FloatEnergy from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
206                     exception);
207         }
208     }
209 
210     /**
211      * Returns a FloatEnergy 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 FloatEnergy of(final float value, final String unitString)
219     {
220         Throw.whenNull(unitString, "Error parsing FloatEnergy: unitString is null");
221         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatEnergy: empty unitString");
222         EnergyUnit unit = EnergyUnit.BASE.getUnitByAbbreviation(unitString);
223         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing FloatEnergy with unit %s", unitString);
224         return new FloatEnergy(value, unit);
225     }
226 
227     /**
228      * Calculate the division of FloatEnergy and FloatEnergy, which results in a FloatDimensionless scalar.
229      * @param v scalar
230      * @return scalar as a division of FloatEnergy and FloatEnergy
231      */
232     public final FloatDimensionless divide(final FloatEnergy v)
233     {
234         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
235     }
236 
237     /**
238      * Calculate the division of FloatEnergy and FloatForce, which results in a FloatLength scalar.
239      * @param v scalar
240      * @return scalar as a division of FloatEnergy and FloatForce
241      */
242     public final FloatLength divide(final FloatForce v)
243     {
244         return new FloatLength(this.si / v.si, LengthUnit.SI);
245     }
246 
247     /**
248      * Calculate the division of FloatEnergy and FloatLength, which results in a FloatForce scalar.
249      * @param v scalar
250      * @return scalar as a division of FloatEnergy and FloatLength
251      */
252     public final FloatForce divide(final FloatLength v)
253     {
254         return new FloatForce(this.si / v.si, ForceUnit.SI);
255     }
256 
257     /**
258      * Calculate the multiplication of FloatEnergy and FloatLinearDensity, which results in a FloatForce scalar.
259      * @param v scalar
260      * @return scalar as a multiplication of FloatEnergy and FloatLinearDensity
261      */
262     public final FloatForce times(final FloatLinearDensity v)
263     {
264         return new FloatForce(this.si * v.si, ForceUnit.SI);
265     }
266 
267     /**
268      * Calculate the division of FloatEnergy and FloatDuration, which results in a FloatPower scalar.
269      * @param v scalar
270      * @return scalar as a division of FloatEnergy and FloatDuration
271      */
272     public final FloatPower divide(final FloatDuration v)
273     {
274         return new FloatPower(this.si / v.si, PowerUnit.SI);
275     }
276 
277     /**
278      * Calculate the division of FloatEnergy and FloatPower, which results in a FloatDuration scalar.
279      * @param v scalar
280      * @return scalar as a division of FloatEnergy and FloatPower
281      */
282     public final FloatDuration divide(final FloatPower v)
283     {
284         return new FloatDuration(this.si / v.si, DurationUnit.SI);
285     }
286 
287     /**
288      * Calculate the division of FloatEnergy and FloatVolume, which results in a FloatPressure scalar.
289      * @param v scalar
290      * @return scalar as a division of FloatEnergy and FloatVolume
291      */
292     public final FloatPressure divide(final FloatVolume v)
293     {
294         return new FloatPressure(this.si / v.si, PressureUnit.SI);
295     }
296 
297     /**
298      * Calculate the division of FloatEnergy and FloatPressure, which results in a FloatVolume scalar.
299      * @param v scalar
300      * @return scalar as a division of FloatEnergy and FloatPressure
301      */
302     public final FloatVolume divide(final FloatPressure v)
303     {
304         return new FloatVolume(this.si / v.si, VolumeUnit.SI);
305     }
306 
307     /**
308      * Calculate the multiplication of FloatEnergy and FloatFrequency, which results in a FloatPower scalar.
309      * @param v scalar
310      * @return scalar as a multiplication of FloatEnergy and FloatFrequency
311      */
312     public final FloatPower times(final FloatFrequency v)
313     {
314         return new FloatPower(this.si * v.si, PowerUnit.SI);
315     }
316 
317     /**
318      * Calculate the division of FloatEnergy and FloatSpeed, which results in a FloatMomentum scalar.
319      * @param v scalar
320      * @return scalar as a division of FloatEnergy and FloatSpeed
321      */
322     public final FloatMomentum divide(final FloatSpeed v)
323     {
324         return new FloatMomentum(this.si / v.si, MomentumUnit.SI);
325     }
326 
327     /**
328      * Calculate the division of FloatEnergy and FloatMomentum, which results in a FloatSpeed scalar.
329      * @param v scalar
330      * @return scalar as a division of FloatEnergy and FloatMomentum
331      */
332     public final FloatSpeed divide(final FloatMomentum v)
333     {
334         return new FloatSpeed(this.si / v.si, SpeedUnit.SI);
335     }
336 
337     @Override
338     public FloatSIScalar reciprocal()
339     {
340         return FloatSIScalar.divide(FloatDimensionless.ONE, this);
341     }
342 
343     /**
344      * Multiply two scalars that result in a scalar of type FloatEnergy.
345      * @param scalar1 the first scalar
346      * @param scalar2 the second scalar
347      * @return the multiplication of both scalars as an instance of FloatEnergy
348      */
349     public static FloatEnergy multiply(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
350     {
351         Throw.whenNull(scalar1, "scalar1 cannot be null");
352         Throw.whenNull(scalar2, "scalar2 cannot be null");
353         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
354                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(EnergyUnit.BASE.getSiDimensions()),
355                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type FloatEnergy",
356                 scalar1.toDisplayString(), scalar2.toDisplayString());
357         return new FloatEnergy(scalar1.si * scalar2.si, EnergyUnit.SI);
358     }
359 
360     /**
361      * Divide two scalars that result in a scalar of type FloatEnergy.
362      * @param scalar1 the first scalar
363      * @param scalar2 the second scalar
364      * @return the division of scalar1 by scalar2 as an instance of FloatEnergy
365      */
366     public static FloatEnergy divide(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
367     {
368         Throw.whenNull(scalar1, "scalar1 cannot be null");
369         Throw.whenNull(scalar2, "scalar2 cannot be null");
370         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
371                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(EnergyUnit.BASE.getSiDimensions()),
372                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type FloatEnergy",
373                 scalar1.toDisplayString(), scalar2.toDisplayString());
374         return new FloatEnergy(scalar1.si / scalar2.si, EnergyUnit.SI);
375     }
376 
377 }