View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import javax.annotation.Generated;
6   
7   import org.djunits.Throw;
8   import org.djunits.unit.DimensionlessUnit;
9   import org.djunits.unit.DurationUnit;
10  import org.djunits.unit.EnergyUnit;
11  import org.djunits.unit.ForceUnit;
12  import org.djunits.unit.LengthUnit;
13  import org.djunits.unit.MomentumUnit;
14  import org.djunits.unit.PowerUnit;
15  import org.djunits.unit.PressureUnit;
16  import org.djunits.unit.SpeedUnit;
17  import org.djunits.unit.VolumeUnit;
18  import org.djunits.value.util.ValueUtil;
19  import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRel;
20  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
21  
22  /**
23   * Easy access methods for the Energy DoubleScalar, which is relative by definition.
24   * <p>
25   * Copyright (c) 2013-2022 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 = "2022-03-14T11:14:15.180987200Z")
32  public class Energy extends AbstractDoubleScalarRel<EnergyUnit, Energy>
33  {
34      /** */
35      private static final long serialVersionUID = 20150905L;
36  
37      /** Constant with value zero. */
38      public static final Energyle/scalar/Energy.html#Energy">Energy ZERO = new Energy(0.0, EnergyUnit.SI);
39  
40      /** Constant with value one. */
41      public static final Energyble/scalar/Energy.html#Energy">Energy ONE = new Energy(1.0, EnergyUnit.SI);
42  
43      /** Constant with value NaN. */
44      @SuppressWarnings("checkstyle:constantname")
45      public static final Energyble/scalar/Energy.html#Energy">Energy NaN = new Energy(Double.NaN, EnergyUnit.SI);
46  
47      /** Constant with value POSITIVE_INFINITY. */
48      public static final Energyrgy.html#Energy">Energy POSITIVE_INFINITY = new Energy(Double.POSITIVE_INFINITY, EnergyUnit.SI);
49  
50      /** Constant with value NEGATIVE_INFINITY. */
51      public static final Energyrgy.html#Energy">Energy NEGATIVE_INFINITY = new Energy(Double.NEGATIVE_INFINITY, EnergyUnit.SI);
52  
53      /** Constant with value MAX_VALUE. */
54      public static final Energyr/Energy.html#Energy">Energy POS_MAXVALUE = new Energy(Double.MAX_VALUE, EnergyUnit.SI);
55  
56      /** Constant with value -MAX_VALUE. */
57      public static final Energyr/Energy.html#Energy">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 Energyvdouble/scalar/Energy.html#Energy">Energy(final Energy value)
74      {
75          super(value);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final Energy instantiateRel(final double value, final EnergyUnit unit)
81      {
82          return new Energy(value, unit);
83      }
84  
85      /**
86       * Construct Energy scalar.
87       * @param value double; the double value in SI units
88       * @return Energy; the new scalar with the SI value
89       */
90      public static final Energy instantiateSI(final double value)
91      {
92          return new Energy(value, EnergyUnit.SI);
93      }
94  
95      /**
96       * Interpolate between two values.
97       * @param zero Energy; the low value
98       * @param one Energy; the high value
99       * @param ratio double; the ratio between 0 and 1, inclusive
100      * @return Energy; a Scalar at the ratio between
101      */
102     public static Energye/scalar/Energy.html#Energy">Energyar/Energy.html#Energy">Energy interpolate(final Energye/scalar/Energy.html#Energy">Energy zero, final Energy one, final double ratio)
103     {
104         return new Energy(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 Energy; the first scalar
110      * @param r2 Energy; the second scalar
111      * @return Energy; the maximum value of two relative scalars
112      */
113     public static Energyble/scalar/Energy.html#Energy">Energyble/scalar/Energy.html#Energy">Energy max(final Energyble/scalar/Energy.html#Energy">Energy r1, final Energy 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 Energy; the first scalar
121      * @param r2 Energy; the second scalar
122      * @param rn Energy...; the other scalars
123      * @return Energy; the maximum value of more than two relative scalars
124      */
125     public static Energyble/scalar/Energy.html#Energy">Energyble/scalar/Energy.html#Energy">Energy max(final Energyble/scalar/Energy.html#Energy">Energy r1, final Energy r2, final Energy... rn)
126     {
127         Energy maxr = r1.gt(r2) ? r1 : r2;
128         for (Energy 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 Energy; the first scalar
141      * @param r2 Energy; the second scalar
142      * @return Energy; the minimum value of two relative scalars
143      */
144     public static Energyble/scalar/Energy.html#Energy">Energyble/scalar/Energy.html#Energy">Energy min(final Energyble/scalar/Energy.html#Energy">Energy r1, final Energy 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 Energy; the first scalar
152      * @param r2 Energy; the second scalar
153      * @param rn Energy...; the other scalars
154      * @return Energy; the minimum value of more than two relative scalars
155      */
156     public static Energyble/scalar/Energy.html#Energy">Energyble/scalar/Energy.html#Energy">Energy min(final Energyble/scalar/Energy.html#Energy">Energy r1, final Energy r2, final Energy... rn)
157     {
158         Energy minr = r1.lt(r2) ? r1 : r2;
159         for (Energy r : rn)
160         {
161             if (r.lt(minr))
162             {
163                 minr = r;
164             }
165         }
166         return minr;
167     }
168 
169     /**
170      * Returns a Energy 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 the official abbreviation of the unit. Spaces are allowed, but not
172      * required, between the value and the unit.
173      * @param text String; the textual representation to parse into a Energy
174      * @return Energy; 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 Energy valueOf(final String text)
179     {
180         Throw.whenNull(text, "Error parsing Energy: text to parse is null");
181         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Energy: empty text to parse");
182         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
183         if (matcher.find())
184         {
185             int index = matcher.end();
186             String unitString = text.substring(index).trim();
187             String valueString = text.substring(0, index).trim();
188             EnergyUnit unit = EnergyUnit.BASE.getUnitByAbbreviation(unitString);
189             if (unit != null)
190             {
191                 double d = Double.parseDouble(valueString);
192                 return new Energy(d, unit);
193             }
194         }
195         throw new IllegalArgumentException("Error parsing Energy from " + text);
196     }
197 
198     /**
199      * Returns a Energy based on a value and the textual representation of the unit.
200      * @param value double; the value to use
201      * @param unitString String; the textual representation of the unit
202      * @return Energy; the Scalar representation of the value in its unit
203      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
204      * @throws NullPointerException when the unitString argument is null
205      */
206     public static Energy of(final double value, final String unitString)
207     {
208         Throw.whenNull(unitString, "Error parsing Energy: unitString is null");
209         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Energy: empty unitString");
210         EnergyUnit unit = EnergyUnit.BASE.getUnitByAbbreviation(unitString);
211         if (unit != null)
212         {
213             return new Energy(value, unit);
214         }
215         throw new IllegalArgumentException("Error parsing Energy with unit " + unitString);
216     }
217 
218     /**
219      * Calculate the division of Energy and Energy, which results in a Dimensionless scalar.
220      * @param v Energy; scalar
221      * @return Dimensionless; scalar as a division of Energy and Energy
222      */
223     public final Dimensionless divide(final Energy v)
224     {
225         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
226     }
227 
228     /**
229      * Calculate the division of Energy and Force, which results in a Length scalar.
230      * @param v Energy; scalar
231      * @return Length; scalar as a division of Energy and Force
232      */
233     public final Length divide(final Force v)
234     {
235         return new Length(this.si / v.si, LengthUnit.SI);
236     }
237 
238     /**
239      * Calculate the division of Energy and Length, which results in a Force scalar.
240      * @param v Energy; scalar
241      * @return Force; scalar as a division of Energy and Length
242      */
243     public final Force divide(final Length v)
244     {
245         return new Force(this.si / v.si, ForceUnit.SI);
246     }
247 
248     /**
249      * Calculate the multiplication of Energy and LinearDensity, which results in a Force scalar.
250      * @param v Energy; scalar
251      * @return Force; scalar as a multiplication of Energy and LinearDensity
252      */
253     public final Force times(final LinearDensity v)
254     {
255         return new Force(this.si * v.si, ForceUnit.SI);
256     }
257 
258     /**
259      * Calculate the division of Energy and Duration, which results in a Power scalar.
260      * @param v Energy; scalar
261      * @return Power; scalar as a division of Energy and Duration
262      */
263     public final Power divide(final Duration v)
264     {
265         return new Power(this.si / v.si, PowerUnit.SI);
266     }
267 
268     /**
269      * Calculate the division of Energy and Power, which results in a Duration scalar.
270      * @param v Energy; scalar
271      * @return Duration; scalar as a division of Energy and Power
272      */
273     public final Duration divide(final Power v)
274     {
275         return new Duration(this.si / v.si, DurationUnit.SI);
276     }
277 
278     /**
279      * Calculate the division of Energy and Volume, which results in a Pressure scalar.
280      * @param v Energy; scalar
281      * @return Pressure; scalar as a division of Energy and Volume
282      */
283     public final Pressure divide(final Volume v)
284     {
285         return new Pressure(this.si / v.si, PressureUnit.SI);
286     }
287 
288     /**
289      * Calculate the division of Energy and Pressure, which results in a Volume scalar.
290      * @param v Energy; scalar
291      * @return Volume; scalar as a division of Energy and Pressure
292      */
293     public final Volume divide(final Pressure v)
294     {
295         return new Volume(this.si / v.si, VolumeUnit.SI);
296     }
297 
298     /**
299      * Calculate the multiplication of Energy and Frequency, which results in a Power scalar.
300      * @param v Energy; scalar
301      * @return Power; scalar as a multiplication of Energy and Frequency
302      */
303     public final Power times(final Frequency v)
304     {
305         return new Power(this.si * v.si, PowerUnit.SI);
306     }
307 
308     /**
309      * Calculate the division of Energy and Speed, which results in a Momentum scalar.
310      * @param v Energy; scalar
311      * @return Momentum; scalar as a division of Energy and Speed
312      */
313     public final Momentum divide(final Speed v)
314     {
315         return new Momentum(this.si / v.si, MomentumUnit.SI);
316     }
317 
318     /**
319      * Calculate the division of Energy and Momentum, which results in a Speed scalar.
320      * @param v Energy; scalar
321      * @return Speed; scalar as a division of Energy and Momentum
322      */
323     public final Speed divide(final Momentum v)
324     {
325         return new Speed(this.si / v.si, SpeedUnit.SI);
326     }
327 
328     /** {@inheritDoc} */
329     @Override
330     public SIScalar reciprocal()
331     {
332         return DoubleScalar.divide(Dimensionless.ONE, this);
333     }
334 
335 }