View Javadoc
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.DoubleScalarRel;
20  import org.djunits.value.vdouble.scalar.base.DoubleScalarRelWithAbs;
21  import org.djutils.base.NumberParser;
22  import org.djutils.exceptions.Throw;
23  
24  import jakarta.annotation.Generated;
25  
26  /**
27   * Easy access methods for the Relative Duration DoubleScalar.
28   * <p>
29   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
30   * All rights reserved. <br>
31   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
32   * </p>
33   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
35   */
36  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2025-09-06T15:16:28.380798Z")
37  public class Duration extends DoubleScalarRelWithAbs<TimeUnit, Time, DurationUnit, Duration>
38  {
39      /** */
40      private static final long serialVersionUID = 20150901L;
41  
42      /** Constant with value zero. */
43      public static final Duration ZERO = new Duration(0.0, DurationUnit.SI);
44  
45      /** Constant with value one. */
46      public static final Duration ONE = new Duration(1.0, DurationUnit.SI);
47  
48      /** Constant with value NaN. */
49      @SuppressWarnings("checkstyle:constantname")
50      public static final Duration NaN = new Duration(Double.NaN, DurationUnit.SI);
51  
52      /** Constant with value POSITIVE_INFINITY. */
53      public static final Duration POSITIVE_INFINITY = new Duration(Double.POSITIVE_INFINITY, DurationUnit.SI);
54  
55      /** Constant with value NEGATIVE_INFINITY. */
56      public static final Duration NEGATIVE_INFINITY = new Duration(Double.NEGATIVE_INFINITY, DurationUnit.SI);
57  
58      /** Constant with value MAX_VALUE. */
59      public static final Duration POS_MAXVALUE = new Duration(Double.MAX_VALUE, DurationUnit.SI);
60  
61      /** Constant with value -MAX_VALUE. */
62      public static final Duration NEG_MAXVALUE = new Duration(-Double.MAX_VALUE, DurationUnit.SI);
63  
64      /**
65       * Construct Duration scalar with a unit.
66       * @param value the double value, expressed in the given unit
67       * @param unit unit for the double value
68       */
69      public Duration(final double value, final DurationUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct Duration scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public Duration(final Duration value)
79      {
80          super(value);
81      }
82  
83      @Override
84      public final Duration instantiateRel(final double value, final DurationUnit unit)
85      {
86          return new Duration(value, unit);
87      }
88  
89      @Override
90      public final Time instantiateAbs(final double value, final TimeUnit unit)
91      {
92          return new Time(value, unit);
93      }
94  
95      /**
96       * Construct Duration scalar based on an SI value.
97       * @param value the double value in SI units
98       * @return the new scalar with the SI value
99       */
100     public static final Duration ofSI(final double value)
101     {
102         return new Duration(value, DurationUnit.SI);
103     }
104 
105     /**
106      * Interpolate between two values. Note that the first value does not have to be smaller than the second.
107      * @param zero the value at a ratio of zero
108      * @param one the value at a ratio of one
109      * @param ratio the ratio between 0 and 1, inclusive
110      * @return a Duration at the given ratio between 0 and 1
111      */
112     public static Duration interpolate(final Duration zero, final Duration one, final double ratio)
113     {
114         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
115                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
116         return new Duration(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
117                 zero.getDisplayUnit());
118     }
119 
120     /**
121      * Return the maximum value of two relative scalars.
122      * @param r1 the first scalar
123      * @param r2 the second scalar
124      * @return the maximum value of two relative scalars
125      */
126     public static Duration max(final Duration r1, final Duration r2)
127     {
128         return r1.gt(r2) ? r1 : r2;
129     }
130 
131     /**
132      * Return the maximum value of more than two relative scalars.
133      * @param r1 the first scalar
134      * @param r2 the second scalar
135      * @param rn the other scalars
136      * @return the maximum value of more than two relative scalars
137      */
138     public static Duration max(final Duration r1, final Duration r2, final Duration... rn)
139     {
140         Duration maxr = r1.gt(r2) ? r1 : r2;
141         for (Duration r : rn)
142         {
143             if (r.gt(maxr))
144             {
145                 maxr = r;
146             }
147         }
148         return maxr;
149     }
150 
151     /**
152      * Return the minimum value of two relative scalars.
153      * @param r1 the first scalar
154      * @param r2 the second scalar
155      * @return the minimum value of two relative scalars
156      */
157     public static Duration min(final Duration r1, final Duration r2)
158     {
159         return r1.lt(r2) ? r1 : r2;
160     }
161 
162     /**
163      * Return the minimum value of more than two relative scalars.
164      * @param r1 the first scalar
165      * @param r2 the second scalar
166      * @param rn the other scalars
167      * @return the minimum value of more than two relative scalars
168      */
169     public static Duration min(final Duration r1, final Duration r2, final Duration... rn)
170     {
171         Duration minr = r1.lt(r2) ? r1 : r2;
172         for (Duration r : rn)
173         {
174             if (r.lt(minr))
175             {
176                 minr = r;
177             }
178         }
179         return minr;
180     }
181 
182     /**
183      * Returns a Duration representation of a textual representation of a value with a unit. The String representation that can
184      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
185      * allowed, but not required, between the value and the unit.
186      * @param text the textual representation to parse into a Duration
187      * @return the Scalar representation of the value in its unit
188      * @throws IllegalArgumentException when the text cannot be parsed
189      * @throws NullPointerException when the text argument is null
190      */
191     public static Duration valueOf(final String text)
192     {
193         Throw.whenNull(text, "Error parsing Duration: text to parse is null");
194         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Duration: empty text to parse");
195         try
196         {
197             NumberParser numberParser = new NumberParser().lenient().trailing();
198             double d = numberParser.parseDouble(text);
199             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
200             DurationUnit unit = DurationUnit.BASE.getUnitByAbbreviation(unitString);
201             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Duration", unitString);
202             return new Duration(d, unit);
203         }
204         catch (Exception exception)
205         {
206             throw new IllegalArgumentException(
207                     "Error parsing Duration from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
208                     exception);
209         }
210     }
211 
212     /**
213      * Returns a Duration based on a value and the textual representation of the unit, which can be localized.
214      * @param value the value to use
215      * @param unitString the textual representation of the unit
216      * @return the Scalar representation of the value in its unit
217      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
218      * @throws NullPointerException when the unitString argument is null
219      */
220     public static Duration of(final double value, final String unitString)
221     {
222         Throw.whenNull(unitString, "Error parsing Duration: unitString is null");
223         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Duration: empty unitString");
224         DurationUnit unit = DurationUnit.BASE.getUnitByAbbreviation(unitString);
225         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Duration with unit %s", unitString);
226         return new Duration(value, unit);
227     }
228 
229     /**
230      * Calculate the division of Duration and Duration, which results in a Dimensionless scalar.
231      * @param v scalar
232      * @return scalar as a division of Duration and Duration
233      */
234     public final Dimensionless divide(final Duration v)
235     {
236         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
237     }
238 
239     /**
240      * Calculate the multiplication of Duration and Frequency, which results in a Dimensionless scalar.
241      * @param v scalar
242      * @return scalar as a multiplication of Duration and Frequency
243      */
244     public final Dimensionless times(final Frequency v)
245     {
246         return new Dimensionless(this.si * v.si, DimensionlessUnit.SI);
247     }
248 
249     /**
250      * Calculate the multiplication of Duration and ElectricalCurrent, which results in a ElectricalCharge scalar.
251      * @param v scalar
252      * @return scalar as a multiplication of Duration and ElectricalCurrent
253      */
254     public final ElectricalCharge times(final ElectricalCurrent v)
255     {
256         return new ElectricalCharge(this.si * v.si, ElectricalChargeUnit.SI);
257     }
258 
259     /**
260      * Calculate the multiplication of Duration and FlowMass, which results in a Mass scalar.
261      * @param v scalar
262      * @return scalar as a multiplication of Duration and FlowMass
263      */
264     public final Mass times(final FlowMass v)
265     {
266         return new Mass(this.si * v.si, MassUnit.SI);
267     }
268 
269     /**
270      * Calculate the multiplication of Duration and FlowVolume, which results in a Volume scalar.
271      * @param v scalar
272      * @return scalar as a multiplication of Duration and FlowVolume
273      */
274     public final Volume times(final FlowVolume v)
275     {
276         return new Volume(this.si * v.si, VolumeUnit.SI);
277     }
278 
279     /**
280      * Calculate the multiplication of Duration and Acceleration, which results in a Speed scalar.
281      * @param v scalar
282      * @return scalar as a multiplication of Duration and Acceleration
283      */
284     public final Speed times(final Acceleration v)
285     {
286         return new Speed(this.si * v.si, SpeedUnit.SI);
287     }
288 
289     /**
290      * Calculate the multiplication of Duration and Power, which results in a Energy scalar.
291      * @param v scalar
292      * @return scalar as a multiplication of Duration and Power
293      */
294     public final Energy times(final Power v)
295     {
296         return new Energy(this.si * v.si, EnergyUnit.SI);
297     }
298 
299     /**
300      * Calculate the multiplication of Duration and Speed, which results in a Length scalar.
301      * @param v scalar
302      * @return scalar as a multiplication of Duration and Speed
303      */
304     public final Length times(final Speed v)
305     {
306         return new Length(this.si * v.si, LengthUnit.SI);
307     }
308 
309     /**
310      * Calculate the multiplication of Duration and ElectricalPotential, which results in a MagneticFlux scalar.
311      * @param v scalar
312      * @return scalar as a multiplication of Duration and ElectricalPotential
313      */
314     public final MagneticFlux times(final ElectricalPotential v)
315     {
316         return new MagneticFlux(this.si * v.si, MagneticFluxUnit.SI);
317     }
318 
319     /**
320      * Calculate the multiplication of Duration and ElectricalResistance, which results in a ElectricalInductance scalar.
321      * @param v scalar
322      * @return scalar as a multiplication of Duration and ElectricalResistance
323      */
324     public final ElectricalInductance times(final ElectricalResistance v)
325     {
326         return new ElectricalInductance(this.si * v.si, ElectricalInductanceUnit.SI);
327     }
328 
329     /**
330      * Calculate the multiplication of Duration and ElectricalConductance, which results in a ElectricalCapacitance scalar.
331      * @param v scalar
332      * @return scalar as a multiplication of Duration and ElectricalConductance
333      */
334     public final ElectricalCapacitance times(final ElectricalConductance v)
335     {
336         return new ElectricalCapacitance(this.si * v.si, ElectricalCapacitanceUnit.SI);
337     }
338 
339     /**
340      * Calculate the multiplication of Duration and AngularVelocity, which results in a Angle scalar.
341      * @param v scalar
342      * @return scalar as a multiplication of Duration and AngularVelocity
343      */
344     public final Angle times(final AngularVelocity v)
345     {
346         return new Angle(this.si * v.si, AngleUnit.SI);
347     }
348 
349     /**
350      * Calculate the multiplication of Duration and AngularAcceleration, which results in a AngularVelocity scalar.
351      * @param v scalar
352      * @return scalar as a multiplication of Duration and AngularAcceleration
353      */
354     public final AngularVelocity times(final AngularAcceleration v)
355     {
356         return new AngularVelocity(this.si * v.si, AngularVelocityUnit.SI);
357     }
358 
359     @Override
360     public Frequency reciprocal()
361     {
362         return Frequency.ofSI(1.0 / this.si);
363     }
364 
365     /**
366      * Multiply two scalars that result in a scalar of type Duration.
367      * @param scalar1 the first scalar
368      * @param scalar2 the second scalar
369      * @return the multiplication of both scalars as an instance of Duration
370      */
371     public static Duration multiply(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
372     {
373         Throw.whenNull(scalar1, "scalar1 cannot be null");
374         Throw.whenNull(scalar2, "scalar2 cannot be null");
375         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
376                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(DurationUnit.BASE.getSiDimensions()),
377                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type Duration",
378                 scalar1.toDisplayString(), scalar2.toDisplayString());
379         return new Duration(scalar1.si * scalar2.si, DurationUnit.SI);
380     }
381 
382     /**
383      * Divide two scalars that result in a scalar of type Duration.
384      * @param scalar1 the first scalar
385      * @param scalar2 the second scalar
386      * @return the division of scalar1 by scalar2 as an instance of Duration
387      */
388     public static Duration divide(final DoubleScalarRel<?, ?> scalar1, final DoubleScalarRel<?, ?> scalar2)
389     {
390         Throw.whenNull(scalar1, "scalar1 cannot be null");
391         Throw.whenNull(scalar2, "scalar2 cannot be null");
392         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
393                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(DurationUnit.BASE.getSiDimensions()),
394                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type Duration",
395                 scalar1.toDisplayString(), scalar2.toDisplayString());
396         return new Duration(scalar1.si / scalar2.si, DurationUnit.SI);
397     }
398 
399 }