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
15
16
17
18
19
20
21 public class Energy extends Quantity<Energy>
22 {
23
24 public static final Energy ZERO = Energy.ofSi(0.0);
25
26
27 public static final Energy ONE = Energy.ofSi(1.0);
28
29
30 @SuppressWarnings("checkstyle:constantname")
31 public static final Energy NaN = Energy.ofSi(Double.NaN);
32
33
34 public static final Energy POSITIVE_INFINITY = Energy.ofSi(Double.POSITIVE_INFINITY);
35
36
37 public static final Energy NEGATIVE_INFINITY = Energy.ofSi(Double.NEGATIVE_INFINITY);
38
39
40 public static final Energy POS_MAXVALUE = Energy.ofSi(Double.MAX_VALUE);
41
42
43 public static final Energy NEG_MAXVALUE = Energy.ofSi(-Double.MAX_VALUE);
44
45
46 private static final long serialVersionUID = 600L;
47
48
49
50
51
52
53 public Energy(final double valueInUnit, final Energy.Unit unit)
54 {
55 super(valueInUnit, unit);
56 }
57
58
59
60
61
62
63 public Energy(final double valueInUnit, final String abbreviation)
64 {
65 this(valueInUnit, Units.resolve(Energy.Unit.class, abbreviation));
66 }
67
68
69
70
71
72 public Energy(final Energy value)
73 {
74 super(value.si(), Energy.Unit.SI);
75 setDisplayUnit(value.getDisplayUnit());
76 }
77
78
79
80
81
82
83 public static Energy ofSi(final double si)
84 {
85 return new Energy(si, Energy.Unit.SI);
86 }
87
88 @Override
89 public Energy instantiateSi(final double si)
90 {
91 return ofSi(si);
92 }
93
94 @Override
95 public SIUnit siUnit()
96 {
97 return Energy.Unit.SI_UNIT;
98 }
99
100
101
102
103
104
105
106
107
108
109 public static Energy valueOf(final String text)
110 {
111 return Quantity.valueOf(text, ZERO);
112 }
113
114
115
116
117
118
119
120
121
122 public static Energy of(final double valueInUnit, final String unitString)
123 {
124 return Quantity.of(valueInUnit, unitString, ZERO);
125 }
126
127 @Override
128 public Energy.Unit getDisplayUnit()
129 {
130 return (Energy.Unit) super.getDisplayUnit();
131 }
132
133
134
135
136
137
138 public final Dimensionless divide(final Energy v)
139 {
140 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
141 }
142
143
144
145
146
147
148 public final Length divide(final Force v)
149 {
150 return new Length(this.si() / v.si(), Length.Unit.SI);
151 }
152
153
154
155
156
157
158 public final Force divide(final Length v)
159 {
160 return new Force(this.si() / v.si(), Force.Unit.SI);
161 }
162
163
164
165
166
167
168 public final Force multiply(final LinearObjectDensity v)
169 {
170 return new Force(this.si() * v.si(), Force.Unit.SI);
171 }
172
173
174
175
176
177
178 public final Power divide(final Duration v)
179 {
180 return new Power(this.si() / v.si(), Power.Unit.SI);
181 }
182
183
184
185
186
187
188 public final Duration divide(final Power v)
189 {
190 return new Duration(this.si() / v.si(), Duration.Unit.SI);
191 }
192
193
194
195
196
197
198 public final Pressure divide(final Volume v)
199 {
200 return new Pressure(this.si() / v.si(), Pressure.Unit.SI);
201 }
202
203
204
205
206
207
208 public final Volume divide(final Pressure v)
209 {
210 return new Volume(this.si() / v.si(), Volume.Unit.SI);
211 }
212
213
214
215
216
217
218 public final Power multiply(final Frequency v)
219 {
220 return new Power(this.si() * v.si(), Power.Unit.SI);
221 }
222
223
224
225
226
227
228 public final Momentum divide(final Speed v)
229 {
230 return new Momentum(this.si() / v.si(), Momentum.Unit.SI);
231 }
232
233
234
235
236
237
238 public final Speed divide(final Momentum v)
239 {
240 return new Speed(this.si() / v.si(), Speed.Unit.SI);
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255 @SuppressWarnings("checkstyle:constantname")
256 public static class Unit extends AbstractUnit<Energy.Unit, Energy>
257 {
258
259 public static final SIUnit SI_UNIT = SIUnit.of("kgm2/s2");
260
261
262 public static final Energy.Unit J = new Energy.Unit("J", "joule", 1.0, UnitSystem.SI_DERIVED);
263
264
265 public static final Energy.Unit SI = J.generateSiPrefixes(false, false);
266
267
268 public static final Energy.Unit muJ = Units.resolve(Energy.Unit.class, "muJ");
269
270
271 public static final Energy.Unit mJ = Units.resolve(Energy.Unit.class, "mJ");
272
273
274 public static final Energy.Unit kJ = Units.resolve(Energy.Unit.class, "kJ");
275
276
277 public static final Energy.Unit MJ = Units.resolve(Energy.Unit.class, "MJ");
278
279
280 public static final Energy.Unit GJ = Units.resolve(Energy.Unit.class, "GJ");
281
282
283 public static final Energy.Unit TJ = Units.resolve(Energy.Unit.class, "TJ");
284
285
286 public static final Energy.Unit PJ = Units.resolve(Energy.Unit.class, "PJ");
287
288
289 public static final Energy.Unit ft_lbf = J.deriveUnit("ft.lbf", "foot pound-force",
290 Length.Unit.CONST_FT * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
291
292
293 public static final Energy.Unit in_lbf = J.deriveUnit("in.lbf", "inch pound-force",
294 Length.Unit.CONST_IN * Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY, UnitSystem.IMPERIAL);
295
296
297 public static final Energy.Unit BTU_ISO =
298 J.deriveUnit("BTU(ISO)", "British thermal unit (ISO)", 1.0545E3, UnitSystem.IMPERIAL);
299
300
301 public static final Energy.Unit BTU_IT =
302 J.deriveUnit("BTU(IT)", "British thermal unit (Int. Table)", 1.05505585262E3, UnitSystem.IMPERIAL);
303
304
305 public static final Energy.Unit cal_IT = J.deriveUnit("cal(IT)", "calorie (Int. Table)", 4.1868, UnitSystem.IMPERIAL);
306
307
308 public static final Energy.Unit cal = J.deriveUnit("cal", "calorie", 4.184, UnitSystem.OTHER);
309
310
311 public static final Energy.Unit kcal = cal.deriveUnit("kcal", "kilocalorie", 1000.0, UnitSystem.OTHER);
312
313
314 public static final Energy.Unit Wh = new Energy.Unit("Wh", "watt hour", 3600.0, UnitSystem.SI_DERIVED);
315
316
317 public static final Energy.Unit muWh =
318 Wh.deriveUnit("muWh", "\u03BCWh", "microwatt hour", 1E-6, UnitSystem.SI_DERIVED);
319
320
321 public static final Energy.Unit mWh = Wh.deriveUnit("mWh", "milliwatt hour", 1E-3, UnitSystem.SI_DERIVED);
322
323
324 public static final Energy.Unit kWh = Wh.deriveUnit("kWh", "kilowatt hour", 1E3, UnitSystem.SI_DERIVED);
325
326
327 public static final Energy.Unit MWh = Wh.deriveUnit("MWh", "megawatt hour", 1E6, UnitSystem.SI_DERIVED);
328
329
330 public static final Energy.Unit GWh = Wh.deriveUnit("GWh", "gigawatt hour", 1E9, UnitSystem.SI_DERIVED);
331
332
333 public static final Energy.Unit TWh = Wh.deriveUnit("TWh", "terawatt hour", 1E12, UnitSystem.SI_DERIVED);
334
335
336 public static final Energy.Unit PWh = Wh.deriveUnit("PWh", "petawatt hour", 1E15, UnitSystem.SI_DERIVED);
337
338
339 public static final Energy.Unit eV = new Energy.Unit("eV", "electronvolt", 1.602176634E-19, UnitSystem.SI_ACCEPTED);
340
341
342 public static final Energy.Unit keV = eV.deriveUnit("keV", "kiloelectronvolt", 1E3, UnitSystem.SI_ACCEPTED);
343
344
345 public static final Energy.Unit MeV = eV.deriveUnit("MeV", "megaelectronvolt", 1E6, UnitSystem.SI_ACCEPTED);
346
347
348 public static final Energy.Unit GeV = eV.deriveUnit("GeV", "gigaelectronvolt", 1E9, UnitSystem.SI_ACCEPTED);
349
350
351 public static final Energy.Unit sn_m = J.deriveUnit("sn.m", "sthene meter", 1000.0, UnitSystem.MTS);
352
353
354 public static final Energy.Unit erg = J.deriveUnit("erg", "erg", 1.0E-7, UnitSystem.CGS);
355
356
357
358
359
360
361
362
363 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
364 {
365 super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
366 }
367
368
369
370
371
372
373
374
375
376 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
377 final UnitSystem unitSystem)
378 {
379 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
380 }
381
382 @Override
383 public SIUnit siUnit()
384 {
385 return SI_UNIT;
386 }
387
388 @Override
389 public Unit getBaseUnit()
390 {
391 return SI;
392 }
393
394 @Override
395 public Energy ofSi(final double si)
396 {
397 return Energy.ofSi(si);
398 }
399
400 @Override
401 public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
402 final double scaleFactor, final UnitSystem unitSystem)
403 {
404 if (getScale() instanceof LinearScale ls)
405 {
406 return new Energy.Unit(textualAbbreviation, displayAbbreviation, name,
407 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
408 }
409 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
410 }
411
412 }
413 }