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