View Javadoc
1   package org.djunits.value.vdouble.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.vdouble.scalar.base.DoubleScalar;
11  import org.djunits.value.vdouble.scalar.base.DoubleScalarRelWithAbs;
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 Relative Angle DoubleScalar.
19   * <p>
20   * Copyright (c) 2013-2024 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 = "2023-07-23T14:06:38.224104100Z")
28  public class Angle extends DoubleScalarRelWithAbs<DirectionUnit, Direction, AngleUnit, Angle>
29  {
30      /** */
31      private static final long serialVersionUID = 20150901L;
32  
33      /** Constant with value zero. */
34      public static final Angle ZERO = new Angle(0.0, AngleUnit.SI);
35  
36      /** Constant with value one. */
37      public static final Angle ONE = new Angle(1.0, AngleUnit.SI);
38  
39      /** Constant with value NaN. */
40      @SuppressWarnings("checkstyle:constantname")
41      public static final Angle NaN = new Angle(Double.NaN, AngleUnit.SI);
42  
43      /** Constant with value POSITIVE_INFINITY. */
44      public static final Angle POSITIVE_INFINITY = new Angle(Double.POSITIVE_INFINITY, AngleUnit.SI);
45  
46      /** Constant with value NEGATIVE_INFINITY. */
47      public static final Angle NEGATIVE_INFINITY = new Angle(Double.NEGATIVE_INFINITY, AngleUnit.SI);
48  
49      /** Constant with value MAX_VALUE. */
50      public static final Angle POS_MAXVALUE = new Angle(Double.MAX_VALUE, AngleUnit.SI);
51  
52      /** Constant with value -MAX_VALUE. */
53      public static final Angle NEG_MAXVALUE = new Angle(-Double.MAX_VALUE, AngleUnit.SI);
54  
55      /**
56       * Construct Angle scalar.
57       * @param value double; double value
58       * @param unit AngleUnit; unit for the double value
59       */
60      public Angle(final double value, final AngleUnit unit)
61      {
62          super(value, unit);
63      }
64  
65      /**
66       * Construct Angle scalar.
67       * @param value Angle; Scalar from which to construct this instance
68       */
69      public Angle(final Angle value)
70      {
71          super(value);
72      }
73  
74      @Override
75      public final Angle instantiateRel(final double value, final AngleUnit unit)
76      {
77          return new Angle(value, unit);
78      }
79  
80      @Override
81      public final Direction instantiateAbs(final double value, final DirectionUnit unit)
82      {
83          return new Direction(value, unit);
84      }
85  
86      /**
87       * Construct Angle scalar.
88       * @param value double; the double value in SI units
89       * @return Angle; the new scalar with the SI value
90       */
91      public static final Angle instantiateSI(final double value)
92      {
93          return new Angle(value, AngleUnit.SI);
94      }
95  
96      /**
97       * Interpolate between two values.
98       * @param zero Angle; the low value
99       * @param one Angle; the high value
100      * @param ratio double; the ratio between 0 and 1, inclusive
101      * @return Angle; a Scalar at the ratio between
102      */
103     public static Angle interpolate(final Angle zero, final Angle one, final double ratio)
104     {
105         return new Angle(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio, zero.getDisplayUnit());
106     }
107 
108     /**
109      * Return the maximum value of two relative scalars.
110      * @param r1 Angle; the first scalar
111      * @param r2 Angle; the second scalar
112      * @return Angle; the maximum value of two relative scalars
113      */
114     public static Angle max(final Angle r1, final Angle r2)
115     {
116         return r1.gt(r2) ? r1 : r2;
117     }
118 
119     /**
120      * Return the maximum value of more than two relative scalars.
121      * @param r1 Angle; the first scalar
122      * @param r2 Angle; the second scalar
123      * @param rn Angle...; the other scalars
124      * @return Angle; the maximum value of more than two relative scalars
125      */
126     public static Angle max(final Angle r1, final Angle r2, final Angle... rn)
127     {
128         Angle maxr = r1.gt(r2) ? r1 : r2;
129         for (Angle r : rn)
130         {
131             if (r.gt(maxr))
132             {
133                 maxr = r;
134             }
135         }
136         return maxr;
137     }
138 
139     /**
140      * Return the minimum value of two relative scalars.
141      * @param r1 Angle; the first scalar
142      * @param r2 Angle; the second scalar
143      * @return Angle; the minimum value of two relative scalars
144      */
145     public static Angle min(final Angle r1, final Angle r2)
146     {
147         return r1.lt(r2) ? r1 : r2;
148     }
149 
150     /**
151      * Return the minimum value of more than two relative scalars.
152      * @param r1 Angle; the first scalar
153      * @param r2 Angle; the second scalar
154      * @param rn Angle...; the other scalars
155      * @return Angle; the minimum value of more than two relative scalars
156      */
157     public static Angle min(final Angle r1, final Angle r2, final Angle... rn)
158     {
159         Angle minr = r1.lt(r2) ? r1 : r2;
160         for (Angle r : rn)
161         {
162             if (r.lt(minr))
163             {
164                 minr = r;
165             }
166         }
167         return minr;
168     }
169 
170     /**
171      * Returns a Angle representation of a textual representation of a value with a unit. The String representation that can be
172      * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
173      * but not required, between the value and the unit.
174      * @param text String; the textual representation to parse into a Angle
175      * @return Angle; the Scalar representation of the value in its unit
176      * @throws IllegalArgumentException when the text cannot be parsed
177      * @throws NullPointerException when the text argument is null
178      */
179     public static Angle valueOf(final String text)
180     {
181         Throw.whenNull(text, "Error parsing Angle: text to parse is null");
182         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Angle: empty text to parse");
183         try
184         {
185             NumberParser numberParser = new NumberParser().lenient().trailing();
186             double d = numberParser.parseDouble(text);
187             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
188             AngleUnit unit = AngleUnit.BASE.getUnitByAbbreviation(unitString);
189             if (unit == null)
190                 throw new IllegalArgumentException("Unit " + unitString + " not found");
191             return new Angle(d, unit);
192         }
193         catch (Exception exception)
194         {
195             throw new IllegalArgumentException(
196                     "Error parsing Angle from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
197                     exception);
198         }
199     }
200 
201     /**
202      * Returns a Angle based on a value and the textual representation of the unit, which can be localized.
203      * @param value double; the value to use
204      * @param unitString String; the textual representation of the unit
205      * @return Angle; the Scalar representation of the value in its unit
206      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
207      * @throws NullPointerException when the unitString argument is null
208      */
209     public static Angle of(final double value, final String unitString)
210     {
211         Throw.whenNull(unitString, "Error parsing Angle: unitString is null");
212         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Angle: empty unitString");
213         AngleUnit unit = AngleUnit.BASE.getUnitByAbbreviation(unitString);
214         if (unit != null)
215         {
216             return new Angle(value, unit);
217         }
218         throw new IllegalArgumentException("Error parsing Angle with unit " + unitString);
219     }
220 
221     /**
222      * Calculate the division of Angle and Angle, which results in a Dimensionless scalar.
223      * @param v Angle; scalar
224      * @return Dimensionless; scalar as a division of Angle and Angle
225      */
226     public final Dimensionless divide(final Angle v)
227     {
228         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
229     }
230 
231     /**
232      * Calculate the multiplication of Angle and Frequency, which results in a AngularVelocity scalar.
233      * @param v Angle; scalar
234      * @return AngularVelocity; scalar as a multiplication of Angle and Frequency
235      */
236     public final AngularVelocity times(final Frequency v)
237     {
238         return new AngularVelocity(this.si * v.si, AngularVelocityUnit.SI);
239     }
240 
241     /**
242      * Calculate the division of Angle and Duration, which results in a AngularVelocity scalar.
243      * @param v Angle; scalar
244      * @return AngularVelocity; scalar as a division of Angle and Duration
245      */
246     public final AngularVelocity divide(final Duration v)
247     {
248         return new AngularVelocity(this.si / v.si, AngularVelocityUnit.SI);
249     }
250 
251     /**
252      * Calculate the division of Angle and AngularVelocity, which results in a Duration scalar.
253      * @param v Angle; scalar
254      * @return Duration; scalar as a division of Angle and AngularVelocity
255      */
256     public final Duration divide(final AngularVelocity v)
257     {
258         return new Duration(this.si / v.si, DurationUnit.SI);
259     }
260 
261     @Override
262     public SIScalar reciprocal()
263     {
264         return DoubleScalar.divide(Dimensionless.ONE, this);
265     }
266 
267 }