View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import javax.annotation.Generated;
6   
7   import org.djunits.Throw;
8   import org.djunits.unit.DurationUnit;
9   import org.djunits.unit.TimeUnit;
10  import org.djunits.value.util.ValueUtil;
11  import org.djunits.value.vfloat.scalar.base.AbstractFloatScalarAbs;
12  
13  /**
14   * Easy access methods for the FloatTime FloatScalar.
15   * <p>
16   * Copyright (c) 2013-2022 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 = "2022-03-14T11:14:15.180987200Z")
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 FloatTimecalar/FloatTime.html#FloatTime">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 FloatTimeoat/scalar/FloatTime.html#FloatTime">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 FloatTimealar/FloatTime.html#FloatTime">FloatTimeloatTime.html#FloatTime">FloatTime interpolate(final FloatTimealar/FloatTime.html#FloatTime">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 FloatTimescalar/FloatTime.html#FloatTime">FloatTimescalar/FloatTime.html#FloatTime">FloatTime max(final FloatTimescalar/FloatTime.html#FloatTime">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 FloatTimescalar/FloatTime.html#FloatTime">FloatTimescalar/FloatTime.html#FloatTime">FloatTime max(final FloatTimescalar/FloatTime.html#FloatTime">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 FloatTimescalar/FloatTime.html#FloatTime">FloatTimescalar/FloatTime.html#FloatTime">FloatTime min(final FloatTimescalar/FloatTime.html#FloatTime">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 FloatTimescalar/FloatTime.html#FloatTime">FloatTimescalar/FloatTime.html#FloatTime">FloatTime min(final FloatTimescalar/FloatTime.html#FloatTime">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 the official abbreviation of the unit. Spaces are allowed, but not
170      * 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         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
181         if (matcher.find())
182         {
183             int index = matcher.end();
184             String unitString = text.substring(index).trim();
185             String valueString = text.substring(0, index).trim();
186             TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
187             if (unit != null)
188             {
189                 float f = Float.parseFloat(valueString);
190                 return new FloatTime(f, unit);
191             }
192         }
193         throw new IllegalArgumentException("Error parsing FloatTime from " + text);
194     }
195 
196     /**
197      * Returns a FloatTime based on a value and the textual representation of the unit.
198      * @param value double; the value to use
199      * @param unitString String; the textual representation of the unit
200      * @return FloatTime; the Scalar representation of the value in its unit
201      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
202      * @throws NullPointerException when the unitString argument is null
203      */
204     public static FloatTime of(final float value, final String unitString)
205     {
206         Throw.whenNull(unitString, "Error parsing FloatTime: unitString is null");
207         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatTime: empty unitString");
208         TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
209         if (unit != null)
210         {
211             return new FloatTime(value, unit);
212         }
213         throw new IllegalArgumentException("Error parsing FloatTime with unit " + unitString);
214     }
215 
216 }