View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.AccelerationUnit;
6   import org.djunits.unit.AngularAccelerationUnit;
7   import org.djunits.unit.AngularVelocityUnit;
8   import org.djunits.unit.DimensionlessUnit;
9   import org.djunits.unit.FrequencyUnit;
10  import org.djunits.unit.PowerUnit;
11  import org.djunits.unit.SpeedUnit;
12  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
13  import org.djutils.base.NumberParser;
14  import org.djutils.exceptions.Throw;
15  
16  import jakarta.annotation.Generated;
17  
18  /**
19   * Easy access methods for the Frequency DoubleScalar, which is relative by definition.
20   * <p>
21   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
23   * </p>
24   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
26   */
27  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2023-07-23T14:06:38.224104100Z")
28  public class Frequency extends DoubleScalarRel<FrequencyUnit, Frequency>
29  {
30      /** */
31      private static final long serialVersionUID = 20150905L;
32  
33      /** Constant with value zero. */
34      public static final Frequency ZERO = new Frequency(0.0, FrequencyUnit.SI);
35  
36      /** Constant with value one. */
37      public static final Frequency ONE = new Frequency(1.0, FrequencyUnit.SI);
38  
39      /** Constant with value NaN. */
40      @SuppressWarnings("checkstyle:constantname")
41      public static final Frequency NaN = new Frequency(Double.NaN, FrequencyUnit.SI);
42  
43      /** Constant with value POSITIVE_INFINITY. */
44      public static final Frequency POSITIVE_INFINITY = new Frequency(Double.POSITIVE_INFINITY, FrequencyUnit.SI);
45  
46      /** Constant with value NEGATIVE_INFINITY. */
47      public static final Frequency NEGATIVE_INFINITY = new Frequency(Double.NEGATIVE_INFINITY, FrequencyUnit.SI);
48  
49      /** Constant with value MAX_VALUE. */
50      public static final Frequency POS_MAXVALUE = new Frequency(Double.MAX_VALUE, FrequencyUnit.SI);
51  
52      /** Constant with value -MAX_VALUE. */
53      public static final Frequency NEG_MAXVALUE = new Frequency(-Double.MAX_VALUE, FrequencyUnit.SI);
54  
55      /**
56       * Construct Frequency scalar.
57       * @param value double; the double value
58       * @param unit FrequencyUnit; unit for the double value
59       */
60      public Frequency(final double value, final FrequencyUnit unit)
61      {
62          super(value, unit);
63      }
64  
65      /**
66       * Construct Frequency scalar.
67       * @param value Frequency; Scalar from which to construct this instance
68       */
69      public Frequency(final Frequency value)
70      {
71          super(value);
72      }
73  
74      @Override
75      public final Frequency instantiateRel(final double value, final FrequencyUnit unit)
76      {
77          return new Frequency(value, unit);
78      }
79  
80      /**
81       * Construct Frequency scalar.
82       * @param value double; the double value in SI units
83       * @return Frequency; the new scalar with the SI value
84       */
85      public static final Frequency instantiateSI(final double value)
86      {
87          return new Frequency(value, FrequencyUnit.SI);
88      }
89  
90      /**
91       * Interpolate between two values.
92       * @param zero Frequency; the low value
93       * @param one Frequency; the high value
94       * @param ratio double; the ratio between 0 and 1, inclusive
95       * @return Frequency; a Scalar at the ratio between
96       */
97      public static Frequency interpolate(final Frequency zero, final Frequency one, final double ratio)
98      {
99          return new Frequency(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
100                 zero.getDisplayUnit());
101     }
102 
103     /**
104      * Return the maximum value of two relative scalars.
105      * @param r1 Frequency; the first scalar
106      * @param r2 Frequency; the second scalar
107      * @return Frequency; the maximum value of two relative scalars
108      */
109     public static Frequency max(final Frequency r1, final Frequency r2)
110     {
111         return r1.gt(r2) ? r1 : r2;
112     }
113 
114     /**
115      * Return the maximum value of more than two relative scalars.
116      * @param r1 Frequency; the first scalar
117      * @param r2 Frequency; the second scalar
118      * @param rn Frequency...; the other scalars
119      * @return Frequency; the maximum value of more than two relative scalars
120      */
121     public static Frequency max(final Frequency r1, final Frequency r2, final Frequency... rn)
122     {
123         Frequency maxr = r1.gt(r2) ? r1 : r2;
124         for (Frequency r : rn)
125         {
126             if (r.gt(maxr))
127             {
128                 maxr = r;
129             }
130         }
131         return maxr;
132     }
133 
134     /**
135      * Return the minimum value of two relative scalars.
136      * @param r1 Frequency; the first scalar
137      * @param r2 Frequency; the second scalar
138      * @return Frequency; the minimum value of two relative scalars
139      */
140     public static Frequency min(final Frequency r1, final Frequency r2)
141     {
142         return r1.lt(r2) ? r1 : r2;
143     }
144 
145     /**
146      * Return the minimum value of more than two relative scalars.
147      * @param r1 Frequency; the first scalar
148      * @param r2 Frequency; the second scalar
149      * @param rn Frequency...; the other scalars
150      * @return Frequency; the minimum value of more than two relative scalars
151      */
152     public static Frequency min(final Frequency r1, final Frequency r2, final Frequency... rn)
153     {
154         Frequency minr = r1.lt(r2) ? r1 : r2;
155         for (Frequency r : rn)
156         {
157             if (r.lt(minr))
158             {
159                 minr = r;
160             }
161         }
162         return minr;
163     }
164 
165     /**
166      * Returns a Frequency representation of a textual representation of a value with a unit. The String representation that can
167      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
168      * allowed, but not required, between the value and the unit.
169      * @param text String; the textual representation to parse into a Frequency
170      * @return Frequency; the Scalar representation of the value in its unit
171      * @throws IllegalArgumentException when the text cannot be parsed
172      * @throws NullPointerException when the text argument is null
173      */
174     public static Frequency valueOf(final String text)
175     {
176         Throw.whenNull(text, "Error parsing Frequency: text to parse is null");
177         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Frequency: empty text to parse");
178         try
179         {
180             NumberParser numberParser = new NumberParser().lenient().trailing();
181             double d = numberParser.parseDouble(text);
182             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
183             FrequencyUnit unit = FrequencyUnit.BASE.getUnitByAbbreviation(unitString);
184             if (unit == null)
185                 throw new IllegalArgumentException("Unit " + unitString + " not found");
186             return new Frequency(d, unit);
187         }
188         catch (Exception exception)
189         {
190             throw new IllegalArgumentException(
191                     "Error parsing Frequency from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
192                     exception);
193         }
194     }
195 
196     /**
197      * Returns a Frequency based on a value and the textual representation of the unit, which can be localized.
198      * @param value double; the value to use
199      * @param unitString String; the textual representation of the unit
200      * @return Frequency; the Scalar representation of the value in its unit
201      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
202      * @throws NullPointerException when the unitString argument is null
203      */
204     public static Frequency of(final double value, final String unitString)
205     {
206         Throw.whenNull(unitString, "Error parsing Frequency: unitString is null");
207         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Frequency: empty unitString");
208         FrequencyUnit unit = FrequencyUnit.BASE.getUnitByAbbreviation(unitString);
209         if (unit != null)
210         {
211             return new Frequency(value, unit);
212         }
213         throw new IllegalArgumentException("Error parsing Frequency with unit " + unitString);
214     }
215 
216     /**
217      * Calculate the division of Frequency and Frequency, which results in a Dimensionless scalar.
218      * @param v Frequency; scalar
219      * @return Dimensionless; scalar as a division of Frequency and Frequency
220      */
221     public final Dimensionless divide(final Frequency v)
222     {
223         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
224     }
225 
226     /**
227      * Calculate the multiplication of Frequency and Duration, which results in a Dimensionless scalar.
228      * @param v Frequency; scalar
229      * @return Dimensionless; scalar as a multiplication of Frequency and Duration
230      */
231     public final Dimensionless times(final Duration v)
232     {
233         return new Dimensionless(this.si * v.si, DimensionlessUnit.SI);
234     }
235 
236     /**
237      * Calculate the multiplication of Frequency and Length, which results in a Speed scalar.
238      * @param v Frequency; scalar
239      * @return Speed; scalar as a multiplication of Frequency and Length
240      */
241     public final Speed times(final Length v)
242     {
243         return new Speed(this.si * v.si, SpeedUnit.SI);
244     }
245 
246     /**
247      * Calculate the multiplication of Frequency and Speed, which results in a Acceleration scalar.
248      * @param v Frequency; scalar
249      * @return Acceleration; scalar as a multiplication of Frequency and Speed
250      */
251     public final Acceleration times(final Speed v)
252     {
253         return new Acceleration(this.si * v.si, AccelerationUnit.SI);
254     }
255 
256     /**
257      * Calculate the multiplication of Frequency and Energy, which results in a Power scalar.
258      * @param v Frequency; scalar
259      * @return Power; scalar as a multiplication of Frequency and Energy
260      */
261     public final Power times(final Energy v)
262     {
263         return new Power(this.si * v.si, PowerUnit.SI);
264     }
265 
266     /**
267      * Calculate the multiplication of Frequency and Angle, which results in a AngularVelocity scalar.
268      * @param v Frequency; scalar
269      * @return AngularVelocity; scalar as a multiplication of Frequency and Angle
270      */
271     public final AngularVelocity times(final Angle v)
272     {
273         return new AngularVelocity(this.si * v.si, AngularVelocityUnit.SI);
274     }
275 
276     /**
277      * Calculate the multiplication of Frequency and AngularVelocity, which results in a AngularAcceleration scalar.
278      * @param v Frequency; scalar
279      * @return AngularAcceleration; scalar as a multiplication of Frequency and AngularVelocity
280      */
281     public final AngularAcceleration times(final AngularVelocity v)
282     {
283         return new AngularAcceleration(this.si * v.si, AngularAccelerationUnit.SI);
284     }
285 
286     @Override
287     public Duration reciprocal()
288     {
289         return Duration.instantiateSI(1.0 / this.si);
290     }
291 
292 }