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