1 package org.djunits.value.vdouble.scalar;
2
3 import java.util.Locale;
4
5 import org.djunits.unit.DimensionlessUnit;
6 import org.djunits.unit.DurationUnit;
7 import org.djunits.unit.EnergyUnit;
8 import org.djunits.unit.ForceUnit;
9 import org.djunits.unit.LengthUnit;
10 import org.djunits.unit.MomentumUnit;
11 import org.djunits.unit.PowerUnit;
12 import org.djunits.unit.PressureUnit;
13 import org.djunits.unit.SpeedUnit;
14 import org.djunits.unit.VolumeUnit;
15 import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
16 import org.djutils.base.NumberParser;
17 import org.djutils.exceptions.Throw;
18
19 import jakarta.annotation.Generated;
20
21
22
23
24
25
26
27
28
29
30 @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
31 public class Energy extends DoubleScalarRel<EnergyUnit, Energy>
32 {
33
34 private static final long serialVersionUID = 20150905L;
35
36
37 public static final Energy ZERO = new Energy(0.0, EnergyUnit.SI);
38
39
40 public static final Energy ONE = new Energy(1.0, EnergyUnit.SI);
41
42
43 @SuppressWarnings("checkstyle:constantname")
44 public static final Energy NaN = new Energy(Double.NaN, EnergyUnit.SI);
45
46
47 public static final Energy POSITIVE_INFINITY = new Energy(Double.POSITIVE_INFINITY, EnergyUnit.SI);
48
49
50 public static final Energy NEGATIVE_INFINITY = new Energy(Double.NEGATIVE_INFINITY, EnergyUnit.SI);
51
52
53 public static final Energy POS_MAXVALUE = new Energy(Double.MAX_VALUE, EnergyUnit.SI);
54
55
56 public static final Energy NEG_MAXVALUE = new Energy(-Double.MAX_VALUE, EnergyUnit.SI);
57
58
59
60
61
62
63 public Energy(final double value, final EnergyUnit unit)
64 {
65 super(value, unit);
66 }
67
68
69
70
71
72 public Energy(final Energy value)
73 {
74 super(value);
75 }
76
77 @Override
78 public final Energy instantiateRel(final double value, final EnergyUnit unit)
79 {
80 return new Energy(value, unit);
81 }
82
83
84
85
86
87
88 public static final Energy ofSI(final double value)
89 {
90 return new Energy(value, EnergyUnit.SI);
91 }
92
93
94
95
96
97
98
99
100 public static Energy interpolate(final Energy zero, final Energy one, final double ratio)
101 {
102 Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
103 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
104 return new Energy(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
105 }
106
107
108
109
110
111
112
113 public static Energy max(final Energy r1, final Energy r2)
114 {
115 return r1.gt(r2) ? r1 : r2;
116 }
117
118
119
120
121
122
123
124
125 public static Energy max(final Energy r1, final Energy r2, final Energy... rn)
126 {
127 Energy maxr = r1.gt(r2) ? r1 : r2;
128 for (Energy r : rn)
129 {
130 if (r.gt(maxr))
131 {
132 maxr = r;
133 }
134 }
135 return maxr;
136 }
137
138
139
140
141
142
143
144 public static Energy min(final Energy r1, final Energy r2)
145 {
146 return r1.lt(r2) ? r1 : r2;
147 }
148
149
150
151
152
153
154
155
156 public static Energy min(final Energy r1, final Energy r2, final Energy... rn)
157 {
158 Energy minr = r1.lt(r2) ? r1 : r2;
159 for (Energy r : rn)
160 {
161 if (r.lt(minr))
162 {
163 minr = r;
164 }
165 }
166 return minr;
167 }
168
169
170
171
172
173
174
175
176
177
178 public static Energy valueOf(final String text)
179 {
180 Throw.whenNull(text, "Error parsing Energy: text to parse is null");
181 Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Energy: empty text to parse");
182 try
183 {
184 NumberParser numberParser = new NumberParser().lenient().trailing();
185 double d = numberParser.parseDouble(text);
186 String unitString = text.substring(numberParser.getTrailingPosition()).trim();
187 EnergyUnit unit = EnergyUnit.BASE.getUnitByAbbreviation(unitString);
188 Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Energy", unitString);
189 return new Energy(d, unit);
190 }
191 catch (Exception exception)
192 {
193 throw new IllegalArgumentException(
194 "Error parsing Energy from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
195 exception);
196 }
197 }
198
199
200
201
202
203
204
205
206
207 public static Energy of(final double value, final String unitString)
208 {
209 Throw.whenNull(unitString, "Error parsing Energy: unitString is null");
210 Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Energy: empty unitString");
211 EnergyUnit unit = EnergyUnit.BASE.getUnitByAbbreviation(unitString);
212 Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Energy with unit %s", unitString);
213 return new Energy(value, unit);
214 }
215
216
217
218
219
220
221 public final Dimensionless divide(final Energy v)
222 {
223 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
224 }
225
226
227
228
229
230
231 public final Length divide(final Force v)
232 {
233 return new Length(this.si / v.si, LengthUnit.SI);
234 }
235
236
237
238
239
240
241 public final Force divide(final Length v)
242 {
243 return new Force(this.si / v.si, ForceUnit.SI);
244 }
245
246
247
248
249
250
251 public final Force times(final LinearDensity v)
252 {
253 return new Force(this.si * v.si, ForceUnit.SI);
254 }
255
256
257
258
259
260
261 public final Power divide(final Duration v)
262 {
263 return new Power(this.si / v.si, PowerUnit.SI);
264 }
265
266
267
268
269
270
271 public final Duration divide(final Power v)
272 {
273 return new Duration(this.si / v.si, DurationUnit.SI);
274 }
275
276
277
278
279
280
281 public final Pressure divide(final Volume v)
282 {
283 return new Pressure(this.si / v.si, PressureUnit.SI);
284 }
285
286
287
288
289
290
291 public final Volume divide(final Pressure v)
292 {
293 return new Volume(this.si / v.si, VolumeUnit.SI);
294 }
295
296
297
298
299
300
301 public final Power times(final Frequency v)
302 {
303 return new Power(this.si * v.si, PowerUnit.SI);
304 }
305
306
307
308
309
310
311 public final Momentum divide(final Speed v)
312 {
313 return new Momentum(this.si / v.si, MomentumUnit.SI);
314 }
315
316
317
318
319
320
321 public final Speed divide(final Momentum v)
322 {
323 return new Speed(this.si / v.si, SpeedUnit.SI);
324 }
325
326 @Override
327 public SIScalar reciprocal()
328 {
329 return SIScalar.divide(Dimensionless.ONE, this);
330 }
331
332
333
334
335
336
337
338 public static Energy multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
339 {
340 Throw.whenNull(scalar1, "scalar1 cannot be null");
341 Throw.whenNull(scalar2, "scalar2 cannot be null");
342 Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
343 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(EnergyUnit.BASE.getSiDimensions()),
344 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Energy",
345 scalar1.toDisplayString(), scalar2.toDisplayString());
346 return new Energy(scalar1.si * scalar2.si, EnergyUnit.SI);
347 }
348
349
350
351
352
353
354
355 public static Energy divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
356 {
357 Throw.whenNull(scalar1, "scalar1 cannot be null");
358 Throw.whenNull(scalar2, "scalar2 cannot be null");
359 Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
360 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(EnergyUnit.BASE.getSiDimensions()),
361 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Energy",
362 scalar1.toDisplayString(), scalar2.toDisplayString());
363 return new Energy(scalar1.si / scalar2.si, EnergyUnit.SI);
364 }
365
366 }