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.ForceUnit;
7   import org.djunits.unit.FrequencyUnit;
8   import org.djunits.unit.LengthUnit;
9   import org.djunits.unit.LinearDensityUnit;
10  import org.djunits.unit.MoneyPerLengthUnit;
11  import org.djunits.unit.Unit;
12  
13  /**
14   * Easy access methods for the LinearDensity FloatScalar, which is relative by definition. An example is Speed. Instead of:
15   * 
16   * <pre>
17   * FloatScalar.Rel&lt;LinearDensityUnit&gt; value = new FloatScalar.Rel&lt;LinearDensityUnit&gt;(100.0, LinearDensityUnit.SI);
18   * </pre>
19   * 
20   * we can now write:
21   * 
22   * <pre>
23   * FloatLinearDensity value = new FloatLinearDensity(100.0, LinearDensityUnit.SI);
24   * </pre>
25   * 
26   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
27   * used are compatible.
28   * <p>
29   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
31   * <p>
32   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
33   * initial version Sep 5, 2015 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   */
37  public class FloatLinearDensity extends AbstractFloatScalarRel<LinearDensityUnit, FloatLinearDensity>
38  {
39      /** */
40      private static final long serialVersionUID = 20150901L;
41  
42      /** constant with value zero. */
43      public static final FloatLinearDensity ZERO = new FloatLinearDensity(0.0f, LinearDensityUnit.SI);
44  
45      /** constant with value NaN. */
46      @SuppressWarnings("checkstyle:constantname")
47      public static final FloatLinearDensity NaN = new FloatLinearDensity(Float.NaN, LinearDensityUnit.SI);
48  
49      /** constant with value POSITIVE_INFINITY. */
50      public static final FloatLinearDensity POSITIVE_INFINITY =
51              new FloatLinearDensity(Float.POSITIVE_INFINITY, LinearDensityUnit.SI);
52  
53      /** constant with value NEGATIVE_INFINITY. */
54      public static final FloatLinearDensity NEGATIVE_INFINITY =
55              new FloatLinearDensity(Float.NEGATIVE_INFINITY, LinearDensityUnit.SI);
56  
57      /** constant with value MAX_VALUE. */
58      public static final FloatLinearDensity POS_MAXVALUE = new FloatLinearDensity(Float.MAX_VALUE, LinearDensityUnit.SI);
59  
60      /** constant with value -MAX_VALUE. */
61      public static final FloatLinearDensity NEG_MAXVALUE = new FloatLinearDensity(-Float.MAX_VALUE, LinearDensityUnit.SI);
62  
63      /**
64       * Construct FloatLinearDensity scalar.
65       * @param value float value
66       * @param unit unit for the float value
67       */
68      public FloatLinearDensity(final float value, final LinearDensityUnit unit)
69      {
70          super(value, unit);
71      }
72  
73      /**
74       * Construct FloatLinearDensity scalar.
75       * @param value Scalar from which to construct this instance
76       */
77      public FloatLinearDensity(final FloatLinearDensity value)
78      {
79          super(value);
80      }
81  
82      /**
83       * Construct FloatLinearDensity scalar using a double value.
84       * @param value double value
85       * @param unit unit for the resulting float value
86       */
87      public FloatLinearDensity(final double value, final LinearDensityUnit unit)
88      {
89          super((float) value, unit);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public final FloatLinearDensity instantiateRel(final float value, final LinearDensityUnit unit)
95      {
96          return new FloatLinearDensity(value, unit);
97      }
98  
99      /**
100      * Construct FloatLinearDensity scalar.
101      * @param value float value in SI units
102      * @return the new scalar with the SI value
103      */
104     public static final FloatLinearDensity createSI(final float value)
105     {
106         return new FloatLinearDensity(value, LinearDensityUnit.SI);
107     }
108 
109     /**
110      * Interpolate between two values.
111      * @param zero the low value
112      * @param one the high value
113      * @param ratio the ratio between 0 and 1, inclusive
114      * @return a Scalar at the ratio between
115      */
116     public static FloatLinearDensity interpolate(final FloatLinearDensity zero, final FloatLinearDensity one, final float ratio)
117     {
118         return new FloatLinearDensity(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
119     }
120 
121     /**
122      * Return the maximum value of two relative scalars.
123      * @param r1 the first scalar
124      * @param r2 the second scalar
125      * @return the maximum value of two relative scalars
126      */
127     public static FloatLinearDensity max(final FloatLinearDensity r1, final FloatLinearDensity r2)
128     {
129         return (r1.gt(r2)) ? r1 : r2;
130     }
131 
132     /**
133      * Return the maximum value of more than two relative scalars.
134      * @param r1 the first scalar
135      * @param r2 the second scalar
136      * @param rn the other scalars
137      * @return the maximum value of more than two relative scalars
138      */
139     public static FloatLinearDensity max(final FloatLinearDensity r1, final FloatLinearDensity r2,
140             final FloatLinearDensity... rn)
141     {
142         FloatLinearDensity maxr = (r1.gt(r2)) ? r1 : r2;
143         for (FloatLinearDensity r : rn)
144         {
145             if (r.gt(maxr))
146             {
147                 maxr = r;
148             }
149         }
150         return maxr;
151     }
152 
153     /**
154      * Return the minimum value of two relative scalars.
155      * @param r1 the first scalar
156      * @param r2 the second scalar
157      * @return the minimum value of two relative scalars
158      */
159     public static FloatLinearDensity min(final FloatLinearDensity r1, final FloatLinearDensity r2)
160     {
161         return (r1.lt(r2)) ? r1 : r2;
162     }
163 
164     /**
165      * Return the minimum value of more than two relative scalars.
166      * @param r1 the first scalar
167      * @param r2 the second scalar
168      * @param rn the other scalars
169      * @return the minimum value of more than two relative scalars
170      */
171     public static FloatLinearDensity min(final FloatLinearDensity r1, final FloatLinearDensity r2,
172             final FloatLinearDensity... rn)
173     {
174         FloatLinearDensity minr = (r1.lt(r2)) ? r1 : r2;
175         for (FloatLinearDensity r : rn)
176         {
177             if (r.lt(minr))
178             {
179                 minr = r;
180             }
181         }
182         return minr;
183     }
184 
185     /**
186      * Returns a FloatLinearDensity representation of a textual representation of a value with a unit. The String representation
187      * that can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are
188      * allowed, but not necessary, between the value and the unit.
189      * @param text String; the textual representation to parse into a FloatLinearDensity
190      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
191      * @throws IllegalArgumentException when the text cannot be parsed
192      */
193     public static FloatLinearDensity valueOf(final String text) throws IllegalArgumentException
194     {
195         if (text == null || text.length() == 0)
196         {
197             throw new IllegalArgumentException("Error parsing FloatLinearDensity -- null or empty argument");
198         }
199         Matcher matcher = NUMBER_PATTERN.matcher(text);
200         if (matcher.find())
201         {
202             int index = matcher.end();
203             try
204             {
205                 String unitString = text.substring(index).trim();
206                 String valueString = text.substring(0, index).trim();
207                 for (LinearDensityUnit unit : Unit.getUnits(LinearDensityUnit.class))
208                 {
209                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
210                     {
211                         float f = Float.parseFloat(valueString);
212                         return new FloatLinearDensity(f, unit);
213                     }
214                 }
215             }
216             catch (Exception exception)
217             {
218                 throw new IllegalArgumentException("Error parsing FloatLinearDensity from " + text, exception);
219             }
220         }
221         throw new IllegalArgumentException("Error parsing FloatLinearDensity from " + text);
222     }
223 
224     /**
225      * Calculate the division of FloatLinearDensity and FloatLinearDensity, which results in a FloatDimensionless scalar.
226      * @param v FloatLinearDensity scalar
227      * @return FloatDimensionless scalar as a division of FloatLinearDensity and FloatLinearDensity
228      */
229     public final FloatDimensionless divideBy(final FloatLinearDensity v)
230     {
231         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
232     }
233 
234     /**
235      * Calculate the multiplication of FloatLinearDensity and FloatArea, which results in a FloatLength scalar.
236      * @param v FloatLinearDensity scalar
237      * @return FloatLength scalar as a multiplication of FloatLinearDensity and FloatArea
238      */
239     public final FloatLength multiplyBy(final FloatArea v)
240     {
241         return new FloatLength(this.si * v.si, LengthUnit.SI);
242     }
243 
244     /**
245      * Calculate the multiplication of FloatLinearDensity and FloatEnergy, which results in a FloatForce scalar.
246      * @param v FloatLinearDensity scalar
247      * @return FloatForce scalar as a multiplication of FloatLinearDensity and FloatEnergy
248      */
249     public final FloatForce multiplyBy(final FloatEnergy v)
250     {
251         return new FloatForce(this.si * v.si, ForceUnit.SI);
252     }
253 
254     /**
255      * Calculate the multiplication of FloatLinearDensity and FloatSpeed, which results in a FloatFrequency scalar.
256      * @param v FloatLinearDensity scalar
257      * @return FloatFrequency scalar as a multiplication of FloatLinearDensity and FloatSpeed
258      */
259     public final FloatFrequency multiplyBy(final FloatSpeed v)
260     {
261         return new FloatFrequency(this.si * v.si, FrequencyUnit.SI);
262     }
263 
264     /**
265      * Calculate the multiplication of FloatLinearDensity and FloatMoney, which results in a FloatMoneyPerLength scalar.
266      * @param v FloatLinearDensity scalar
267      * @return FloatMoneyPerLength scalar as a multiplication of FloatLinearDensity and FloatMoney
268      */
269     public final FloatMoneyPerLength multiplyBy(final FloatMoney v)
270     {
271         return new FloatMoneyPerLength(this.si * v.si, MoneyPerLengthUnit.getStandardMoneyPerLengthUnit());
272     }
273 
274 }