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