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