View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.AreaUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.DurationUnit;
8   import org.djunits.unit.EnergyUnit;
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.unit.LinearDensityUnit;
11  import org.djunits.unit.MoneyUnit;
12  import org.djunits.unit.PositionUnit;
13  import org.djunits.unit.SpeedUnit;
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;LengthUnit&gt; value = new FloatScalar.Rel&lt;LengthUnit&gt;(100.0, LengthUnit.SI);
22   * </pre>
23   * 
24   * we can now write:
25   * 
26   * <pre>
27   * FloatLength value = new FloatLength(100.0, LengthUnit.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: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, 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 FloatLength extends AbstractFloatScalarRel<LengthUnit, FloatLength>
43  {
44      /** */
45      private static final long serialVersionUID = 20150901L;
46  
47      /** constant with value zero. */
48      public static final FloatLength ZERO = new FloatLength(0.0f, LengthUnit.SI);
49  
50      /** constant with value NaN. */
51      @SuppressWarnings("checkstyle:constantname")
52      public static final FloatLength NaN = new FloatLength(Float.NaN, LengthUnit.SI);
53  
54      /** constant with value POSITIVE_INFINITY. */
55      public static final FloatLength POSITIVE_INFINITY = new FloatLength(Float.POSITIVE_INFINITY, LengthUnit.SI);
56  
57      /** constant with value NEGATIVE_INFINITY. */
58      public static final FloatLength NEGATIVE_INFINITY = new FloatLength(Float.NEGATIVE_INFINITY, LengthUnit.SI);
59  
60      /** constant with value MAX_VALUE. */
61      public static final FloatLength POS_MAXVALUE = new FloatLength(Float.MAX_VALUE, LengthUnit.SI);
62  
63      /** constant with value -MAX_VALUE. */
64      public static final FloatLength NEG_MAXVALUE = new FloatLength(-Float.MAX_VALUE, LengthUnit.SI);
65  
66      /**
67       * Construct FloatLength scalar.
68       * @param value float value
69       * @param unit unit for the float value
70       */
71      public FloatLength(final float value, final LengthUnit unit)
72      {
73          super(value, unit);
74      }
75  
76      /**
77       * Construct FloatLength scalar.
78       * @param value Scalar from which to construct this instance
79       */
80      public FloatLength(final FloatLength value)
81      {
82          super(value);
83      }
84  
85      /**
86       * Construct FloatLength scalar using a double value.
87       * @param value double value
88       * @param unit unit for the resulting float value
89       */
90      public FloatLength(final double value, final LengthUnit unit)
91      {
92          super((float) value, unit);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public final FloatLength instantiateRel(final float value, final LengthUnit unit)
98      {
99          return new FloatLength(value, unit);
100     }
101 
102     /**
103      * Construct FloatLength scalar.
104      * @param value float value in SI units
105      * @return the new scalar with the SI value
106      */
107     public static final FloatLength createSI(final float value)
108     {
109         return new FloatLength(value, LengthUnit.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 FloatPosition instantiateAbs(final float value, final PositionUnit unit)
119     {
120         return new FloatPosition(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 FloatLength interpolate(final FloatLength zero, final FloatLength one, final float ratio)
131     {
132         return new FloatLength(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 FloatPosition plus(final FloatPosition v)
141     {
142         PositionUnit 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 FloatLength max(final FloatLength r1, final FloatLength 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 FloatLength max(final FloatLength r1, final FloatLength r2, final FloatLength... rn)
165     {
166         FloatLength maxr = (r1.gt(r2)) ? r1 : r2;
167         for (FloatLength 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 FloatLength min(final FloatLength r1, final FloatLength 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 FloatLength min(final FloatLength r1, final FloatLength r2, final FloatLength... rn)
196     {
197         FloatLength minr = (r1.lt(r2)) ? r1 : r2;
198         for (FloatLength r : rn)
199         {
200             if (r.lt(minr))
201             {
202                 minr = r;
203             }
204         }
205         return minr;
206     }
207 
208     /**
209      * Returns a FloatLength 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 FloatLength
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 FloatLength valueOf(final String text) throws IllegalArgumentException
217     {
218         if (text == null || text.length() == 0)
219         {
220             throw new IllegalArgumentException("Error parsing FloatLength -- 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 (LengthUnit unit : Unit.getUnits(LengthUnit.class))
231                 {
232                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
233                     {
234                         float f = Float.parseFloat(valueString);
235                         return new FloatLength(f, unit);
236                     }
237                 }
238             }
239             catch (Exception exception)
240             {
241                 throw new IllegalArgumentException("Error parsing FloatLength from " + text, exception);
242             }
243         }
244         throw new IllegalArgumentException("Error parsing FloatLength from " + text);
245     }
246 
247     /**
248      * Calculate the division of FloatLength and FloatLength, which results in a FloatDimensionless scalar.
249      * @param v FloatLength scalar
250      * @return FloatDimensionless scalar as a division of FloatLength and FloatLength
251      */
252     public final FloatDimensionless divideBy(final FloatLength v)
253     {
254         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
255     }
256 
257     /**
258      * Calculate the multiplication of FloatLength and FloatLength, which results in a FloatArea scalar.
259      * @param v FloatLength scalar
260      * @return FloatArea scalar as a multiplication of FloatLength and FloatLength
261      */
262     public final FloatArea multiplyBy(final FloatLength v)
263     {
264         return new FloatArea(this.si * v.si, AreaUnit.SI);
265     }
266 
267     /**
268      * Calculate the division of FloatLength and FloatLinearDensity, which results in a FloatArea scalar.
269      * @param v FloatLength scalar
270      * @return FloatArea scalar as a division of FloatLength and FloatLinearDensity
271      */
272     public final FloatArea divideBy(final FloatLinearDensity v)
273     {
274         return new FloatArea(this.si / v.si, AreaUnit.SI);
275     }
276 
277     /**
278      * Calculate the division of FloatLength and FloatArea, which results in a FloatLinearDensity scalar.
279      * @param v FloatLength scalar
280      * @return FloatLinearDensity scalar as a division of FloatLength and FloatArea
281      */
282     public final FloatLinearDensity divideBy(final FloatArea v)
283     {
284         return new FloatLinearDensity(this.si / v.si, LinearDensityUnit.SI);
285     }
286 
287     /**
288      * Calculate the multiplication of FloatLength and FloatArea, which results in a FloatVolume scalar.
289      * @param v FloatLength scalar
290      * @return FloatVolume scalar as a multiplication of FloatLength and FloatArea
291      */
292     public final FloatVolume multiplyBy(final FloatArea v)
293     {
294         return new FloatVolume(this.si * v.si, VolumeUnit.SI);
295     }
296 
297     /**
298      * Calculate the multiplication of FloatLength and FloatForce, which results in a FloatEnergy scalar.
299      * @param v FloatLength scalar
300      * @return FloatEnergy scalar as a multiplication of FloatLength and FloatForce
301      */
302     public final FloatEnergy multiplyBy(final FloatForce v)
303     {
304         return new FloatEnergy(this.si * v.si, EnergyUnit.SI);
305     }
306 
307     /**
308      * Calculate the multiplication of FloatLength and FloatFrequency, which results in a FloatSpeed scalar.
309      * @param v FloatLength scalar
310      * @return FloatSpeed scalar as a multiplication of FloatLength and FloatFrequency
311      */
312     public final FloatSpeed multiplyBy(final FloatFrequency v)
313     {
314         return new FloatSpeed(this.si * v.si, SpeedUnit.SI);
315     }
316 
317     /**
318      * Calculate the division of FloatLength and FloatDuration, which results in a FloatSpeed scalar.
319      * @param v FloatLength scalar
320      * @return FloatSpeed scalar as a division of FloatLength and FloatDuration
321      */
322     public final FloatSpeed divideBy(final FloatDuration v)
323     {
324         return new FloatSpeed(this.si / v.si, SpeedUnit.SI);
325     }
326 
327     /**
328      * Calculate the division of FloatLength and FloatSpeed, which results in a FloatDuration scalar.
329      * @param v FloatLength scalar
330      * @return FloatDuration scalar as a division of FloatLength and FloatSpeed
331      */
332     public final FloatDuration divideBy(final FloatSpeed v)
333     {
334         return new FloatDuration(this.si / v.si, DurationUnit.SI);
335     }
336 
337     /**
338      * Calculate the multiplication of FloatLength and FloatMoneyPerLength, which results in a FloatMoney scalar.
339      * @param v FloatLength scalar
340      * @return FloatMoney scalar as a multiplication of FloatLength and FloatMoneyPerLength
341      */
342     public final FloatMoney multiplyBy(final FloatMoneyPerLength v)
343     {
344         return new FloatMoney(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
345     }
346 
347 }