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 * Duration is the interval of time between two events, measured in seconds (s). This quantity encodes a <i>relative</i> amount
19 * of time. The Time quantity encodes an absolute time instant.
20 * <p>
21 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22 * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
23 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
24 * @author Alexander Verbraeck
25 */
26
27 public class Duration extends Quantity<Duration>
28 {
29 /** Constant with value zero. */
30 public static final Duration ZERO = ofSi(0.0);
31
32 /** Constant with value one. */
33 public static final Duration ONE = ofSi(1.0);
34
35 /** Constant with value NaN. */
36 @SuppressWarnings("checkstyle:constantname")
37 public static final Duration NaN = ofSi(Double.NaN);
38
39 /** Constant with value POSITIVE_INFINITY. */
40 public static final Duration POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
41
42 /** Constant with value NEGATIVE_INFINITY. */
43 public static final Duration NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
44
45 /** Constant with value MAX_VALUE. */
46 public static final Duration POS_MAXVALUE = ofSi(Double.MAX_VALUE);
47
48 /** Constant with value -MAX_VALUE. */
49 public static final Duration NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
50
51 /** */
52 private static final long serialVersionUID = 600L;
53
54 /**
55 * Instantiate a Duration quantity with an SI or base value and a display unit.
56 * @param value the quantity value expressed in the SI or base unit
57 * @param displayUnit the display unit to use
58 * @param useSi use SI value when true, use value in unit when false
59 */
60 public Duration(final double value, final Duration.Unit displayUnit, final boolean useSi)
61 {
62 super(value, displayUnit, useSi);
63 }
64
65 /**
66 * Instantiate a Duration quantity expressed in the given unit.
67 * @param valueInUnit the quantity value expressed in the given unit
68 * @param unit the unit of the value, also acts as the display unit
69 */
70 public Duration(final double valueInUnit, final Duration.Unit unit)
71 {
72 this(valueInUnit, unit, false);
73 }
74
75 /**
76 * Return a Duration instance based on an SI value.
77 * @param si the si value
78 * @return the Duration instance based on an SI value
79 */
80 public static Duration ofSi(final double si)
81 {
82 return new Duration(si, Duration.Unit.SI, true);
83 }
84
85 /**
86 * Instantiate a Duration quantity with an SI or base value and a display unit.
87 * @param siValue the quantity value expressed in the SI or base unit
88 * @param displayUnit the display unit to use
89 * @return the Duration instance based on an SI value with the given display unit
90 */
91 public static Duration ofSi(final double siValue, final Duration.Unit displayUnit)
92 {
93 return new Duration(siValue, displayUnit, true);
94 }
95
96 @Override
97 public Duration instantiateSi(final double siValue, final UnitInterface<Duration> displayUnit)
98 {
99 return new Duration(siValue, (Duration.Unit) displayUnit, true);
100 }
101
102 /**
103 * Returns a Duration representation of a textual representation of a value with a unit. The String representation that can
104 * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
105 * allowed, but not required, between the value and the unit.
106 * @param text the textual representation to parse into a Duration
107 * @return the Scalar representation of the value in its unit
108 * @throws IllegalArgumentException when the text cannot be parsed
109 * @throws NullPointerException when the text argument is null
110 */
111 public static Duration valueOf(final String text)
112 {
113 return Quantity.valueOf(text, ZERO);
114 }
115
116 /**
117 * Returns a Duration based on a value expressed in the unit.
118 * @param valueInUnit the value, expressed in the given unit
119 * @param unit the unit of the value, also acts as the display unit
120 * @return ab Duration representation of the value in its unit
121 */
122 public static Duration of(final double valueInUnit, final Duration.Unit unit)
123 {
124 return new Duration(valueInUnit, unit);
125 }
126
127 /**
128 * Returns a Duration based on a value and the textual representation of the unit, which can be localized.
129 * @param valueInUnit the value, expressed in the unit as given by unitString
130 * @param unitString the textual representation of the unit
131 * @return the Scalar representation of the value in its unit
132 * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
133 * @throws NullPointerException when the unitString argument is null
134 */
135 public static Duration of(final double valueInUnit, final String unitString)
136 {
137 return Quantity.of(valueInUnit, unitString, ZERO);
138 }
139
140 @Override
141 public Duration.Unit getDisplayUnit()
142 {
143 return (Duration.Unit) super.getDisplayUnit();
144 }
145
146 /**
147 * Add an (absolute) time to this duration, and return a time. The unit of the return value will be the unit of this
148 * duration, and the reference of the return value will be the reference belonging to the given time. <code>R.add(A)</code>
149 * = unit of R and reference value of A.
150 * @param time the absolute time to add
151 * @return the absolute time plus this duration
152 */
153 public Time add(final Time time)
154 {
155 return new Time(new Duration(time.si() + si(), getDisplayUnit(), true), time.getReference());
156 }
157
158 /**
159 * Calculate the division of Duration and Duration, which results in a Dimensionless quantity.
160 * @param v quantity
161 * @return quantity as a division of Duration and Duration
162 */
163 public Dimensionless divide(final Duration v)
164 {
165 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
166 }
167
168 /**
169 * Calculate the multiplication of Duration and ElectricCurrent, which results in a ElectricCharge scalar.
170 * @param v scalar
171 * @return scalar as a multiplication of Duration and ElectricCurrent
172 */
173 public ElectricCharge multiply(final ElectricCurrent v)
174 {
175 return new ElectricCharge(this.si() * v.si(), ElectricCharge.Unit.SI);
176 }
177
178 /**
179 * Calculate the multiplication of Duration and FlowMass, which results in a Mass scalar.
180 * @param v scalar
181 * @return scalar as a multiplication of Duration and FlowMass
182 */
183 public Mass multiply(final FlowMass v)
184 {
185 return new Mass(this.si() * v.si(), Mass.Unit.SI);
186 }
187
188 /**
189 * Calculate the multiplication of Duration and FlowVolume, which results in a Volume scalar.
190 * @param v scalar
191 * @return scalar as a multiplication of Duration and FlowVolume
192 */
193 public Volume multiply(final FlowVolume v)
194 {
195 return new Volume(this.si() * v.si(), Volume.Unit.SI);
196 }
197
198 /**
199 * Calculate the multiplication of Duration and Acceleration, which results in a Speed scalar.
200 * @param v scalar
201 * @return scalar as a multiplication of Duration and Acceleration
202 */
203 public Speed multiply(final Acceleration v)
204 {
205 return new Speed(this.si() * v.si(), Speed.Unit.SI);
206 }
207
208 /**
209 * Calculate the multiplication of Duration and Power, which results in a Energy scalar.
210 * @param v scalar
211 * @return scalar as a multiplication of Duration and Power
212 */
213 public Energy multiply(final Power v)
214 {
215 return new Energy(this.si() * v.si(), Energy.Unit.SI);
216 }
217
218 /**
219 * Calculate the multiplication of Duration and Speed, which results in a Length scalar.
220 * @param v scalar
221 * @return scalar as a multiplication of Duration and Speed
222 */
223 public Length multiply(final Speed v)
224 {
225 return new Length(this.si() * v.si(), Length.Unit.SI);
226 }
227
228 /**
229 * Calculate the multiplication of Duration and ElectricPotential, which results in a MagneticFlux scalar.
230 * @param v scalar
231 * @return scalar as a multiplication of Duration and ElectricPotential
232 */
233 public MagneticFlux multiply(final ElectricPotential v)
234 {
235 return new MagneticFlux(this.si() * v.si(), MagneticFlux.Unit.SI);
236 }
237
238 /**
239 * Calculate the multiplication of Duration and ElectricalResistance, which results in a ElectricalInductance scalar.
240 * @param v scalar
241 * @return scalar as a multiplication of Duration and ElectricalResistance
242 */
243 public ElectricalInductance multiply(final ElectricalResistance v)
244 {
245 return new ElectricalInductance(this.si() * v.si(), ElectricalInductance.Unit.SI);
246 }
247
248 /**
249 * Calculate the multiplication of Duration and ElectricalConductance, which results in a ElectricalCapacitance scalar.
250 * @param v scalar
251 * @return scalar as a multiplication of Duration and ElectricalConductance
252 */
253 public ElectricalCapacitance multiply(final ElectricalConductance v)
254 {
255 return new ElectricalCapacitance(this.si() * v.si(), ElectricalCapacitance.Unit.SI);
256 }
257
258 /**
259 * Calculate the multiplication of Duration and AngularVelocity, which results in a Angle scalar.
260 * @param v scalar
261 * @return scalar as a multiplication of Duration and AngularVelocity
262 */
263 public Angle multiply(final AngularVelocity v)
264 {
265 return new Angle(this.si() * v.si(), Angle.Unit.SI);
266 }
267
268 /**
269 * Calculate the multiplication of Duration and AngularAcceleration, which results in a AngularVelocity scalar.
270 * @param v scalar
271 * @return scalar as a multiplication of Duration and AngularAcceleration
272 */
273 public AngularVelocity multiply(final AngularAcceleration v)
274 {
275 return new AngularVelocity(this.si() * v.si(), AngularVelocity.Unit.SI);
276 }
277
278 @Override
279 public Frequency reciprocal()
280 {
281 return Frequency.ofSi(1.0 / this.si());
282 }
283
284 /******************************************************************************************************/
285 /********************************************** UNIT CLASS ********************************************/
286 /******************************************************************************************************/
287
288 /**
289 * Duration.Unit encodes the units of relative time.
290 * <p>
291 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
292 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
293 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
294 * @author Alexander Verbraeck
295 */
296 @SuppressWarnings("checkstyle:constantname")
297 public static class Unit extends AbstractUnit<Duration>
298 {
299 /** The dimensions of duration: s. */
300 public static final SIUnit SI_UNIT = SIUnit.of("s");
301
302 /** second. */
303 public static final Duration.Unit s =
304 new Duration.Unit("s", "s", "second", IdentityScale.SCALE, UnitSystem.SI_BASE, SIPrefixes.getSiPrefix(""));
305
306 /** The SI or BASE unit. */
307 public static final Duration.Unit SI = (Unit) s.generateSiPrefixes(false, false);
308
309 /** picosecond. */
310 public static final Duration.Unit ps = Units.resolve(Duration.Unit.class, "ps");
311
312 /** nanosecond. */
313 public static final Duration.Unit ns = Units.resolve(Duration.Unit.class, "ns");
314
315 /** microsecond. */
316 public static final Duration.Unit mus = Units.resolve(Duration.Unit.class, "mus");
317
318 /** millisecond. */
319 public static final Duration.Unit ms = Units.resolve(Duration.Unit.class, "ms");
320
321 /** minute. */
322 public static final Duration.Unit min = s.deriveUnit("min", "minute", 60.0, UnitSystem.SI_ACCEPTED);
323
324 /** hour. */
325 public static final Duration.Unit h = min.deriveUnit("h", "hour", 60.0, UnitSystem.SI_ACCEPTED);
326
327 /** day. */
328 public static final Duration.Unit day = h.deriveUnit("day", "day", 24.0, UnitSystem.OTHER);
329
330 /** week. */
331 public static final Duration.Unit wk = day.deriveUnit("wk", "week", 7.0, UnitSystem.OTHER);
332
333 /**
334 * Create a new Duration unit.
335 * @param id the id or main abbreviation of the unit
336 * @param name the full name of the unit
337 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
338 * @param unitSystem the unit system such as SI or IMPERIAL
339 */
340 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
341 {
342 super(id, name, scaleFactorToBaseUnit, unitSystem);
343 }
344
345 /**
346 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
347 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
348 * @param displayAbbreviation the display abbreviation of the unit
349 * @param name the full name of the unit
350 * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
351 * @param unitSystem unit system, e.g. SI or Imperial
352 * @param siPrefix the SI Prefix of this unit
353 */
354 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
355 final UnitSystem unitSystem, final SIPrefix siPrefix)
356 {
357 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
358 }
359
360 @Override
361 public SIUnit siUnit()
362 {
363 return SI_UNIT;
364 }
365
366 @Override
367 public Unit getBaseUnit()
368 {
369 return SI;
370 }
371
372 @Override
373 public Duration ofSi(final double si, final UnitInterface<Duration> displayUnit)
374 {
375 return new Duration(si, (Unit) displayUnit, true);
376 }
377
378 @Override
379 public Duration.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
380 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
381 {
382 if (getScale() instanceof LinearScale ls)
383 {
384 return new Duration.Unit(textualAbbreviation, displayAbbreviation, name,
385 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
386 }
387 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
388 }
389
390 @Override
391 public Duration.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
392 final UnitSystem unitSystem)
393 {
394 return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
395 }
396
397 }
398
399 }