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-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, 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.J", SI_DERIVED);
160         JOULE = SI;
161         PICOJOULE = new EnergyUnit("EnergyUnit.pJ", SI_BASE, JOULE, 1E-12);
162         NANOJOULE = new EnergyUnit("EnergyUnit.nJ", SI_BASE, JOULE, 1E-9);
163         MICROJOULE = new EnergyUnit("EnergyUnit.muJ", SI_BASE, JOULE, 1E-6);
164         MILLIJOULE = new EnergyUnit("EnergyUnit.mJ", SI_BASE, JOULE, 1E-3);
165         KILOJOULE = new EnergyUnit("EnergyUnit.kJ", SI_BASE, JOULE, 1000.0);
166         MEGAJOULE = new EnergyUnit("EnergyUnit.MJ", SI_BASE, JOULE, 1E9);
167         GIGAJOULE = new EnergyUnit("EnergyUnit.GJ", SI_BASE, JOULE, 1E12);
168         TERAJOULE = new EnergyUnit("EnergyUnit.TJ", SI_BASE, JOULE, 1E15);
169         PETAJOULE = new EnergyUnit("EnergyUnit.PJ", SI_BASE, JOULE, 1E18);
170         FOOT_POUND_FORCE = new EnergyUnit(ForceUnit.POUND_FORCE, LengthUnit.FOOT, "EnergyUnit.ft.lbf", IMPERIAL);
171         INCH_POUND_FORCE = new EnergyUnit(ForceUnit.POUND_FORCE, LengthUnit.INCH, "EnergyUnit.in.lbf", IMPERIAL);
172         BTU_ISO = new EnergyUnit("EnergyUnit.BTU(ISO)", IMPERIAL, JOULE, 1.0545E3);
173         BTU_IT = new EnergyUnit("EnergyUnit.BTU(IT)", IMPERIAL, JOULE, 1.05505585262E3);
174         CALORIE_IT = new EnergyUnit("EnergyUnit.cal(IT)", OTHER, JOULE, 4.1868);
175         CALORIE = new EnergyUnit("EnergyUnit.cal", OTHER, JOULE, 4.184);
176         KILOCALORIE = new EnergyUnit("EnergyUnit.kcal", OTHER, CALORIE, 1000.0);
177         WATT_HOUR = new EnergyUnit("EnergyUnit.Wh", SI_DERIVED, JOULE, 3600.0);
178         FEMTOWATT_HOUR = new EnergyUnit("EnergyUnit.fWh", SI_DERIVED, WATT_HOUR, 1.0E-15);
179         PICOWATT_HOUR = new EnergyUnit("EnergyUnit.pWh", SI_DERIVED, WATT_HOUR, 1.0E-12);
180         NANOWATT_HOUR = new EnergyUnit("EnergyUnit.nWh", SI_DERIVED, WATT_HOUR, 1.0E-9);
181         MICROWATT_HOUR = new EnergyUnit("EnergyUnit.muWh", SI_DERIVED, WATT_HOUR, 1.0E-6);
182         MILLIWATT_HOUR = new EnergyUnit("EnergyUnit.mWh", SI_DERIVED, WATT_HOUR, 1.0E-3);
183         KILOWATT_HOUR = new EnergyUnit("EnergyUnit.kWh", SI_DERIVED, WATT_HOUR, 1000.0);
184         MEGAWATT_HOUR = new EnergyUnit("EnergyUnit.MWh", SI_DERIVED, WATT_HOUR, 1.0E6);
185         GIGAWATT_HOUR = new EnergyUnit("EnergyUnit.GWh", SI_DERIVED, WATT_HOUR, 1.0E9);
186         TERAWATT_HOUR = new EnergyUnit("EnergyUnit.TWh", SI_DERIVED, WATT_HOUR, 1.0E12);
187         PETAWATT_HOUR = new EnergyUnit("EnergyUnit.PWh", SI_DERIVED, WATT_HOUR, 1.0E15);
188         ELECTRONVOLT = new EnergyUnit("EnergyUnit.eV", SI_ACCEPTED, JOULE, 1.602176565314E-19);
189         MICROELECTRONVOLT = new EnergyUnit("EnergyUnit.mueV", SI_ACCEPTED, ELECTRONVOLT, 1.0E-6);
190         MILLIELECTRONVOLT = new EnergyUnit("EnergyUnit.meV", SI_ACCEPTED, ELECTRONVOLT, 1.0E-3);
191         KILOELECTRONVOLT = new EnergyUnit("EnergyUnit.keV", SI_ACCEPTED, ELECTRONVOLT, 1.0E3);
192         MEGAELECTRONVOLT = new EnergyUnit("EnergyUnit.MeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E6);
193         GIGAELECTRONVOLT = new EnergyUnit("EnergyUnit.GeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E9);
194         TERAELECTRONVOLT = new EnergyUnit("EnergyUnit.TeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E12);
195         PETAELECTRONVOLT = new EnergyUnit("EnergyUnit.PeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E15);
196         EXAELECTRONVOLT = new EnergyUnit("EnergyUnit.EeV", SI_ACCEPTED, ELECTRONVOLT, 1.0E18);
197         STHENE_METER = new EnergyUnit("EnergyUnit.sn.m", MTS, JOULE, 1000.0);
198         ERG = new EnergyUnit("EnergyUnit.erg", CGS, JOULE, 1.0E-7);
199     }
200 
201     /**
202      * Define an EnergyUnit based on its constituent base units, e.g. a J = km.m^2/s^2.
203      * @param massUnit MassUnit; the unit of mass for the energy unit, e.g., kilogram
204      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., meter
205      * @param durationUnit DurationUnit; the unit of time for the energy unit, e.g., second
206      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
207      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
208      */
209     private EnergyUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final DurationUnit durationUnit,
210             final String abbreviationKey, final UnitSystem unitSystem)
211     {
212         super(abbreviationKey, unitSystem, JOULE, massUnit.getScaleFactor() * lengthUnit.getScaleFactor()
213                 * lengthUnit.getScaleFactor() / (durationUnit.getScaleFactor() * durationUnit.getScaleFactor()));
214         this.massUnit = massUnit;
215         this.lengthUnit = lengthUnit;
216         this.durationUnit = durationUnit;
217     }
218 
219     /**
220      * Define a user-defined EnergyUnit based on its constituent base units, e.g. a J = km.m^2/s^2.
221      * @param massUnit MassUnit; the unit of mass for the energy unit, e.g., kilogram
222      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., meter
223      * @param durationUnit DurationUnit; the unit of time for the energy unit, e.g., second
224      * @param name String; the long name of the unit
225      * @param abbreviation String; the abbreviation of the unit
226      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
227      */
228     public EnergyUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final DurationUnit durationUnit, final String name,
229             final String abbreviation, final UnitSystem unitSystem)
230     {
231         super(name, abbreviation, unitSystem, JOULE, massUnit.getScaleFactor() * lengthUnit.getScaleFactor()
232                 * lengthUnit.getScaleFactor() / (durationUnit.getScaleFactor() * durationUnit.getScaleFactor()));
233         this.massUnit = massUnit;
234         this.lengthUnit = lengthUnit;
235         this.durationUnit = durationUnit;
236     }
237 
238     /**
239      * Define an EnergyUnit based on a LengthUnit and a ForceUnit, e.g. a J = N.m.
240      * @param forceUnit ForceUnit; the unit of force for the energy unit, e.g., Newton
241      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., Meter
242      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
243      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
244      */
245     private EnergyUnit(final ForceUnit forceUnit, final LengthUnit lengthUnit, final String abbreviationKey,
246             final UnitSystem unitSystem)
247     {
248         super(abbreviationKey, unitSystem, JOULE, forceUnit.getScaleFactor() * lengthUnit.getScaleFactor());
249         this.massUnit = forceUnit.getMassUnit();
250         this.lengthUnit = forceUnit.getLengthUnit();
251         this.durationUnit = forceUnit.getDurationUnit();
252     }
253 
254     /**
255      * Define a user-defined EnergyUnit based on a LengthUnit and a ForceUnit, e.g. a J = N.m.
256      * @param forceUnit ForceUnit; the unit of force for the energy unit, e.g., Newton
257      * @param lengthUnit LengthUnit; the unit of length for the energy unit, e.g., Meter
258      * @param name String; the long name of the unit
259      * @param abbreviation String; the abbreviation of the unit
260      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
261      */
262     public EnergyUnit(final ForceUnit forceUnit, final LengthUnit lengthUnit, final String name, final String abbreviation,
263             final UnitSystem unitSystem)
264     {
265         super(name, abbreviation, unitSystem, JOULE, forceUnit.getScaleFactor() * lengthUnit.getScaleFactor());
266         this.massUnit = forceUnit.getMassUnit();
267         this.lengthUnit = forceUnit.getLengthUnit();
268         this.durationUnit = forceUnit.getDurationUnit();
269     }
270 
271     /**
272      * Build an EnergyUnit with a conversion factor to another EnergyUnit.
273      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
274      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
275      * @param referenceUnit EnergyUnit; the unit to convert to
276      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
277      *            unit
278      */
279     private EnergyUnit(final String abbreviationKey, final UnitSystem unitSystem, final EnergyUnit referenceUnit,
280             final double scaleFactorToReferenceUnit)
281     {
282         super(abbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
283         this.massUnit = referenceUnit.getMassUnit();
284         this.lengthUnit = referenceUnit.getLengthUnit();
285         this.durationUnit = referenceUnit.getDurationUnit();
286     }
287 
288     /**
289      * Build a user-defined EnergyUnit with a conversion factor to another EnergyUnit.
290      * @param name String; the long name of the unit
291      * @param abbreviation String; the abbreviation of the unit
292      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
293      * @param referenceUnit EnergyUnit; the unit to convert to
294      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
295      *            unit
296      */
297     public EnergyUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final EnergyUnit referenceUnit,
298             final double scaleFactorToReferenceUnit)
299     {
300         super(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
301         this.massUnit = referenceUnit.getMassUnit();
302         this.lengthUnit = referenceUnit.getLengthUnit();
303         this.durationUnit = referenceUnit.getDurationUnit();
304     }
305 
306     /**
307      * @return massUnit
308      */
309     public final MassUnit getMassUnit()
310     {
311         return this.massUnit;
312     }
313 
314     /**
315      * @return lengthUnit
316      */
317     public final LengthUnit getLengthUnit()
318     {
319         return this.lengthUnit;
320     }
321 
322     /**
323      * @return durationUnit
324      */
325     public final DurationUnit getDurationUnit()
326     {
327         return this.durationUnit;
328     }
329 
330     /** {@inheritDoc} */
331     @Override
332     public final EnergyUnit getStandardUnit()
333     {
334         return JOULE;
335     }
336 
337     /** {@inheritDoc} */
338     @Override
339     public final String getSICoefficientsString()
340     {
341         return "kgm2/s2";
342     }
343 
344 }