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