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