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