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, Duration.Unit>
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 value the value, expressed in the unit
53 * @param unit the unit in which the value is expressed
54 */
55 public Duration(final double value, final Duration.Unit unit)
56 {
57 super(value, unit);
58 }
59
60 /**
61 * Instantiate a Duration quantity with a unit, expressed as a String.
62 * @param value 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 value, final String abbreviation)
66 {
67 this(value, 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 instantiate(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 value the value to use
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 value, final String unitString)
125 {
126 return Quantity.of(value, unitString, ZERO);
127 }
128
129 /**
130 * Add an (absolute) time to this duration, and return a time. The unit of the return value will be the unit of this
131 * duration, and the reference of the return value will be the reference belonging to the given time. <code>R.add(A)</code>
132 * = unit of R and reference value of A.
133 * @param time the absolute time to add
134 * @return the absolute time plus this duration
135 */
136 public final Time add(final Time time)
137 {
138 return time.add(this).setDisplayUnit(getDisplayUnit());
139 }
140
141 /**
142 * Calculate the division of Duration and Duration, which results in a Dimensionless quantity.
143 * @param v quantity
144 * @return quantity as a division of Duration and Duration
145 */
146 public final Dimensionless divide(final Duration v)
147 {
148 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
149 }
150
151 /**
152 * Calculate the multiplication of Duration and ElectricCurrent, which results in a ElectricCharge scalar.
153 * @param v scalar
154 * @return scalar as a multiplication of Duration and ElectricCurrent
155 */
156 public final ElectricCharge multiply(final ElectricCurrent v)
157 {
158 return new ElectricCharge(this.si() * v.si(), ElectricCharge.Unit.SI);
159 }
160
161 /**
162 * Calculate the multiplication of Duration and FlowMass, which results in a Mass scalar.
163 * @param v scalar
164 * @return scalar as a multiplication of Duration and FlowMass
165 */
166 public final Mass multiply(final FlowMass v)
167 {
168 return new Mass(this.si() * v.si(), Mass.Unit.SI);
169 }
170
171 /**
172 * Calculate the multiplication of Duration and FlowVolume, which results in a Volume scalar.
173 * @param v scalar
174 * @return scalar as a multiplication of Duration and FlowVolume
175 */
176 public final Volume multiply(final FlowVolume v)
177 {
178 return new Volume(this.si() * v.si(), Volume.Unit.SI);
179 }
180
181 /**
182 * Calculate the multiplication of Duration and Acceleration, which results in a Speed scalar.
183 * @param v scalar
184 * @return scalar as a multiplication of Duration and Acceleration
185 */
186 public final Speed multiply(final Acceleration v)
187 {
188 return new Speed(this.si() * v.si(), Speed.Unit.SI);
189 }
190
191 /**
192 * Calculate the multiplication of Duration and Power, which results in a Energy scalar.
193 * @param v scalar
194 * @return scalar as a multiplication of Duration and Power
195 */
196 public final Energy multiply(final Power v)
197 {
198 return new Energy(this.si() * v.si(), Energy.Unit.SI);
199 }
200
201 /**
202 * Calculate the multiplication of Duration and Speed, which results in a Length scalar.
203 * @param v scalar
204 * @return scalar as a multiplication of Duration and Speed
205 */
206 public final Length multiply(final Speed v)
207 {
208 return new Length(this.si() * v.si(), Length.Unit.SI);
209 }
210
211 /**
212 * Calculate the multiplication of Duration and ElectricPotential, which results in a MagneticFlux scalar.
213 * @param v scalar
214 * @return scalar as a multiplication of Duration and ElectricPotential
215 */
216 public final MagneticFlux multiply(final ElectricPotential v)
217 {
218 return new MagneticFlux(this.si() * v.si(), MagneticFlux.Unit.SI);
219 }
220
221 /**
222 * Calculate the multiplication of Duration and ElectricalResistance, which results in a ElectricalInductance scalar.
223 * @param v scalar
224 * @return scalar as a multiplication of Duration and ElectricalResistance
225 */
226 public final ElectricalInductance multiply(final ElectricalResistance v)
227 {
228 return new ElectricalInductance(this.si() * v.si(), ElectricalInductance.Unit.SI);
229 }
230
231 /**
232 * Calculate the multiplication of Duration and ElectricalConductance, which results in a ElectricalCapacitance scalar.
233 * @param v scalar
234 * @return scalar as a multiplication of Duration and ElectricalConductance
235 */
236 public final ElectricalCapacitance multiply(final ElectricalConductance v)
237 {
238 return new ElectricalCapacitance(this.si() * v.si(), ElectricalCapacitance.Unit.SI);
239 }
240
241 /**
242 * Calculate the multiplication of Duration and AngularVelocity, which results in a Angle scalar.
243 * @param v scalar
244 * @return scalar as a multiplication of Duration and AngularVelocity
245 */
246 public final Angle multiply(final AngularVelocity v)
247 {
248 return new Angle(this.si() * v.si(), Angle.Unit.SI);
249 }
250
251 /**
252 * Calculate the multiplication of Duration and AngularAcceleration, which results in a AngularVelocity scalar.
253 * @param v scalar
254 * @return scalar as a multiplication of Duration and AngularAcceleration
255 */
256 public final AngularVelocity multiply(final AngularAcceleration v)
257 {
258 return new AngularVelocity(this.si() * v.si(), AngularVelocity.Unit.SI);
259 }
260
261 @Override
262 public Frequency reciprocal()
263 {
264 return Frequency.ofSi(1.0 / this.si());
265 }
266
267 /******************************************************************************************************/
268 /********************************************** UNIT CLASS ********************************************/
269 /******************************************************************************************************/
270
271 /**
272 * Duration.Unit encodes the units of relative time.
273 * <p>
274 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
275 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
276 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
277 * @author Alexander Verbraeck
278 */
279 @SuppressWarnings("checkstyle:constantname")
280 public static class Unit extends AbstractUnit<Duration.Unit, Duration>
281 {
282 /** The dimensions of duration: s. */
283 public static final SIUnit SI_UNIT = SIUnit.of("s");
284
285 /** second. */
286 public static final Duration.Unit s = new Duration.Unit("s", "second", 1.0, UnitSystem.SI_BASE);
287
288 /** The SI or BASE unit. */
289 public static final Duration.Unit SI = s.generateSiPrefixes(false, false);
290
291 /** picosecond. */
292 public static final Duration.Unit ps = Units.resolve(Duration.Unit.class, "ps");
293
294 /** nanosecond. */
295 public static final Duration.Unit ns = Units.resolve(Duration.Unit.class, "ns");
296
297 /** microsecond. */
298 public static final Duration.Unit mus = Units.resolve(Duration.Unit.class, "mus");
299
300 /** millisecond. */
301 public static final Duration.Unit ms = Units.resolve(Duration.Unit.class, "ms");
302
303 /** minute. */
304 public static final Duration.Unit min = s.deriveUnit("min", "minute", 60.0, UnitSystem.SI_ACCEPTED);
305
306 /** hour. */
307 public static final Duration.Unit h = min.deriveUnit("h", "hour", 60.0, UnitSystem.SI_ACCEPTED);
308
309 /** day. */
310 public static final Duration.Unit day = h.deriveUnit("day", "day", 24.0, UnitSystem.OTHER);
311
312 /** week. */
313 public static final Duration.Unit wk = day.deriveUnit("wk", "week", 7.0, UnitSystem.OTHER);
314
315 /**
316 * Create a new Duration unit.
317 * @param id the id or main abbreviation of the unit
318 * @param name the full name of the unit
319 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
320 * @param unitSystem the unit system such as SI or IMPERIAL
321 */
322 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
323 {
324 super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
325 }
326
327 /**
328 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
329 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
330 * @param displayAbbreviation the display abbreviation of the unit
331 * @param name the full name of the unit
332 * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
333 * @param unitSystem unit system, e.g. SI or Imperial
334 */
335 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
336 final UnitSystem unitSystem)
337 {
338 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
339 }
340
341 @Override
342 public SIUnit siUnit()
343 {
344 return SI_UNIT;
345 }
346
347 @Override
348 public Unit getBaseUnit()
349 {
350 return SI;
351 }
352
353 @Override
354 public Duration ofSi(final double si)
355 {
356 return Duration.ofSi(si);
357 }
358
359 @Override
360 public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
361 final double scaleFactor, final UnitSystem unitSystem)
362 {
363 if (getScale() instanceof LinearScale ls)
364 {
365 return new Duration.Unit(textualAbbreviation, displayAbbreviation, name,
366 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
367 }
368 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
369 }
370
371 }
372 }