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-2019 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://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
22   */
23  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2019-10-18T12:12:25.568Z")
24  public class FloatTime extends AbstractFloatScalarAbs<TimeUnit, FloatTime, DurationUnit, FloatDuration>
25  {
26      /** */
27      private static final long serialVersionUID = 20150901L;
28  
29      /** Constant with value zero. */
30      public static final FloatTimecalar/FloatTime.html#FloatTime">FloatTime ZERO = new FloatTime(0.0f, TimeUnit.DEFAULT);
31  
32      /**
33       * Construct FloatTime scalar.
34       * @param value float; the float value
35       * @param unit TimeUnit; unit for the float value
36       */
37      public FloatTime(final float value, final TimeUnit unit)
38      {
39          super(value, unit);
40      }
41  
42      /**
43       * Construct FloatTime scalar using a double value.
44       * @param value double; the double value
45       * @param unit TimeUnit; unit for the resulting float value
46       */
47      public FloatTime(final double value, final TimeUnit unit)
48      {
49          super((float) value, unit);
50      }
51  
52      /**
53       * Construct FloatTime scalar.
54       * @param value FloatTime; Scalar from which to construct this instance
55       */
56      public FloatTimeoat/scalar/FloatTime.html#FloatTime">FloatTime(final FloatTime value)
57      {
58          super(value);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final FloatTime instantiateAbs(final float value, final TimeUnit unit)
64      {
65          return new FloatTime(value, unit);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final FloatDuration instantiateRel(final float value, final DurationUnit unit)
71      {
72          return new FloatDuration(value, unit);
73      }
74  
75      /**
76       * Construct FloatTime scalar.
77       * @param value float; the float value in BASE units
78       * @return FloatTime; the new scalar with the BASE value
79       */
80      public static final FloatTime instantiateSI(final float value)
81      {
82          return new FloatTime(value, TimeUnit.DEFAULT);
83      }
84  
85      /**
86       * Interpolate between two values.
87       * @param zero FloatTime; the low value
88       * @param one FloatTime; the high value
89       * @param ratio float; the ratio between 0 and 1, inclusive
90       * @return FloatTime; a Scalar at the ratio between
91       */
92      public static FloatTimealar/FloatTime.html#FloatTime">FloatTimeloatTime.html#FloatTime">FloatTime interpolate(final FloatTimealar/FloatTime.html#FloatTime">FloatTime zero, final FloatTime one, final float ratio)
93      {
94          return new FloatTime(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
95                  zero.getDisplayUnit());
96      }
97  
98      /**
99       * Return the maximum value of two absolute scalars.
100      * @param a1 FloatTime; the first scalar
101      * @param a2 FloatTime; the second scalar
102      * @return FloatTime; the maximum value of two absolute scalars
103      */
104     public static FloatTimescalar/FloatTime.html#FloatTime">FloatTimescalar/FloatTime.html#FloatTime">FloatTime max(final FloatTimescalar/FloatTime.html#FloatTime">FloatTime a1, final FloatTime a2)
105     {
106         return (a1.gt(a2)) ? a1 : a2;
107     }
108 
109     /**
110      * Return the maximum value of more than two absolute scalars.
111      * @param a1 FloatTime; the first scalar
112      * @param a2 FloatTime; the second scalar
113      * @param an FloatTime...; the other scalars
114      * @return FloatTime; the maximum value of more than two absolute scalars
115      */
116     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)
117     {
118         FloatTime maxa = (a1.gt(a2)) ? a1 : a2;
119         for (FloatTime a : an)
120         {
121             if (a.gt(maxa))
122             {
123                 maxa = a;
124             }
125         }
126         return maxa;
127     }
128 
129     /**
130      * Return the minimum value of two absolute scalars.
131      * @param a1 FloatTime; the first scalar
132      * @param a2 FloatTime; the second scalar
133      * @return FloatTime; the minimum value of two absolute scalars
134      */
135     public static FloatTimescalar/FloatTime.html#FloatTime">FloatTimescalar/FloatTime.html#FloatTime">FloatTime min(final FloatTimescalar/FloatTime.html#FloatTime">FloatTime a1, final FloatTime a2)
136     {
137         return (a1.lt(a2)) ? a1 : a2;
138     }
139 
140     /**
141      * Return the minimum value of more than two absolute scalars.
142      * @param a1 FloatTime; the first scalar
143      * @param a2 FloatTime; the second scalar
144      * @param an FloatTime...; the other scalars
145      * @return FloatTime; the minimum value of more than two absolute scalars
146      */
147     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)
148     {
149         FloatTime mina = (a1.lt(a2)) ? a1 : a2;
150         for (FloatTime a : an)
151         {
152             if (a.lt(mina))
153             {
154                 mina = a;
155             }
156         }
157         return mina;
158     }
159 
160     /**
161      * Returns a FloatTime representation of a textual representation of a value with a unit. The String representation that can
162      * be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
163      * required, between the value and the unit.
164      * @param text String; the textual representation to parse into a FloatTime
165      * @return FloatTime; the Scalar representation of the value in its unit
166      * @throws IllegalArgumentException when the text cannot be parsed
167      * @throws NullPointerException when the text argument is null
168      */
169     public static FloatTime valueOf(final String text)
170     {
171         Throw.whenNull(text, "Error parsing FloatTime: text to parse is null");
172         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatTime: empty text to parse");
173         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
174         if (matcher.find())
175         {
176             int index = matcher.end();
177             String unitString = text.substring(index).trim();
178             String valueString = text.substring(0, index).trim();
179             TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
180             if (unit != null)
181             {
182                 float f = Float.parseFloat(valueString);
183                 return new FloatTime(f, unit);
184             }
185         }
186         throw new IllegalArgumentException("Error parsing FloatTime from " + text);
187     }
188 
189     /**
190      * Returns a FloatTime based on a value and the textual representation of the unit.
191      * @param value double; the value to use
192      * @param unitString String; the textual representation of the unit
193      * @return FloatTime; the Scalar representation of the value in its unit
194      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
195      * @throws NullPointerException when the unitString argument is null
196      */
197     public static FloatTime of(final float value, final String unitString)
198     {
199         Throw.whenNull(unitString, "Error parsing FloatTime: unitString is null");
200         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatTime: empty unitString");
201         TimeUnit unit = TimeUnit.BASE.getUnitByAbbreviation(unitString);
202         if (unit != null)
203         {
204             return new FloatTime(value, unit);
205         }
206         throw new IllegalArgumentException("Error parsing FloatTime with unit " + unitString);
207     }
208 
209 }