View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.AbstractUnit;
5   import org.djunits.unit.UnitRuntimeException;
6   import org.djunits.unit.Unitless;
7   import org.djunits.unit.Units;
8   import org.djunits.unit.scale.LinearScale;
9   import org.djunits.unit.scale.Scale;
10  import org.djunits.unit.si.SIUnit;
11  import org.djunits.unit.system.UnitSystem;
12  
13  /**
14   * Energy is a physical quantity representing the capacity to do work, measured in joules (J).
15   * <p>
16   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
18   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
19   * @author Alexander Verbraeck
20   */
21  public class Energy extends Quantity<Energy>
22  {
23      /** Constant with value zero. */
24      public static final Energy ZERO = ofSi(0.0);
25  
26      /** Constant with value one. */
27      public static final Energy ONE = ofSi(1.0);
28  
29      /** Constant with value NaN. */
30      @SuppressWarnings("checkstyle:constantname")
31      public static final Energy NaN = ofSi(Double.NaN);
32  
33      /** Constant with value POSITIVE_INFINITY. */
34      public static final Energy POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
35  
36      /** Constant with value NEGATIVE_INFINITY. */
37      public static final Energy NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
38  
39      /** Constant with value MAX_VALUE. */
40      public static final Energy POS_MAXVALUE = ofSi(Double.MAX_VALUE);
41  
42      /** Constant with value -MAX_VALUE. */
43      public static final Energy NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
44  
45      /** */
46      private static final long serialVersionUID = 600L;
47  
48      /**
49       * Instantiate a Energy quantity with a unit.
50       * @param valueInUnit the value, expressed in the unit
51       * @param unit the unit in which the value is expressed
52       */
53      public Energy(final double valueInUnit, final Energy.Unit unit)
54      {
55          super(valueInUnit, unit);
56      }
57  
58      /**
59       * Return a Energy instance based on an SI value.
60       * @param si the si value
61       * @return the Energy instance based on an SI value
62       */
63      public static Energy ofSi(final double si)
64      {
65          return new Energy(si, Energy.Unit.SI);
66      }
67  
68      @Override
69      public Energy instantiateSi(final double si)
70      {
71          return ofSi(si);
72      }
73  
74      @Override
75      public SIUnit siUnit()
76      {
77          return Energy.Unit.SI_UNIT;
78      }
79  
80      /**
81       * Returns a Energy representation of a textual representation of a value with a unit. The String representation that can be
82       * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
83       * but not required, between the value and the unit.
84       * @param text the textual representation to parse into a Energy
85       * @return the Scalar representation of the value in its unit
86       * @throws IllegalArgumentException when the text cannot be parsed
87       * @throws NullPointerException when the text argument is null
88       */
89      public static Energy valueOf(final String text)
90      {
91          return Quantity.valueOf(text, ZERO);
92      }
93  
94      /**
95       * Returns a Energy based on a value and the textual representation of the unit, which can be localized.
96       * @param valueInUnit the value, expressed in the unit as given by unitString
97       * @param unitString the textual representation of the unit
98       * @return the Scalar representation of the value in its unit
99       * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
100      * @throws NullPointerException when the unitString argument is null
101      */
102     public static Energy of(final double valueInUnit, final String unitString)
103     {
104         return Quantity.of(valueInUnit, unitString, ZERO);
105     }
106 
107     @Override
108     public Energy.Unit getDisplayUnit()
109     {
110         return (Energy.Unit) super.getDisplayUnit();
111     }
112 
113     /**
114      * Calculate the division of Energy and Energy, which results in a Dimensionless quantity.
115      * @param v quantity
116      * @return quantity as a division of Energy and Energy
117      */
118     public final Dimensionless divide(final Energy v)
119     {
120         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
121     }
122 
123     /**
124      * Calculate the division of Energy and Force, which results in a Length scalar.
125      * @param v scalar
126      * @return scalar as a division of Energy and Force
127      */
128     public final Length divide(final Force v)
129     {
130         return new Length(this.si() / v.si(), Length.Unit.SI);
131     }
132 
133     /**
134      * Calculate the division of Energy and Length, which results in a Force scalar.
135      * @param v scalar
136      * @return scalar as a division of Energy and Length
137      */
138     public final Force divide(final Length v)
139     {
140         return new Force(this.si() / v.si(), Force.Unit.SI);
141     }
142 
143     /**
144      * Calculate the multiplication of Energy and LinearDensity, which results in a Force scalar.
145      * @param v scalar
146      * @return scalar as a multiplication of Energy and LinearDensity
147      */
148     public final Force multiply(final LinearObjectDensity v)
149     {
150         return new Force(this.si() * v.si(), Force.Unit.SI);
151     }
152 
153     /**
154      * Calculate the division of Energy and Duration, which results in a Power scalar.
155      * @param v scalar
156      * @return scalar as a division of Energy and Duration
157      */
158     public final Power divide(final Duration v)
159     {
160         return new Power(this.si() / v.si(), Power.Unit.SI);
161     }
162 
163     /**
164      * Calculate the division of Energy and Power, which results in a Duration scalar.
165      * @param v scalar
166      * @return scalar as a division of Energy and Power
167      */
168     public final Duration divide(final Power v)
169     {
170         return new Duration(this.si() / v.si(), Duration.Unit.SI);
171     }
172 
173     /**
174      * Calculate the division of Energy and Volume, which results in a Pressure scalar.
175      * @param v scalar
176      * @return scalar as a division of Energy and Volume
177      */
178     public final Pressure divide(final Volume v)
179     {
180         return new Pressure(this.si() / v.si(), Pressure.Unit.SI);
181     }
182 
183     /**
184      * Calculate the division of Energy and Pressure, which results in a Volume scalar.
185      * @param v scalar
186      * @return scalar as a division of Energy and Pressure
187      */
188     public final Volume divide(final Pressure v)
189     {
190         return new Volume(this.si() / v.si(), Volume.Unit.SI);
191     }
192 
193     /**
194      * Calculate the multiplication of Energy and Frequency, which results in a Power scalar.
195      * @param v scalar
196      * @return scalar as a multiplication of Energy and Frequency
197      */
198     public final Power multiply(final Frequency v)
199     {
200         return new Power(this.si() * v.si(), Power.Unit.SI);
201     }
202 
203     /**
204      * Calculate the division of Energy and Speed, which results in a Momentum scalar.
205      * @param v scalar
206      * @return scalar as a division of Energy and Speed
207      */
208     public final Momentum divide(final Speed v)
209     {
210         return new Momentum(this.si() / v.si(), Momentum.Unit.SI);
211     }
212 
213     /**
214      * Calculate the division of Energy and Momentum, which results in a Speed scalar.
215      * @param v scalar
216      * @return scalar as a division of Energy and Momentum
217      */
218     public final Speed divide(final Momentum v)
219     {
220         return new Speed(this.si() / v.si(), Speed.Unit.SI);
221     }
222 
223     /******************************************************************************************************/
224     /********************************************** UNIT CLASS ********************************************/
225     /******************************************************************************************************/
226 
227     /**
228      * Energy.Unit encodes the units of energy.
229      * <p>
230      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
231      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
232      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
233      * @author Alexander Verbraeck
234      */
235     @SuppressWarnings("checkstyle:constantname")
236     public static class Unit extends AbstractUnit<Energy.Unit, Energy>
237     {
238         /** The dimensions of energy: kgm2/s2. */
239         public static final SIUnit SI_UNIT = SIUnit.of("kgm2/s2");
240 
241         /** Joule. */
242         public static final Energy.Unit J = new Energy.Unit("J", "joule", 1.0, UnitSystem.SI_DERIVED);
243 
244         /** The SI or BASE unit. */
245         public static final Energy.Unit SI = J.generateSiPrefixes(false, false);
246 
247         /** microjoule. */
248         public static final Energy.Unit muJ = Units.resolve(Energy.Unit.class, "muJ");
249 
250         /** millijoule. */
251         public static final Energy.Unit mJ = Units.resolve(Energy.Unit.class, "mJ");
252 
253         /** kilojoule. */
254         public static final Energy.Unit kJ = Units.resolve(Energy.Unit.class, "kJ");
255 
256         /** megajoule. */
257         public static final Energy.Unit MJ = Units.resolve(Energy.Unit.class, "MJ");
258 
259         /** gigajoule. */
260         public static final Energy.Unit GJ = Units.resolve(Energy.Unit.class, "GJ");
261 
262         /** terajoule. */
263         public static final Energy.Unit TJ = Units.resolve(Energy.Unit.class, "TJ");
264 
265         /** petajoule. */
266         public static final Energy.Unit PJ = Units.resolve(Energy.Unit.class, "PJ");
267 
268         /** foot-pound force. */
269         public static final Energy.Unit ft_lbf = J.deriveUnit("ft.lbf", "foot pound-force",
270                 Length.Unit.CONST_FT * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
271 
272         /** inch-pound force. */
273         public static final Energy.Unit in_lbf = J.deriveUnit("in.lbf", "inch pound-force",
274                 Length.Unit.CONST_IN * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
275 
276         /** British thermal unit (ISO). */
277         public static final Energy.Unit BTU_ISO =
278                 J.deriveUnit("BTU(ISO)", "British thermal unit (ISO)", 1.0545E3, UnitSystem.IMPERIAL);
279 
280         /** British thermal unit (International Table). */
281         public static final Energy.Unit BTU_IT =
282                 J.deriveUnit("BTU(IT)", "British thermal unit (Int. Table)", 1.05505585262E3, UnitSystem.IMPERIAL);
283 
284         /** calorie (International Table). */
285         public static final Energy.Unit cal_IT = J.deriveUnit("cal(IT)", "calorie (Int. Table)", 4.1868, UnitSystem.IMPERIAL);
286 
287         /** calorie. */
288         public static final Energy.Unit cal = J.deriveUnit("cal", "calorie", 4.184, UnitSystem.OTHER);
289 
290         /** kilocalorie. */
291         public static final Energy.Unit kcal = cal.deriveUnit("kcal", "kilocalorie", 1000.0, UnitSystem.OTHER);
292 
293         /** watt hour. */
294         public static final Energy.Unit Wh = new Energy.Unit("Wh", "watt hour", 3600.0, UnitSystem.SI_DERIVED);
295 
296         /** microwatt hour. */
297         public static final Energy.Unit muWh = Wh.deriveUnit("muWh", "\u03BCWh", "microwatt hour", 1E-6, UnitSystem.SI_DERIVED);
298 
299         /** milliwatt hour. */
300         public static final Energy.Unit mWh = Wh.deriveUnit("mWh", "milliwatt hour", 1E-3, UnitSystem.SI_DERIVED);
301 
302         /** kilowatt hour. */
303         public static final Energy.Unit kWh = Wh.deriveUnit("kWh", "kilowatt hour", 1E3, UnitSystem.SI_DERIVED);
304 
305         /** megawatt hour. */
306         public static final Energy.Unit MWh = Wh.deriveUnit("MWh", "megawatt hour", 1E6, UnitSystem.SI_DERIVED);
307 
308         /** gigawatt hour. */
309         public static final Energy.Unit GWh = Wh.deriveUnit("GWh", "gigawatt hour", 1E9, UnitSystem.SI_DERIVED);
310 
311         /** terawatt hour. */
312         public static final Energy.Unit TWh = Wh.deriveUnit("TWh", "terawatt hour", 1E12, UnitSystem.SI_DERIVED);
313 
314         /** petawatt hour. */
315         public static final Energy.Unit PWh = Wh.deriveUnit("PWh", "petawatt hour", 1E15, UnitSystem.SI_DERIVED);
316 
317         /** electronvolt. */
318         public static final Energy.Unit eV = new Energy.Unit("eV", "electronvolt", 1.602176634E-19, UnitSystem.SI_ACCEPTED);
319 
320         /** kilo-electronvolt. */
321         public static final Energy.Unit keV = eV.deriveUnit("keV", "kiloelectronvolt", 1E3, UnitSystem.SI_ACCEPTED);
322 
323         /** mega-electronvolt. */
324         public static final Energy.Unit MeV = eV.deriveUnit("MeV", "megaelectronvolt", 1E6, UnitSystem.SI_ACCEPTED);
325 
326         /** giga-electronvolt. */
327         public static final Energy.Unit GeV = eV.deriveUnit("GeV", "gigaelectronvolt", 1E9, UnitSystem.SI_ACCEPTED);
328 
329         /** sthene-meter (mts). */
330         public static final Energy.Unit sn_m = J.deriveUnit("sn.m", "sthene meter", 1000.0, UnitSystem.MTS);
331 
332         /** erg (cgs). */
333         public static final Energy.Unit erg = J.deriveUnit("erg", "erg", 1.0E-7, UnitSystem.CGS);
334 
335         /**
336          * Create a new Energy unit.
337          * @param id the id or main abbreviation of the unit
338          * @param name the full name of the unit
339          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
340          * @param unitSystem the unit system such as SI or IMPERIAL
341          */
342         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
343         {
344             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
345         }
346 
347         /**
348          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
349          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
350          * @param displayAbbreviation the display abbreviation of the unit
351          * @param name the full name of the unit
352          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
353          * @param unitSystem unit system, e.g. SI or Imperial
354          */
355         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
356                 final UnitSystem unitSystem)
357         {
358             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
359         }
360 
361         @Override
362         public SIUnit siUnit()
363         {
364             return SI_UNIT;
365         }
366 
367         @Override
368         public Unit getBaseUnit()
369         {
370             return SI;
371         }
372 
373         @Override
374         public Energy ofSi(final double si)
375         {
376             return Energy.ofSi(si);
377         }
378 
379         @Override
380         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
381                 final double scaleFactor, final UnitSystem unitSystem)
382         {
383             if (getScale() instanceof LinearScale ls)
384             {
385                 return new Energy.Unit(textualAbbreviation, displayAbbreviation, name,
386                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
387             }
388             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
389         }
390 
391     }
392 }