View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.CGS;
4   import static org.djunits.unit.unitsystem.UnitSystem.IMPERIAL;
5   import static org.djunits.unit.unitsystem.UnitSystem.MTS;
6   import static org.djunits.unit.unitsystem.UnitSystem.OTHER;
7   import static org.djunits.unit.unitsystem.UnitSystem.SI_ACCEPTED;
8   import static org.djunits.unit.unitsystem.UnitSystem.SI_BASE;
9   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
10  
11  import org.djunits.unit.unitsystem.UnitSystem;
12  
13  /**
14   * The units of energy.
15   * <p>
16   * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
18   * <p>
19   * $LastChangedDate: 2019-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 324 $, by $Author: averbraeck $,
20   * initial version May 15, 2014 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   */
23  public class EnergyUnit extends LinearUnit<EnergyUnit>
24  {
25      /** */
26      private static final long serialVersionUID = 20140604L;
27  
28      /** the unit of mass for the energy unit, e.g., kilogram. */
29      private final MassUnit massUnit;
30  
31      /** the unit of length for the energy unit, e.g., length. */
32      private final LengthUnit lengthUnit;
33  
34      /** the unit of time for the energy unit, e.g., second. */
35      private final DurationUnit durationUnit;
36  
37      /** The SI unit for energy is Joule. */
38      public static final EnergyUnit SI;
39  
40      /** picojoule. */
41      public static final EnergyUnit PICOJOULE;
42  
43      /** nanojoule. */
44      public static final EnergyUnit NANOJOULE;
45  
46      /** microjoule. */
47      public static final EnergyUnit MICROJOULE;
48  
49      /** millijoule. */
50      public static final EnergyUnit MILLIJOULE;
51  
52      /** Joule. */
53      public static final EnergyUnit JOULE;
54  
55      /** kilojoule. */
56      public static final EnergyUnit KILOJOULE;
57  
58      /** megajoule. */
59      public static final EnergyUnit MEGAJOULE;
60  
61      /** gigajoule. */
62      public static final EnergyUnit GIGAJOULE;
63  
64      /** terajoule. */
65      public static final EnergyUnit TERAJOULE;
66  
67      /** petajoule. */
68      public static final EnergyUnit PETAJOULE;
69  
70      /** foot-pound force. */
71      public static final EnergyUnit FOOT_POUND_FORCE;
72  
73      /** inch-pound force. */
74      public static final EnergyUnit INCH_POUND_FORCE;
75  
76      /** British thermal unit (ISO). */
77      public static final EnergyUnit BTU_ISO;
78  
79      /** British thermal unit (International Table). */
80      public static final EnergyUnit BTU_IT;
81  
82      /** calorie (International Table). */
83      public static final EnergyUnit CALORIE_IT;
84  
85      /** calorie. */
86      public static final EnergyUnit CALORIE;
87  
88      /** kilocalorie. */
89      public static final EnergyUnit KILOCALORIE;
90  
91      /** femtowatt-hour. */
92      public static final EnergyUnit FEMTOWATT_HOUR;
93  
94      /** picowatt-hour. */
95      public static final EnergyUnit PICOWATT_HOUR;
96  
97      /** nanowatt-hour. */
98      public static final EnergyUnit NANOWATT_HOUR;
99  
100     /** microwatt-hour. */
101     public static final EnergyUnit MICROWATT_HOUR;
102 
103     /** milliwatt-hour. */
104     public static final EnergyUnit MILLIWATT_HOUR;
105 
106     /** watt-hour. */
107     public static final EnergyUnit WATT_HOUR;
108 
109     /** kilowatt-hour. */
110     public static final EnergyUnit KILOWATT_HOUR;
111 
112     /** megawatt-hour. */
113     public static final EnergyUnit MEGAWATT_HOUR;
114 
115     /** gigawatt-hour. */
116     public static final EnergyUnit GIGAWATT_HOUR;
117 
118     /** terawatt-hour. */
119     public static final EnergyUnit TERAWATT_HOUR;
120 
121     /** petawatt-hour. */
122     public static final EnergyUnit PETAWATT_HOUR;
123 
124     /** electronvolt. */
125     public static final EnergyUnit ELECTRONVOLT;
126 
127     /** micro-electronvolt. */
128     public static final EnergyUnit MICROELECTRONVOLT;
129 
130     /** milli-electronvolt. */
131     public static final EnergyUnit MILLIELECTRONVOLT;
132 
133     /** kilo-electronvolt. */
134     public static final EnergyUnit KILOELECTRONVOLT;
135 
136     /** mega-electronvolt. */
137     public static final EnergyUnit MEGAELECTRONVOLT;
138 
139     /** giga-electronvolt. */
140     public static final EnergyUnit GIGAELECTRONVOLT;
141 
142     /** tera-electronvolt. */
143     public static final EnergyUnit TERAELECTRONVOLT;
144 
145     /** peta-electronvolt. */
146     public static final EnergyUnit PETAELECTRONVOLT;
147 
148     /** exa-electronvolt. */
149     public static final EnergyUnit EXAELECTRONVOLT;
150 
151     /** sthene-meter (mts). */
152     public static final EnergyUnit STHENE_METER;
153 
154     /** erg (cgs). */
155     public static final EnergyUnit ERG;
156 
157     static
158     {
159         SI = new EnergyUnit(MassUnit.KILOGRAM, LengthUnit.METER, DurationUnit.SECOND, "EnergyUnit.Joule", "EnergyUnit.J",
160                 SI_DERIVED, true);
161         JOULE = SI;
162         PICOJOULE = new EnergyUnit("EnergyUnit.picojoule", "EnergyUnit.pJ", SI_BASE, JOULE, 1E-12, true);
163         NANOJOULE = new EnergyUnit("EnergyUnit.nanojoule", "EnergyUnit.nJ", SI_BASE, JOULE, 1E-9, true);
164         MICROJOULE = new EnergyUnit("EnergyUnit.microjoule", "EnergyUnit.muJ", SI_BASE, JOULE, 1E-6, true);
165         MILLIJOULE = new EnergyUnit("EnergyUnit.millijoule", "EnergyUnit.mJ", SI_BASE, JOULE, 1E-3, true);
166         KILOJOULE = new EnergyUnit("EnergyUnit.kilojoule", "EnergyUnit.kJ", SI_BASE, JOULE, 1000.0, true);
167         MEGAJOULE = new EnergyUnit("EnergyUnit.megajoule", "EnergyUnit.MJ", SI_BASE, JOULE, 1E9, true);
168         GIGAJOULE = new EnergyUnit("EnergyUnit.gigajoule", "EnergyUnit.GJ", SI_BASE, JOULE, 1E12, true);
169         TERAJOULE = new EnergyUnit("EnergyUnit.terajoule", "EnergyUnit.TJ", SI_BASE, JOULE, 1E15, true);
170         PETAJOULE = new EnergyUnit("EnergyUnit.petajoule", "EnergyUnit.PJ", SI_BASE, JOULE, 1E18, true);
171         FOOT_POUND_FORCE = new EnergyUnit(ForceUnit.POUND_FORCE, LengthUnit.FOOT, "EnergyUnit.foot_pound-force",
172                 "EnergyUnit.ft.lbf", IMPERIAL, true);
173         INCH_POUND_FORCE = new EnergyUnit(ForceUnit.POUND_FORCE, LengthUnit.INCH, "EnergyUnit.inch_pound-force",
174                 "EnergyUnit.in.lbf", IMPERIAL, true);
175         BTU_ISO =
176                 new EnergyUnit("EnergyUnit.British_thermal_unit_(ISO)", "EnergyUnit.BTU(ISO)", IMPERIAL, JOULE, 1.0545E3, true);
177         BTU_IT = new EnergyUnit("EnergyUnit.British_thermal_unit_(International_Table)", "EnergyUnit.BTU(IT)", IMPERIAL, JOULE,
178                 1.05505585262E3, true);
179         CALORIE_IT =
180                 new EnergyUnit("EnergyUnit.calorie_(International_Table)", "EnergyUnit.cal(IT)", OTHER, JOULE, 4.1868, true);
181         CALORIE = new EnergyUnit("EnergyUnit.calorie", "EnergyUnit.cal", OTHER, JOULE, 4.184, true);
182         KILOCALORIE = new EnergyUnit("EnergyUnit.kilocalorie", "EnergyUnit.kcal", OTHER, CALORIE, 1000.0, true);
183         WATT_HOUR = new EnergyUnit("EnergyUnit.watt-hour", "EnergyUnit.Wh", SI_DERIVED, JOULE, 3600.0, true);
184         FEMTOWATT_HOUR = new EnergyUnit("EnergyUnit.femtowatt-hour", "EnergyUnit.fWh", SI_DERIVED, WATT_HOUR, 1.0E-15, true);
185         PICOWATT_HOUR = new EnergyUnit("EnergyUnit.picowatt-hour", "EnergyUnit.pWh", SI_DERIVED, WATT_HOUR, 1.0E-12, true);
186         NANOWATT_HOUR = new EnergyUnit("EnergyUnit.nanowatt-hour", "EnergyUnit.nWh", SI_DERIVED, WATT_HOUR, 1.0E-9, true);
187         MICROWATT_HOUR = new EnergyUnit("EnergyUnit.microwatt-hour", "EnergyUnit.muWh", SI_DERIVED, WATT_HOUR, 1.0E-6, true);
188         MILLIWATT_HOUR = new EnergyUnit("EnergyUnit.milliwatt-hour", "EnergyUnit.mWh", SI_DERIVED, WATT_HOUR, 1.0E-3, true);
189         KILOWATT_HOUR = new EnergyUnit("EnergyUnit.kilowatt-hour", "EnergyUnit.kWh", SI_DERIVED, WATT_HOUR, 1000.0, true);
190         MEGAWATT_HOUR = new EnergyUnit("EnergyUnit.megawatt-hour", "EnergyUnit.MWh", SI_DERIVED, WATT_HOUR, 1.0E6, true);
191         GIGAWATT_HOUR = new EnergyUnit("EnergyUnit.gigawatt-hour", "EnergyUnit.GWh", SI_DERIVED, WATT_HOUR, 1.0E9, true);
192         TERAWATT_HOUR = new EnergyUnit("EnergyUnit.terawatt-hour", "EnergyUnit.TWh", SI_DERIVED, WATT_HOUR, 1.0E12, true);
193         PETAWATT_HOUR = new EnergyUnit("EnergyUnit.petawatt-hour", "EnergyUnit.PWh", SI_DERIVED, WATT_HOUR, 1.0E15, true);
194         ELECTRONVOLT = new EnergyUnit("EnergyUnit.electronvolt", "EnergyUnit.eV", SI_ACCEPTED, JOULE, 1.602176565314E-19, true);
195         MICROELECTRONVOLT =
196                 new EnergyUnit("EnergyUnit.micro-electronvolt", "EnergyUnit.mueV", SI_ACCEPTED, ELECTRONVOLT, 1.0E-6, true);
197         MILLIELECTRONVOLT =
198                 new EnergyUnit("EnergyUnit.milli-electronvolt", "EnergyUnit.meV", SI_ACCEPTED, ELECTRONVOLT, 1.0E-3, true);
199         KILOELECTRONVOLT =
200                 new EnergyUnit("EnergyUnit.kilo-electronvolt", "EnergyUnit.keV", SI_ACCEPTED, ELECTRONVOLT, 1.0E3, true);
201         MEGAELECTRONVOLT =
202                 new EnergyUnit("EnergyUnit.mega-electronvolt", "EnergyUnit.MeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E6, true);
203         GIGAELECTRONVOLT =
204                 new EnergyUnit("EnergyUnit.giga-electronvolt", "EnergyUnit.GeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E9, true);
205         TERAELECTRONVOLT =
206                 new EnergyUnit("EnergyUnit.tera-electronvolt", "EnergyUnit.TeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E12, true);
207         PETAELECTRONVOLT =
208                 new EnergyUnit("EnergyUnit.peta-electronvolt", "EnergyUnit.PeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E15, true);
209         EXAELECTRONVOLT =
210                 new EnergyUnit("EnergyUnit.exa-electronvolt", "EnergyUnit.EeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E18, true);
211         STHENE_METER = new EnergyUnit("EnergyUnit.sthene_meter", "EnergyUnit.sn.m", MTS, JOULE, 1000.0, true);
212         ERG = new EnergyUnit("EnergyUnit.erg_(full)", "EnergyUnit.erg", CGS, JOULE, 1.0E-7, true);
213     }
214 
215     /**
216      * Define an EnergyUnit based on its constituent base units, e.g. a J = km.m^2/s^2.
217      * @param massUnit MassUnit; the unit of mass for the energy unit, e.g., kilogram
218      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., meter
219      * @param durationUnit DurationUnit; the unit of time for the energy unit, e.g., second
220      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
221      *            name itself
222      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
223      *            unit, otherwise the abbreviation itself
224      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
225      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
226      *            unit
227      */
228     private EnergyUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final DurationUnit durationUnit,
229             final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
230             final boolean standardUnit)
231     {
232         super(nameOrNameKey,
233                 abbreviationOrAbbreviationKey, unitSystem, JOULE, massUnit.getScaleFactor() * lengthUnit.getScaleFactor()
234                         * lengthUnit.getScaleFactor() / (durationUnit.getScaleFactor() * durationUnit.getScaleFactor()),
235                 standardUnit);
236         this.massUnit = massUnit;
237         this.lengthUnit = lengthUnit;
238         this.durationUnit = durationUnit;
239     }
240 
241     /**
242      * Define a user-defined EnergyUnit based on its constituent base units, e.g. a J = km.m^2/s^2.
243      * @param massUnit MassUnit; the unit of mass for the energy unit, e.g., kilogram
244      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., meter
245      * @param durationUnit DurationUnit; the unit of time for the energy unit, e.g., second
246      * @param name String; the long name of the unit
247      * @param abbreviation String; the abbreviation of the unit
248      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
249      */
250     public EnergyUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final DurationUnit durationUnit, final String name,
251             final String abbreviation, final UnitSystem unitSystem)
252     {
253         this(massUnit, lengthUnit, durationUnit, name, abbreviation, unitSystem, false);
254     }
255 
256     /**
257      * Define an EnergyUnit based on a LengthUnit and a ForceUnit, e.g. a J = N.m.
258      * @param forceUnit ForceUnit; the unit of force for the energy unit, e.g., Newton
259      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., Meter
260      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
261      *            name itself
262      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
263      *            unit, otherwise the abbreviation itself
264      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
265      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
266      *            unit
267      */
268     private EnergyUnit(final ForceUnit forceUnit, final LengthUnit lengthUnit, final String nameOrNameKey,
269             final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
270     {
271         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, JOULE,
272                 forceUnit.getScaleFactor() * lengthUnit.getScaleFactor(), standardUnit);
273         this.massUnit = forceUnit.getMassUnit();
274         this.lengthUnit = forceUnit.getLengthUnit();
275         this.durationUnit = forceUnit.getDurationUnit();
276     }
277 
278     /**
279      * Define a user-defined EnergyUnit based on a LengthUnit and a ForceUnit, e.g. a J = N.m.
280      * @param forceUnit ForceUnit; the unit of force for the energy unit, e.g., Newton
281      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., Meter
282      * @param name String; the long name of the unit
283      * @param abbreviation String; the abbreviation of the unit
284      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
285      */
286     public EnergyUnit(final ForceUnit forceUnit, final LengthUnit lengthUnit, final String name, final String abbreviation,
287             final UnitSystem unitSystem)
288     {
289         this(forceUnit, lengthUnit, name, abbreviation, unitSystem, false);
290     }
291 
292     /**
293      * Build an EnergyUnit with a conversion factor to another EnergyUnit.
294      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
295      *            name itself
296      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
297      *            unit, otherwise the abbreviation itself
298      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
299      * @param referenceUnit EnergyUnit; the unit to convert to
300      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
301      *            unit
302      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
303      *            unit
304      */
305     private EnergyUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
306             final EnergyUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
307     {
308         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
309                 standardUnit);
310         this.massUnit = referenceUnit.getMassUnit();
311         this.lengthUnit = referenceUnit.getLengthUnit();
312         this.durationUnit = referenceUnit.getDurationUnit();
313     }
314 
315     /**
316      * Build a user-defined EnergyUnit with a conversion factor to another EnergyUnit.
317      * @param name String; the long name of the unit
318      * @param abbreviation String; the abbreviation of the unit
319      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
320      * @param referenceUnit EnergyUnit; the unit to convert to
321      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
322      *            unit
323      */
324     public EnergyUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final EnergyUnit referenceUnit,
325             final double scaleFactorToReferenceUnit)
326     {
327         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
328     }
329 
330     /**
331      * @return massUnit
332      */
333     public final MassUnit getMassUnit()
334     {
335         return this.massUnit;
336     }
337 
338     /**
339      * @return lengthUnit
340      */
341     public final LengthUnit getLengthUnit()
342     {
343         return this.lengthUnit;
344     }
345 
346     /**
347      * @return durationUnit
348      */
349     public final DurationUnit getDurationUnit()
350     {
351         return this.durationUnit;
352     }
353 
354     /** {@inheritDoc} */
355     @Override
356     public final EnergyUnit getStandardUnit()
357     {
358         return JOULE;
359     }
360 
361     /** {@inheritDoc} */
362     @Override
363     public final String getSICoefficientsString()
364     {
365         return "kgm2/s2";
366     }
367 
368 }