View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.DurationUnit;
6   import org.djunits.unit.TimeUnit;
7   import org.djunits.unit.Unit;
8   
9   /**
10   * Easy access methods for the Time FloatScalar. Instead of:
11   * 
12   * <pre>
13   * FloatScalar.Abs&lt;TimeUnit&gt; value = new FloatScalar.Abs&lt;TimeUnit&gt;(100.0, TimeUnit.SI);
14   * </pre>
15   * 
16   * we can now write:
17   * 
18   * <pre>
19   * FloatTime value = new FloatTime(100.0, TimeUnit.SI);
20   * </pre>
21   * 
22   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
23   * used are compatible.
24   * <p>
25   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
26   * All rights reserved. <br>
27   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
28   * <p>
29   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
30   * initial version Sep 1, 2015 <br>
31   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33   */
34  public class FloatTime extends AbstractFloatScalarAbs<TimeUnit, FloatTime, DurationUnit, FloatDuration>
35  {
36      /** */
37      private static final long serialVersionUID = 20150901L;
38  
39      /** constant with value zero. */
40      public static final FloatTime ZERO = new FloatTime(0.0f, TimeUnit.BASE);
41  
42      /**
43       * Construct FloatTime scalar.
44       * @param value float value
45       * @param unit unit for the float value
46       */
47      public FloatTime(final float value, final TimeUnit unit)
48      {
49          super(value, unit);
50      }
51  
52      /**
53       * Construct FloatTime scalar using a double value.
54       * @param value double value
55       * @param unit unit for the resulting float value
56       */
57      public FloatTime(final double value, final TimeUnit unit)
58      {
59          super((float) value, unit);
60      }
61  
62      /**
63       * Construct FloatTime scalar.
64       * @param value Scalar from which to construct this instance
65       */
66      public FloatTime(final FloatTime value)
67      {
68          super(value);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public final FloatTime instantiateAbs(final float value, final TimeUnit unit)
74      {
75          return new FloatTime(value, unit);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final FloatDuration instantiateRel(final float value, final DurationUnit unit)
81      {
82          return new FloatDuration(value, unit);
83      }
84  
85      /**
86       * Construct FloatTime scalar.
87       * @param value float value in BASE units
88       * @return the new scalar with the BASE value
89       */
90      public static final FloatTime createSI(final float value)
91      {
92          return new FloatTime(value, TimeUnit.BASE);
93      }
94  
95      /**
96       * Interpolate between two values.
97       * @param zero the low value
98       * @param one the high value
99       * @param ratio the ratio between 0 and 1, inclusive
100      * @return a Scalar at the ratio between
101      */
102     public static FloatTime interpolate(final FloatTime zero, final FloatTime one, final float ratio)
103     {
104         return new FloatTime(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
105     }
106 
107     /**
108      * Return the maximum value of two absolute scalars.
109      * @param a1 the first scalar
110      * @param a2 the second scalar
111      * @return the maximum value of two absolute scalars
112      */
113     public static FloatTime max(final FloatTime a1, final FloatTime a2)
114     {
115         return (a1.gt(a2)) ? a1 : a2;
116     }
117 
118     /**
119      * Return the maximum value of more than two absolute scalars.
120      * @param a1 the first scalar
121      * @param a2 the second scalar
122      * @param an the other scalars
123      * @return the maximum value of more than two absolute scalars
124      */
125     public static FloatTime max(final FloatTime a1, final FloatTime a2, final FloatTime... an)
126     {
127         FloatTime maxa = (a1.gt(a2)) ? a1 : a2;
128         for (FloatTime a : an)
129         {
130             if (a.gt(maxa))
131             {
132                 maxa = a;
133             }
134         }
135         return maxa;
136     }
137 
138     /**
139      * Return the minimum value of two absolute scalars.
140      * @param a1 the first scalar
141      * @param a2 the second scalar
142      * @return the minimum value of two absolute scalars
143      */
144     public static FloatTime min(final FloatTime a1, final FloatTime a2)
145     {
146         return (a1.lt(a2)) ? a1 : a2;
147     }
148 
149     /**
150      * Return the minimum value of more than two absolute scalars.
151      * @param a1 the first scalar
152      * @param a2 the second scalar
153      * @param an the other scalars
154      * @return the minimum value of more than two absolute scalars
155      */
156     public static FloatTime min(final FloatTime a1, final FloatTime a2, final FloatTime... an)
157     {
158         FloatTime mina = (a1.lt(a2)) ? a1 : a2;
159         for (FloatTime a : an)
160         {
161             if (a.lt(mina))
162             {
163                 mina = a;
164             }
165         }
166         return mina;
167     }
168 
169     /**
170      * Returns a FloatTime representation of a textual representation of a value with a unit. The String representation that can
171      * be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
172      * necessary, between the value and the unit.
173      * @param text String; the textual representation to parse into a FloatTime
174      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
175      * @throws IllegalArgumentException when the text cannot be parsed
176      */
177     public static FloatTime valueOf(final String text) throws IllegalArgumentException
178     {
179         if (text == null || text.length() == 0)
180         {
181             throw new IllegalArgumentException("Error parsing FloatTime -- null or empty argument");
182         }
183         Matcher matcher = NUMBER_PATTERN.matcher(text);
184         if (matcher.find())
185         {
186             int index = matcher.end();
187             try
188             {
189                 String unitString = text.substring(index).trim();
190                 String valueString = text.substring(0, index).trim();
191                 for (TimeUnit unit : Unit.getUnits(TimeUnit.class))
192                 {
193                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
194                     {
195                         float f = Float.parseFloat(valueString);
196                         return new FloatTime(f, unit);
197                     }
198                 }
199             }
200             catch (Exception exception)
201             {
202                 throw new IllegalArgumentException("Error parsing FloatTime from " + text, exception);
203             }
204         }
205         throw new IllegalArgumentException("Error parsing FloatTime from " + text);
206     }
207 
208 }