View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.DurationUnit;
6   import org.djunits.unit.TimeUnit;
7   import org.djunits.value.vdouble.scalar.base.DoubleScalarAbs;
8   import org.djutils.base.NumberParser;
9   import org.djutils.exceptions.Throw;
10  
11  import jakarta.annotation.Generated;
12  
13  /**
14   * Easy access methods for the Absolute Time DoubleScalar.
15   * <p>
16   * Note that when the offset of a stored absolute Time becomes large, precision of a double might not be enough for the required
17   * resolution of a Time. A double has around 16 significant digits (52 bit mantissa). This means that when we need to have a
18   * double Time with TimeUnit.BASE as its unit, the largest value where the ms precision is reached is 2^51 = 2.3E15, which is
19   * around 71000 years. This is sufficient to store a date in the 21st Century with a BASE or an Epoch offset precise to a
20   * microsecond.
21   * </p>
22   * <p>
23   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
24   * All rights reserved. <br>
25   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
26   * </p>
27   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
29   */
30  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2023-07-23T14:06:38.224104100Z")
31  public class Time extends DoubleScalarAbs<TimeUnit, Time, DurationUnit, Duration>
32  {
33      /** */
34      private static final long serialVersionUID = 20150901L;
35  
36      /** Constant with value zero. */
37      public static final Time ZERO = new Time(0.0, TimeUnit.DEFAULT);
38  
39      /**
40       * Construct Time scalar.
41       * @param value double; value
42       * @param unit TimeUnit; unit for the double value
43       */
44      public Time(final double value, final TimeUnit unit)
45      {
46          super(value, unit);
47      }
48  
49      /**
50       * Construct Time scalar.
51       * @param value Time; Scalar from which to construct this instance
52       */
53      public Time(final Time value)
54      {
55          super(value);
56      }
57  
58      @Override
59      public final Time instantiateAbs(final double value, final TimeUnit unit)
60      {
61          return new Time(value, unit);
62      }
63  
64      @Override
65      public final Duration instantiateRel(final double value, final DurationUnit unit)
66      {
67          return new Duration(value, unit);
68      }
69  
70      /**
71       * Construct Time scalar.
72       * @param value double; value in SI units
73       * @return Time; the new scalar with the SI value
74       */
75      public static final Time instantiateSI(final double value)
76      {
77          return new Time(value, TimeUnit.DEFAULT);
78      }
79  
80      /**
81       * Interpolate between two values.
82       * @param zero Time; the low value
83       * @param one Time; the high value
84       * @param ratio double; the ratio between 0 and 1, inclusive
85       * @return Time; a Scalar at the ratio between
86       */
87      public static Time interpolate(final Time zero, final Time one, final double ratio)
88      {
89          return new Time(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
90      }
91  
92      /**
93       * Return the maximum value of two absolute scalars.
94       * @param a1 Time; the first scalar
95       * @param a2 Time; the second scalar
96       * @return Time; the maximum value of two absolute scalars
97       */
98      public static Time max(final Time a1, final Time a2)
99      {
100         return a1.gt(a2) ? a1 : a2;
101     }
102 
103     /**
104      * Return the maximum value of more than two absolute scalars.
105      * @param a1 Time; the first scalar
106      * @param a2 Time; the second scalar
107      * @param an Time...; the other scalars
108      * @return the maximum value of more than two absolute scalars
109      */
110     public static Time max(final Time a1, final Time a2, final Time... an)
111     {
112         Time maxa = a1.gt(a2) ? a1 : a2;
113         for (Time a : an)
114         {
115             if (a.gt(maxa))
116             {
117                 maxa = a;
118             }
119         }
120         return maxa;
121     }
122 
123     /**
124      * Return the minimum value of two absolute scalars.
125      * @param a1 Time; the first scalar
126      * @param a2 Time; the second scalar
127      * @return the minimum value of two absolute scalars
128      */
129     public static Time min(final Time a1, final Time a2)
130     {
131         return a1.lt(a2) ? a1 : a2;
132     }
133 
134     /**
135      * Return the minimum value of more than two absolute scalars.
136      * @param a1 Time; the first scalar
137      * @param a2 Time; the second scalar
138      * @param an Time...; the other scalars
139      * @return the minimum value of more than two absolute scalars
140      */
141     public static Time min(final Time a1, final Time a2, final Time... an)
142     {
143         Time mina = a1.lt(a2) ? a1 : a2;
144         for (Time a : an)
145         {
146             if (a.lt(mina))
147             {
148                 mina = a;
149             }
150         }
151         return mina;
152     }
153 
154     /**
155      * Returns a Time representation of a textual representation of a value with a unit. The String representation that can be
156      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
157      * but not required, between the value and the unit.
158      * @param text String; the textual representation to parse into a Time
159      * @return Time; the Scalar representation of the value in its unit
160      * @throws IllegalArgumentException when the text cannot be parsed
161      * @throws NullPointerException when the text argument is null
162      */
163     public static Time valueOf(final String text)
164     {
165         Throw.whenNull(text, "Error parsing Time: text to parse is null");
166         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Time: empty text to parse");
167         try
168         {
169             NumberParser numberParser = new NumberParser().lenient().trailing();
170             double d = numberParser.parseDouble(text);
171             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
172             TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
173             if (unit == null)
174                 throw new IllegalArgumentException("Unit " + unitString + " not found");
175             return new Time(d, unit);
176         }
177         catch (Exception exception)
178         {
179             throw new IllegalArgumentException(
180                     "Error parsing Time from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
181                     exception);
182         }
183     }
184 
185     /**
186      * Returns a Time based on a value and the textual representation of the unit, which can be localized.
187      * @param value double; the value to use
188      * @param unitString String; the textual representation of the unit
189      * @return Time; the Scalar representation of the value in its unit
190      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
191      * @throws NullPointerException when the unitString argument is null
192      */
193     public static Time of(final double value, final String unitString)
194     {
195         Throw.whenNull(unitString, "Error parsing Time: unitString is null");
196         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Time: empty unitString");
197         TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
198         if (unit != null)
199         {
200             return new Time(value, unit);
201         }
202         throw new IllegalArgumentException("Error parsing Time with unit " + unitString);
203     }
204 
205 }