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