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-2018 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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, 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 the unit of mass for the energy unit, e.g., kilogram
218      * @param lengthUnit the unit of length for the energy unit, e.g., meter
219      * @param durationUnit the unit of time for the energy unit, e.g., second
220      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
221      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
222      *            otherwise the abbreviation itself
223      * @param unitSystem the unit system, e.g. SI or Imperial
224      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
225      */
226     private EnergyUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final DurationUnit durationUnit,
227             final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
228             final boolean standardUnit)
229     {
230         super(nameOrNameKey,
231                 abbreviationOrAbbreviationKey, unitSystem, JOULE, massUnit.getScaleFactor() * lengthUnit.getScaleFactor()
232                         * lengthUnit.getScaleFactor() / (durationUnit.getScaleFactor() * durationUnit.getScaleFactor()),
233                 standardUnit);
234         this.massUnit = massUnit;
235         this.lengthUnit = lengthUnit;
236         this.durationUnit = durationUnit;
237     }
238 
239     /**
240      * Define a user-defined EnergyUnit based on its constituent base units, e.g. a J = km.m^2/s^2.
241      * @param massUnit the unit of mass for the energy unit, e.g., kilogram
242      * @param lengthUnit the unit of length for the energy unit, e.g., meter
243      * @param durationUnit the unit of time for the energy unit, e.g., second
244      * @param name the long name of the unit
245      * @param abbreviation the abbreviation of the unit
246      * @param unitSystem the unit system, e.g. SI or Imperial
247      */
248     public EnergyUnit(final MassUnit massUnit, final LengthUnit lengthUnit, final DurationUnit durationUnit, final String name,
249             final String abbreviation, final UnitSystem unitSystem)
250     {
251         this(massUnit, lengthUnit, durationUnit, name, abbreviation, unitSystem, false);
252     }
253 
254     /**
255      * Define an EnergyUnit based on a LengthUnit and a ForceUnit, e.g. a J = N.m.
256      * @param forceUnit the unit of force for the energy unit, e.g., Newton
257      * @param lengthUnit the unit of length for the energy unit, e.g., Meter
258      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
259      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
260      *            otherwise the abbreviation itself
261      * @param unitSystem the unit system, e.g. SI or Imperial
262      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
263      */
264     private EnergyUnit(final ForceUnit forceUnit, final LengthUnit lengthUnit, final String nameOrNameKey,
265             final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
266     {
267         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, JOULE,
268                 forceUnit.getScaleFactor() * lengthUnit.getScaleFactor(), standardUnit);
269         this.massUnit = forceUnit.getMassUnit();
270         this.lengthUnit = forceUnit.getLengthUnit();
271         this.durationUnit = forceUnit.getDurationUnit();
272     }
273 
274     /**
275      * Define a user-defined EnergyUnit based on a LengthUnit and a ForceUnit, e.g. a J = N.m.
276      * @param forceUnit the unit of force for the energy unit, e.g., Newton
277      * @param lengthUnit the unit of length for the energy unit, e.g., Meter
278      * @param name the long name of the unit
279      * @param abbreviation the abbreviation of the unit
280      * @param unitSystem the unit system, e.g. SI or Imperial
281      */
282     public EnergyUnit(final ForceUnit forceUnit, final LengthUnit lengthUnit, final String name, final String abbreviation,
283             final UnitSystem unitSystem)
284     {
285         this(forceUnit, lengthUnit, name, abbreviation, unitSystem, false);
286     }
287 
288     /**
289      * Build an EnergyUnit with a conversion factor to another EnergyUnit.
290      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
291      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
292      *            otherwise the abbreviation itself
293      * @param unitSystem the unit system, e.g. SI or Imperial
294      * @param referenceUnit the unit to convert to
295      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
296      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
297      */
298     private EnergyUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
299             final EnergyUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
300     {
301         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
302                 standardUnit);
303         this.massUnit = referenceUnit.getMassUnit();
304         this.lengthUnit = referenceUnit.getLengthUnit();
305         this.durationUnit = referenceUnit.getDurationUnit();
306     }
307 
308     /**
309      * Build a user-defined EnergyUnit with a conversion factor to another EnergyUnit.
310      * @param name the long name of the unit
311      * @param abbreviation the abbreviation of the unit
312      * @param unitSystem the unit system, e.g. SI or Imperial
313      * @param referenceUnit the unit to convert to
314      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
315      */
316     public EnergyUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final EnergyUnit referenceUnit,
317             final double scaleFactorToReferenceUnit)
318     {
319         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
320     }
321 
322     /**
323      * @return massUnit
324      */
325     public final MassUnit getMassUnit()
326     {
327         return this.massUnit;
328     }
329 
330     /**
331      * @return lengthUnit
332      */
333     public final LengthUnit getLengthUnit()
334     {
335         return this.lengthUnit;
336     }
337 
338     /**
339      * @return durationUnit
340      */
341     public final DurationUnit getDurationUnit()
342     {
343         return this.durationUnit;
344     }
345 
346     /** {@inheritDoc} */
347     @Override
348     public final EnergyUnit getStandardUnit()
349     {
350         return JOULE;
351     }
352 
353     /** {@inheritDoc} */
354     @Override
355     public final String getSICoefficientsString()
356     {
357         return "kgm2/s2";
358     }
359 
360 }