View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import org.djunits.unit.DurationUnit;
4   import org.djunits.unit.TimeUnit;
5   
6   /**
7    * Easy access methods for the Time FloatScalar. Instead of:
8    * 
9    * <pre>
10   * FloatScalar.Abs&lt;TimeUnit&gt; value = new FloatScalar.Abs&lt;TimeUnit&gt;(100.0, TimeUnit.SI);
11   * </pre>
12   * 
13   * we can now write:
14   * 
15   * <pre>
16   * FloatTime value = new FloatTime(100.0, TimeUnit.SI);
17   * </pre>
18   * 
19   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
20   * used are compatible.
21   * <p>
22   * Note that when the offset of a stored absolute Time becomes large, precision of a float might not be enough for the required
23   * resolution of a Time. A float has around 7 significant digits (23 bit mantissa). This means that when we need to have a float
24   * 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
25   * Epoch values that are in the order of magnitude of 2E12 ms! So feeding System.TimeInMillis() to a FloatTime with
26   * TimeUnit.BASE as its unit is not having the required precision. At best, a FloatTime can store TimeUnit.BASE or
27   * TimeUnit.EPOCH values with real calendar values with a precision of several minutes.
28   * <p>
29   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
30   * All rights reserved. <br>
31   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
32   * <p>
33   * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
34   * initial version Sep 1, 2015 <br>
35   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
36   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
37   */
38  public class FloatTime extends AbstractFloatScalarAbs<TimeUnit, FloatTime, DurationUnit, FloatDuration>
39  {
40      /** */
41      private static final long serialVersionUID = 20150901L;
42  
43      /** constant with value zero. */
44      public static final FloatTime ZERO = new FloatTime(0.0f, TimeUnit.BASE);
45  
46      /**
47       * Construct FloatTime scalar.
48       * @param value float value
49       * @param unit unit for the float value
50       */
51      public FloatTime(final float value, final TimeUnit unit)
52      {
53          super(value, unit);
54      }
55  
56      /**
57       * Construct FloatTime scalar using a double value.
58       * @param value double value
59       * @param unit unit for the resulting float value
60       */
61      public FloatTime(final double value, final TimeUnit unit)
62      {
63          super((float) value, unit);
64      }
65  
66      /**
67       * Construct FloatTime scalar.
68       * @param value Scalar from which to construct this instance
69       */
70      public FloatTime(final FloatTime value)
71      {
72          super(value);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final FloatTime instantiateAbs(final float value, final TimeUnit unit)
78      {
79          return new FloatTime(value, unit);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final FloatDuration instantiateRel(final float value, final DurationUnit unit)
85      {
86          return new FloatDuration(value, unit);
87      }
88  
89      /**
90       * Construct FloatTime scalar.
91       * @param value float value in BASE units
92       * @return the new scalar with the BASE value
93       */
94      public static final FloatTime createSI(final float value)
95      {
96          return new FloatTime(value, TimeUnit.BASE);
97      }
98  
99      /**
100      * Interpolate between two values.
101      * @param zero the low value
102      * @param one the high value
103      * @param ratio the ratio between 0 and 1, inclusive
104      * @return a Scalar at the ratio between
105      */
106     public static FloatTime interpolate(final FloatTime zero, final FloatTime one, final float ratio)
107     {
108         return new FloatTime(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
109     }
110 
111     /**
112      * Return the maximum value of two absolute scalars.
113      * @param a1 the first scalar
114      * @param a2 the second scalar
115      * @return the maximum value of two absolute scalars
116      */
117     public static FloatTime max(final FloatTime a1, final FloatTime a2)
118     {
119         return (a1.gt(a2)) ? a1 : a2;
120     }
121 
122     /**
123      * Return the maximum value of more than two absolute scalars.
124      * @param a1 the first scalar
125      * @param a2 the second scalar
126      * @param an the other scalars
127      * @return the maximum value of more than two absolute scalars
128      */
129     public static FloatTime max(final FloatTime a1, final FloatTime a2, final FloatTime... an)
130     {
131         FloatTime maxa = (a1.gt(a2)) ? a1 : a2;
132         for (FloatTime a : an)
133         {
134             if (a.gt(maxa))
135             {
136                 maxa = a;
137             }
138         }
139         return maxa;
140     }
141 
142     /**
143      * Return the minimum value of two absolute scalars.
144      * @param a1 the first scalar
145      * @param a2 the second scalar
146      * @return the minimum value of two absolute scalars
147      */
148     public static FloatTime min(final FloatTime a1, final FloatTime a2)
149     {
150         return (a1.lt(a2)) ? a1 : a2;
151     }
152 
153     /**
154      * Return the minimum value of more than two absolute scalars.
155      * @param a1 the first scalar
156      * @param a2 the second scalar
157      * @param an the other scalars
158      * @return the minimum value of more than two absolute scalars
159      */
160     public static FloatTime min(final FloatTime a1, final FloatTime a2, final FloatTime... an)
161     {
162         FloatTime mina = (a1.lt(a2)) ? a1 : a2;
163         for (FloatTime a : an)
164         {
165             if (a.lt(mina))
166             {
167                 mina = a;
168             }
169         }
170         return mina;
171     }
172 
173 }