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