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-2025 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 = "2025-09-06T15:16:28.380798Z")
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 with a unit.
41       * @param value the double value, expressed in the given unit
42       * @param unit 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 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 based on a BASE unit value.
72       * @param value value in BASE units
73       * @return the new scalar with the BASE unit value
74       */
75      public static final Time ofSI(final double value)
76      {
77          return new Time(value, TimeUnit.DEFAULT);
78      }
79  
80      /**
81       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
82       * @param zero the value at a ratio of zero
83       * @param one the value at a ratio of one
84       * @param ratio the ratio between 0 and 1, inclusive
85       * @return a Time at the given ratio between 0 and 1
86       */
87      public static Time interpolate(final Time zero, final Time one, final double ratio)
88      {
89          Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
90                  "ratio for interpolation should be between 0 and 1, but is %f", ratio);
91          return new Time(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
92      }
93  
94      /**
95       * Return the maximum value of two absolute scalars.
96       * @param a1 the first scalar
97       * @param a2 the second scalar
98       * @return the maximum value of two absolute scalars
99       */
100     public static Time max(final Time a1, final Time a2)
101     {
102         return a1.gt(a2) ? a1 : a2;
103     }
104 
105     /**
106      * Return the maximum value of more than two absolute scalars.
107      * @param a1 the first scalar
108      * @param a2 the second scalar
109      * @param an the other scalars
110      * @return the maximum value of more than two absolute scalars
111      */
112     public static Time max(final Time a1, final Time a2, final Time... an)
113     {
114         Time maxa = a1.gt(a2) ? a1 : a2;
115         for (Time a : an)
116         {
117             if (a.gt(maxa))
118             {
119                 maxa = a;
120             }
121         }
122         return maxa;
123     }
124 
125     /**
126      * Return the minimum value of two absolute scalars.
127      * @param a1 the first scalar
128      * @param a2 the second scalar
129      * @return the minimum value of two absolute scalars
130      */
131     public static Time min(final Time a1, final Time a2)
132     {
133         return a1.lt(a2) ? a1 : a2;
134     }
135 
136     /**
137      * Return the minimum value of more than two absolute scalars.
138      * @param a1 the first scalar
139      * @param a2 the second scalar
140      * @param an the other scalars
141      * @return the minimum value of more than two absolute scalars
142      */
143     public static Time min(final Time a1, final Time a2, final Time... an)
144     {
145         Time mina = a1.lt(a2) ? a1 : a2;
146         for (Time a : an)
147         {
148             if (a.lt(mina))
149             {
150                 mina = a;
151             }
152         }
153         return mina;
154     }
155 
156     /**
157      * Returns a Time representation of a textual representation of a value with a unit. The String representation that can be
158      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
159      * but not required, between the value and the unit.
160      * @param text the textual representation to parse into a Time
161      * @return the Scalar representation of the value in its unit
162      * @throws IllegalArgumentException when the text cannot be parsed
163      * @throws NullPointerException when the text argument is null
164      */
165     public static Time valueOf(final String text)
166     {
167         Throw.whenNull(text, "Error parsing Time: text to parse is null");
168         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Time: empty text to parse");
169         try
170         {
171             NumberParser numberParser = new NumberParser().lenient().trailing();
172             double d = numberParser.parseDouble(text);
173             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
174             TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
175             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Time", unitString);
176             return new Time(d, unit);
177         }
178         catch (Exception exception)
179         {
180             throw new IllegalArgumentException(
181                     "Error parsing Time from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
182                     exception);
183         }
184     }
185 
186     /**
187      * Returns a Time based on a value and the textual representation of the unit, which can be localized.
188      * @param value the value to use
189      * @param unitString the textual representation of the unit
190      * @return the Scalar representation of the value in its unit
191      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
192      * @throws NullPointerException when the unitString argument is null
193      */
194     public static Time of(final double value, final String unitString)
195     {
196         Throw.whenNull(unitString, "Error parsing Time: unitString is null");
197         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Time: empty unitString");
198         TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
199         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Time with unit %s", unitString);
200         return new Time(value, unit);
201     }
202 
203 }