View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.DimensionlessUnit;
6   import org.djunits.unit.DurationUnit;
7   import org.djunits.unit.ElectricalChargeUnit;
8   import org.djunits.unit.EnergyUnit;
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.unit.MassUnit;
11  import org.djunits.unit.MoneyUnit;
12  import org.djunits.unit.SpeedUnit;
13  import org.djunits.unit.TimeUnit;
14  import org.djunits.unit.Unit;
15  import org.djunits.unit.VolumeUnit;
16  
17  /**
18   * Easy access methods for the %Type% FloatScalar. Instead of:
19   * 
20   * <pre>
21   * FloatScalar.Rel&lt;DurationUnit&gt; value = new FloatScalar.Rel&lt;DurationUnit&gt;(100.0, DurationUnit.SI);
22   * </pre>
23   * 
24   * we can now write:
25   * 
26   * <pre>
27   * FloatDuration value = new FloatDuration(100.0, DurationUnit.SI);
28   * </pre>
29   * 
30   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
31   * used are compatible.
32   * <p>
33   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
34   * All rights reserved. <br>
35   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
36   * <p>
37   * $LastChangedDate: 2015-12-22 04:32:39 +0100 (Tue, 22 Dec 2015) $, @version $Revision: 180 $, by $Author: averbraeck $,
38   * initial version Sep 1, 2015 <br>
39   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
40   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
41   */
42  public class FloatDuration extends AbstractFloatScalarRel<DurationUnit, FloatDuration>
43  {
44      /** */
45      private static final long serialVersionUID = 20150901L;
46  
47      /** constant with value zero. */
48      public static final FloatDuration ZERO = new FloatDuration(0.0f, DurationUnit.SI);
49  
50      /** constant with value NaN. */
51      @SuppressWarnings("checkstyle:constantname")
52      public static final FloatDuration NaN = new FloatDuration(Float.NaN, DurationUnit.SI);
53  
54      /** constant with value POSITIVE_INFINITY. */
55      public static final FloatDuration POSITIVE_INFINITY = new FloatDuration(Float.POSITIVE_INFINITY, DurationUnit.SI);
56  
57      /** constant with value NEGATIVE_INFINITY. */
58      public static final FloatDuration NEGATIVE_INFINITY = new FloatDuration(Float.NEGATIVE_INFINITY, DurationUnit.SI);
59  
60      /** constant with value MAX_VALUE. */
61      public static final FloatDuration POS_MAXVALUE = new FloatDuration(Float.MAX_VALUE, DurationUnit.SI);
62  
63      /** constant with value -MAX_VALUE. */
64      public static final FloatDuration NEG_MAXVALUE = new FloatDuration(-Float.MAX_VALUE, DurationUnit.SI);
65  
66      /**
67       * Construct FloatDuration scalar.
68       * @param value float value
69       * @param unit unit for the float value
70       */
71      public FloatDuration(final float value, final DurationUnit unit)
72      {
73          super(value, unit);
74      }
75  
76      /**
77       * Construct FloatDuration scalar.
78       * @param value Scalar from which to construct this instance
79       */
80      public FloatDuration(final FloatDuration value)
81      {
82          super(value);
83      }
84  
85      /**
86       * Construct FloatDuration scalar using a double value.
87       * @param value double value
88       * @param unit unit for the resulting float value
89       */
90      public FloatDuration(final double value, final DurationUnit unit)
91      {
92          super((float) value, unit);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public final FloatDuration instantiateRel(final float value, final DurationUnit unit)
98      {
99          return new FloatDuration(value, unit);
100     }
101 
102     /**
103      * Construct FloatDuration scalar.
104      * @param value float value in SI units
105      * @return the new scalar with the SI value
106      */
107     public static final FloatDuration createSI(final float value)
108     {
109         return new FloatDuration(value, DurationUnit.SI);
110     }
111 
112     /**
113      * Construct a new Absolute Immutable FloatScalar of the right type. Each extending class must implement this method.
114      * @param value the float value
115      * @param unit the unit
116      * @return A a new absolute instance of the FloatScalar of the right type
117      */
118     public final FloatTime instantiateAbs(final float value, final TimeUnit unit)
119     {
120         return new FloatTime(value, unit);
121     }
122 
123     /**
124      * Interpolate between two values.
125      * @param zero the low value
126      * @param one the high value
127      * @param ratio the ratio between 0 and 1, inclusive
128      * @return a Scalar at the ratio between
129      */
130     public static FloatDuration interpolate(final FloatDuration zero, final FloatDuration one, final float ratio)
131     {
132         return new FloatDuration(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
133     }
134 
135     /**
136      * Relative scalar plus Absolute scalar = Absolute scalar.
137      * @param v the value to add
138      * @return sum of this value and v as a new object
139      */
140     public final FloatTime plus(final FloatTime v)
141     {
142         TimeUnit targetUnit = v.getUnit();
143         return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
144     }
145 
146     /**
147      * Return the maximum value of two relative scalars.
148      * @param r1 the first scalar
149      * @param r2 the second scalar
150      * @return the maximum value of two relative scalars
151      */
152     public static FloatDuration max(final FloatDuration r1, final FloatDuration r2)
153     {
154         return (r1.gt(r2)) ? r1 : r2;
155     }
156 
157     /**
158      * Return the maximum value of more than two relative scalars.
159      * @param r1 the first scalar
160      * @param r2 the second scalar
161      * @param rn the other scalars
162      * @return the maximum value of more than two relative scalars
163      */
164     public static FloatDuration max(final FloatDuration r1, final FloatDuration r2, final FloatDuration... rn)
165     {
166         FloatDuration maxr = (r1.gt(r2)) ? r1 : r2;
167         for (FloatDuration r : rn)
168         {
169             if (r.gt(maxr))
170             {
171                 maxr = r;
172             }
173         }
174         return maxr;
175     }
176 
177     /**
178      * Return the minimum value of two relative scalars.
179      * @param r1 the first scalar
180      * @param r2 the second scalar
181      * @return the minimum value of two relative scalars
182      */
183     public static FloatDuration min(final FloatDuration r1, final FloatDuration r2)
184     {
185         return (r1.lt(r2)) ? r1 : r2;
186     }
187 
188     /**
189      * Return the minimum value of more than two relative scalars.
190      * @param r1 the first scalar
191      * @param r2 the second scalar
192      * @param rn the other scalars
193      * @return the minimum value of more than two relative scalars
194      */
195     public static FloatDuration min(final FloatDuration r1, final FloatDuration r2, final FloatDuration... rn)
196     {
197         FloatDuration minr = (r1.lt(r2)) ? r1 : r2;
198         for (FloatDuration r : rn)
199         {
200             if (r.lt(minr))
201             {
202                 minr = r;
203             }
204         }
205         return minr;
206     }
207 
208     /**
209      * Returns a FloatDuration representation of a textual representation of a value with a unit. The String representation that
210      * can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but
211      * not necessary, between the value and the unit.
212      * @param text String; the textual representation to parse into a FloatDuration
213      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
214      * @throws IllegalArgumentException when the text cannot be parsed
215      */
216     public static FloatDuration valueOf(final String text) throws IllegalArgumentException
217     {
218         if (text == null || text.length() == 0)
219         {
220             throw new IllegalArgumentException("Error parsing FloatDuration -- null or empty argument");
221         }
222         Matcher matcher = NUMBER_PATTERN.matcher(text);
223         if (matcher.find())
224         {
225             int index = matcher.end();
226             try
227             {
228                 String unitString = text.substring(index).trim();
229                 String valueString = text.substring(0, index).trim();
230                 for (DurationUnit unit : Unit.getUnits(DurationUnit.class))
231                 {
232                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
233                     {
234                         float f = Float.parseFloat(valueString);
235                         return new FloatDuration(f, unit);
236                     }
237                 }
238             }
239             catch (Exception exception)
240             {
241                 throw new IllegalArgumentException("Error parsing FloatDuration from " + text, exception);
242             }
243         }
244         throw new IllegalArgumentException("Error parsing FloatDuration from " + text);
245     }
246 
247     /**
248      * Calculate the division of FloatDuration and FloatDuration, which results in a FloatDimensionless scalar.
249      * @param v FloatDuration scalar
250      * @return FloatDimensionless scalar as a division of FloatDuration and FloatDuration
251      */
252     public final FloatDimensionless divideBy(final FloatDuration v)
253     {
254         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
255     }
256 
257     /**
258      * Calculate the multiplication of FloatDuration and FloatFrequency, which results in a FloatDimensionless scalar.
259      * @param v FloatDuration scalar
260      * @return FloatDimensionless scalar as a multiplication of FloatDuration and FloatFrequency
261      */
262     public final FloatDimensionless multiplyBy(final FloatFrequency v)
263     {
264         return new FloatDimensionless(this.si * v.si, DimensionlessUnit.SI);
265     }
266 
267     /**
268      * Calculate the multiplication of FloatDuration and FloatElectricalCurrent, which results in a FloatElectricalCharge
269      * scalar.
270      * @param v FloatDuration scalar
271      * @return FloatElectricalCharge scalar as a multiplication of FloatDuration and FloatElectricalCurrent
272      */
273     public final FloatElectricalCharge multiplyBy(final FloatElectricalCurrent v)
274     {
275         return new FloatElectricalCharge(this.si * v.si, ElectricalChargeUnit.SI);
276     }
277 
278     /**
279      * Calculate the multiplication of FloatDuration and FloatFlowMass, which results in a FloatMass scalar.
280      * @param v FloatDuration scalar
281      * @return FloatMass scalar as a multiplication of FloatDuration and FloatFlowMass
282      */
283     public final FloatMass multiplyBy(final FloatFlowMass v)
284     {
285         return new FloatMass(this.si * v.si, MassUnit.SI);
286     }
287 
288     /**
289      * Calculate the multiplication of FloatDuration and FloatFlowVolume, which results in a FloatVolume scalar.
290      * @param v FloatDuration scalar
291      * @return FloatVolume scalar as a multiplication of FloatDuration and FloatFlowVolume
292      */
293     public final FloatVolume multiplyBy(final FloatFlowVolume v)
294     {
295         return new FloatVolume(this.si * v.si, VolumeUnit.SI);
296     }
297 
298     /**
299      * Calculate the multiplication of FloatDuration and FloatAcceleration, which results in a FloatSpeed scalar.
300      * @param v FloatDuration scalar
301      * @return FloatSpeed scalar as a multiplication of FloatDuration and FloatAcceleration
302      */
303     public final FloatSpeed multiplyBy(final FloatAcceleration v)
304     {
305         return new FloatSpeed(this.si * v.si, SpeedUnit.SI);
306     }
307 
308     /**
309      * Calculate the multiplication of FloatDuration and FloatPower, which results in a FloatEnergy scalar.
310      * @param v FloatDuration scalar
311      * @return FloatEnergy scalar as a multiplication of FloatDuration and FloatPower
312      */
313     public final FloatEnergy multiplyBy(final FloatPower v)
314     {
315         return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
316     }
317 
318     /**
319      * Calculate the multiplication of FloatDuration and FloatSpeed, which results in a FloatLength scalar.
320      * @param v FloatDuration scalar
321      * @return FloatLength scalar as a multiplication of FloatDuration and FloatSpeed
322      */
323     public final FloatLength multiplyBy(final FloatSpeed v)
324     {
325         return new FloatLength(this.si * v.si, LengthUnit.SI);
326     }
327 
328     /**
329      * Calculate the multiplication of FloatDuration and FloatMoneyPerDuration, which results in a FloatMoney scalar.
330      * @param v FloatDuration scalar
331      * @return FloatMoney scalar as a multiplication of FloatDuration and FloatMoneyPerDuration
332      */
333     public final FloatMoney multiplyBy(final FloatMoneyPerDuration v)
334     {
335         return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
336     }
337 
338 }