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