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