View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.AngleUnit;
6   import org.djunits.unit.AngularVelocityUnit;
7   import org.djunits.unit.DimensionlessUnit;
8   import org.djunits.unit.DirectionUnit;
9   import org.djunits.unit.DurationUnit;
10  import org.djunits.value.vfloat.scalar.base.FloatScalarRel;
11  import org.djunits.value.vfloat.scalar.base.FloatScalarRelWithAbs;
12  import org.djutils.base.NumberParser;
13  import org.djutils.exceptions.Throw;
14  
15  import jakarta.annotation.Generated;
16  
17  /**
18   * Easy access methods for the FloatAngle FloatScalar.
19   * <p>
20   * Copyright (c) 2013-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
21   * 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 = "2025-09-06T15:16:28.380798Z")
28  public class FloatAngle extends FloatScalarRelWithAbs<DirectionUnit, FloatDirection, AngleUnit, FloatAngle>
29  {
30      /** */
31      private static final long serialVersionUID = 20150901L;
32  
33      /** Constant with value zero. */
34      public static final FloatAngle ZERO = new FloatAngle(0.0f, AngleUnit.SI);
35  
36      /** Constant with value one. */
37      public static final FloatAngle ONE = new FloatAngle(1.0f, AngleUnit.SI);
38  
39      /** Constant with value pi. */
40      public static final FloatAngle PI = new FloatAngle((float) Math.PI, AngleUnit.RADIAN);
41  
42      /** Constant with value pi/2. */
43      public static final FloatAngle HALF_PI = new FloatAngle((float) (Math.PI / 2.0), AngleUnit.RADIAN);
44  
45      /** Constant with value tau. */
46      public static final FloatAngle TAU = new FloatAngle((float) (Math.PI * 2.0), AngleUnit.RADIAN);
47  
48      /** Constant with value NaN. */
49      @SuppressWarnings("checkstyle:constantname")
50      public static final FloatAngle NaN = new FloatAngle(Float.NaN, AngleUnit.SI);
51  
52      /** Constant with value POSITIVE_INFINITY. */
53      public static final FloatAngle POSITIVE_INFINITY = new FloatAngle(Float.POSITIVE_INFINITY, AngleUnit.SI);
54  
55      /** Constant with value NEGATIVE_INFINITY. */
56      public static final FloatAngle NEGATIVE_INFINITY = new FloatAngle(Float.NEGATIVE_INFINITY, AngleUnit.SI);
57  
58      /** Constant with value MAX_VALUE. */
59      public static final FloatAngle POS_MAXVALUE = new FloatAngle(Float.MAX_VALUE, AngleUnit.SI);
60  
61      /** Constant with value -MAX_VALUE. */
62      public static final FloatAngle NEG_MAXVALUE = new FloatAngle(-Float.MAX_VALUE, AngleUnit.SI);
63  
64      /**
65       * Construct FloatAngle scalar with a unit.
66       * @param value the float value, expressed in the given unit
67       * @param unit unit for the float value
68       */
69      public FloatAngle(final float value, final AngleUnit unit)
70      {
71          super(value, unit);
72      }
73  
74      /**
75       * Construct FloatAngle scalar.
76       * @param value Scalar from which to construct this instance
77       */
78      public FloatAngle(final FloatAngle value)
79      {
80          super(value);
81      }
82  
83      /**
84       * Construct FloatAngle scalar with a unit using a double value.
85       * @param value the double value, expressed in the given unit
86       * @param unit unit for the resulting float value
87       */
88      public FloatAngle(final double value, final AngleUnit unit)
89      {
90          super((float) value, unit);
91      }
92  
93      @Override
94      public final FloatAngle instantiateRel(final float value, final AngleUnit unit)
95      {
96          return new FloatAngle(value, unit);
97      }
98  
99      /**
100      * Construct FloatAngle scalar based on an SI value.
101      * @param value the float value in SI units
102      * @return the new scalar with the SI value
103      */
104     public static final FloatAngle ofSI(final float value)
105     {
106         return new FloatAngle(value, AngleUnit.SI);
107     }
108 
109     @Override
110     public final FloatDirection instantiateAbs(final float value, final DirectionUnit unit)
111     {
112         return new FloatDirection(value, unit);
113     }
114 
115     /**
116      * Interpolate between two values. Note that the first value does not have to be smaller than the second.
117      * @param zero the value at a ratio of zero
118      * @param one the value at a ratio of one
119      * @param ratio the ratio between 0 and 1, inclusive
120      * @return a FloatAngle at the given ratio between 0 and 1
121      */
122     public static FloatAngle interpolate(final FloatAngle zero, final FloatAngle one, final float ratio)
123     {
124         Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
125                 "ratio for interpolation should be between 0 and 1, but is %f", ratio);
126         return new FloatAngle(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
127                 zero.getDisplayUnit());
128     }
129 
130     /**
131      * Return the maximum value of two relative scalars.
132      * @param r1 the first scalar
133      * @param r2 the second scalar
134      * @return the maximum value of two relative scalars
135      */
136     public static FloatAngle max(final FloatAngle r1, final FloatAngle r2)
137     {
138         return r1.gt(r2) ? r1 : r2;
139     }
140 
141     /**
142      * Return the maximum value of more than two relative scalars.
143      * @param r1 the first scalar
144      * @param r2 the second scalar
145      * @param rn the other scalars
146      * @return the maximum value of more than two relative scalars
147      */
148     public static FloatAngle max(final FloatAngle r1, final FloatAngle r2, final FloatAngle... rn)
149     {
150         FloatAngle maxr = r1.gt(r2) ? r1 : r2;
151         for (FloatAngle r : rn)
152         {
153             if (r.gt(maxr))
154             {
155                 maxr = r;
156             }
157         }
158         return maxr;
159     }
160 
161     /**
162      * Return the minimum value of two relative scalars.
163      * @param r1 the first scalar
164      * @param r2 the second scalar
165      * @return the minimum value of two relative scalars
166      */
167     public static FloatAngle min(final FloatAngle r1, final FloatAngle r2)
168     {
169         return r1.lt(r2) ? r1 : r2;
170     }
171 
172     /**
173      * Return the minimum value of more than two relative scalars.
174      * @param r1 the first scalar
175      * @param r2 the second scalar
176      * @param rn the other scalars
177      * @return the minimum value of more than two relative scalars
178      */
179     public static FloatAngle min(final FloatAngle r1, final FloatAngle r2, final FloatAngle... rn)
180     {
181         FloatAngle minr = r1.lt(r2) ? r1 : r2;
182         for (FloatAngle r : rn)
183         {
184             if (r.lt(minr))
185             {
186                 minr = r;
187             }
188         }
189         return minr;
190     }
191 
192     /**
193      * Returns a FloatAngle representation of a textual representation of a value with a unit. The String representation that
194      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
195      * allowed, but not required, between the value and the unit.
196      * @param text the textual representation to parse into a FloatAngle
197      * @return the Scalar representation of the value in its unit
198      * @throws IllegalArgumentException when the text cannot be parsed
199      * @throws NullPointerException when the text argument is null
200      */
201     public static FloatAngle valueOf(final String text)
202     {
203         Throw.whenNull(text, "Error parsing FloatAngle: text to parse is null");
204         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatAngle: empty text to parse");
205         try
206         {
207             NumberParser numberParser = new NumberParser().lenient().trailing();
208             float f = numberParser.parseFloat(text);
209             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
210             AngleUnit unit = AngleUnit.BASE.getUnitByAbbreviation(unitString);
211             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Angle", unitString);
212             return new FloatAngle(f, unit);
213         }
214         catch (Exception exception)
215         {
216             throw new IllegalArgumentException(
217                     "Error parsing FloatAngle from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
218                     exception);
219         }
220     }
221 
222     /**
223      * Returns a FloatAngle based on a value and the textual representation of the unit, which can be localized.
224      * @param value the value to use
225      * @param unitString the textual representation of the unit
226      * @return the Scalar representation of the value in its unit
227      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
228      * @throws NullPointerException when the unitString argument is null
229      */
230     public static FloatAngle of(final float value, final String unitString)
231     {
232         Throw.whenNull(unitString, "Error parsing FloatAngle: unitString is null");
233         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatAngle: empty unitString");
234         AngleUnit unit = AngleUnit.BASE.getUnitByAbbreviation(unitString);
235         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing FloatAngle with unit %s", unitString);
236         return new FloatAngle(value, unit);
237     }
238 
239     /**
240      * Calculate the division of FloatAngle and FloatAngle, which results in a FloatDimensionless scalar.
241      * @param v scalar
242      * @return scalar as a division of FloatAngle and FloatAngle
243      */
244     public final FloatDimensionless divide(final FloatAngle v)
245     {
246         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
247     }
248 
249     /**
250      * Calculate the multiplication of FloatAngle and FloatFrequency, which results in a FloatAngularVelocity scalar.
251      * @param v scalar
252      * @return scalar as a multiplication of FloatAngle and FloatFrequency
253      */
254     public final FloatAngularVelocity times(final FloatFrequency v)
255     {
256         return new FloatAngularVelocity(this.si * v.si, AngularVelocityUnit.SI);
257     }
258 
259     /**
260      * Calculate the division of FloatAngle and FloatDuration, which results in a FloatAngularVelocity scalar.
261      * @param v scalar
262      * @return scalar as a division of FloatAngle and FloatDuration
263      */
264     public final FloatAngularVelocity divide(final FloatDuration v)
265     {
266         return new FloatAngularVelocity(this.si / v.si, AngularVelocityUnit.SI);
267     }
268 
269     /**
270      * Calculate the division of FloatAngle and FloatAngularVelocity, which results in a FloatDuration scalar.
271      * @param v scalar
272      * @return scalar as a division of FloatAngle and FloatAngularVelocity
273      */
274     public final FloatDuration divide(final FloatAngularVelocity v)
275     {
276         return new FloatDuration(this.si / v.si, DurationUnit.SI);
277     }
278 
279     @Override
280     public FloatSIScalar reciprocal()
281     {
282         return FloatSIScalar.divide(FloatDimensionless.ONE, this);
283     }
284 
285     /**
286      * Multiply two scalars that result in a scalar of type FloatAngle.
287      * @param scalar1 the first scalar
288      * @param scalar2 the second scalar
289      * @return the multiplication of both scalars as an instance of FloatAngle
290      */
291     public static FloatAngle multiply(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
292     {
293         Throw.whenNull(scalar1, "scalar1 cannot be null");
294         Throw.whenNull(scalar2, "scalar2 cannot be null");
295         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
296                 .plus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AngleUnit.BASE.getSiDimensions()),
297                 IllegalArgumentException.class, "Multiplying %s by %s does not result in instance of type FloatAngle",
298                 scalar1.toDisplayString(), scalar2.toDisplayString());
299         return new FloatAngle(scalar1.si * scalar2.si, AngleUnit.SI);
300     }
301 
302     /**
303      * Divide two scalars that result in a scalar of type FloatAngle.
304      * @param scalar1 the first scalar
305      * @param scalar2 the second scalar
306      * @return the division of scalar1 by scalar2 as an instance of FloatAngle
307      */
308     public static FloatAngle divide(final FloatScalarRel<?, ?> scalar1, final FloatScalarRel<?, ?> scalar2)
309     {
310         Throw.whenNull(scalar1, "scalar1 cannot be null");
311         Throw.whenNull(scalar2, "scalar2 cannot be null");
312         Throw.when(!scalar1.getDisplayUnit().getQuantity().getSiDimensions()
313                 .minus(scalar2.getDisplayUnit().getQuantity().getSiDimensions()).equals(AngleUnit.BASE.getSiDimensions()),
314                 IllegalArgumentException.class, "Dividing %s by %s does not result in an instance of type FloatAngle",
315                 scalar1.toDisplayString(), scalar2.toDisplayString());
316         return new FloatAngle(scalar1.si / scalar2.si, AngleUnit.SI);
317     }
318 
319 }