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