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.DirectionUnit;
7   import org.djunits.value.vdouble.scalar.base.DoubleScalarAbs;
8   import org.djutils.base.NumberParser;
9   import org.djutils.exceptions.Throw;
10  
11  import jakarta.annotation.Generated;
12  
13  /**
14   * Easy access methods for the Absolute Direction DoubleScalar.
15   * <p>
16   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
17   * All rights reserved. <br>
18   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
19   * </p>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
22   */
23  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2023-07-23T14:06:38.224104100Z")
24  public class Direction extends DoubleScalarAbs<DirectionUnit, Direction, AngleUnit, Angle>
25  {
26      /** */
27      private static final long serialVersionUID = 20150901L;
28  
29      /** Constant with value zero. */
30      public static final Direction ZERO = new Direction(0.0, DirectionUnit.DEFAULT);
31  
32      /**
33       * Construct Direction scalar.
34       * @param value double; value
35       * @param unit DirectionUnit; unit for the double value
36       */
37      public Direction(final double value, final DirectionUnit unit)
38      {
39          super(value, unit);
40      }
41  
42      /**
43       * Construct Direction scalar.
44       * @param value Direction; Scalar from which to construct this instance
45       */
46      public Direction(final Direction value)
47      {
48          super(value);
49      }
50  
51      @Override
52      public final Direction instantiateAbs(final double value, final DirectionUnit unit)
53      {
54          return new Direction(value, unit);
55      }
56  
57      @Override
58      public final Angle instantiateRel(final double value, final AngleUnit unit)
59      {
60          return new Angle(value, unit);
61      }
62  
63      /**
64       * Construct Direction scalar.
65       * @param value double; value in SI units
66       * @return Direction; the new scalar with the SI value
67       */
68      public static final Direction instantiateSI(final double value)
69      {
70          return new Direction(value, DirectionUnit.DEFAULT);
71      }
72  
73      /**
74       * Interpolate between two values.
75       * @param zero Direction; the low value
76       * @param one Direction; the high value
77       * @param ratio double; the ratio between 0 and 1, inclusive
78       * @return Direction; a Scalar at the ratio between
79       */
80      public static Direction interpolate(final Direction zero, final Direction one, final double ratio)
81      {
82          return new Direction(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
83                  zero.getDisplayUnit());
84      }
85  
86      /**
87       * Return the maximum value of two absolute scalars.
88       * @param a1 Direction; the first scalar
89       * @param a2 Direction; the second scalar
90       * @return Direction; the maximum value of two absolute scalars
91       */
92      public static Direction max(final Direction a1, final Direction a2)
93      {
94          return a1.gt(a2) ? a1 : a2;
95      }
96  
97      /**
98       * Return the maximum value of more than two absolute scalars.
99       * @param a1 Direction; the first scalar
100      * @param a2 Direction; the second scalar
101      * @param an Direction...; the other scalars
102      * @return the maximum value of more than two absolute scalars
103      */
104     public static Direction max(final Direction a1, final Direction a2, final Direction... an)
105     {
106         Direction maxa = a1.gt(a2) ? a1 : a2;
107         for (Direction a : an)
108         {
109             if (a.gt(maxa))
110             {
111                 maxa = a;
112             }
113         }
114         return maxa;
115     }
116 
117     /**
118      * Return the minimum value of two absolute scalars.
119      * @param a1 Direction; the first scalar
120      * @param a2 Direction; the second scalar
121      * @return the minimum value of two absolute scalars
122      */
123     public static Direction min(final Direction a1, final Direction a2)
124     {
125         return a1.lt(a2) ? a1 : a2;
126     }
127 
128     /**
129      * Return the minimum value of more than two absolute scalars.
130      * @param a1 Direction; the first scalar
131      * @param a2 Direction; the second scalar
132      * @param an Direction...; the other scalars
133      * @return the minimum value of more than two absolute scalars
134      */
135     public static Direction min(final Direction a1, final Direction a2, final Direction... an)
136     {
137         Direction mina = a1.lt(a2) ? a1 : a2;
138         for (Direction a : an)
139         {
140             if (a.lt(mina))
141             {
142                 mina = a;
143             }
144         }
145         return mina;
146     }
147 
148     /**
149      * Returns a Direction representation of a textual representation of a value with a unit. The String representation that can
150      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
151      * allowed, but not required, between the value and the unit.
152      * @param text String; the textual representation to parse into a Direction
153      * @return Direction; the Scalar representation of the value in its unit
154      * @throws IllegalArgumentException when the text cannot be parsed
155      * @throws NullPointerException when the text argument is null
156      */
157     public static Direction valueOf(final String text)
158     {
159         Throw.whenNull(text, "Error parsing Direction: text to parse is null");
160         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Direction: empty text to parse");
161         try
162         {
163             NumberParser numberParser = new NumberParser().lenient().trailing();
164             double d = numberParser.parseDouble(text);
165             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
166             DirectionUnit unit = DirectionUnit.BASE.getUnitByAbbreviation(unitString);
167             if (unit == null)
168                 throw new IllegalArgumentException("Unit " + unitString + " not found");
169             return new Direction(d, unit);
170         }
171         catch (Exception exception)
172         {
173             throw new IllegalArgumentException(
174                     "Error parsing Direction from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
175                     exception);
176         }
177     }
178 
179     /**
180      * Returns a Direction based on a value and the textual representation of the unit, which can be localized.
181      * @param value double; the value to use
182      * @param unitString String; the textual representation of the unit
183      * @return Direction; the Scalar representation of the value in its unit
184      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
185      * @throws NullPointerException when the unitString argument is null
186      */
187     public static Direction of(final double value, final String unitString)
188     {
189         Throw.whenNull(unitString, "Error parsing Direction: unitString is null");
190         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Direction: empty unitString");
191         DirectionUnit unit = DirectionUnit.BASE.getUnitByAbbreviation(unitString);
192         if (unit != null)
193         {
194             return new Direction(value, unit);
195         }
196         throw new IllegalArgumentException("Error parsing Direction with unit " + unitString);
197     }
198 
199 }