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-2025 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 = "2025-09-06T15:16:28.380798Z")
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 with a unit.
34       * @param value the double value, expressed in the given unit
35       * @param unit 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 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 based on a BASE unit value.
65       * @param value value in BASE units
66       * @return the new scalar with the BASE unit value
67       */
68      public static final Direction ofSI(final double value)
69      {
70          return new Direction(value, DirectionUnit.DEFAULT);
71      }
72  
73      /**
74       * Interpolate between two values. Note that the first value does not have to be smaller than the second.
75       * @param zero the value at a ratio of zero
76       * @param one the value at a ratio of one
77       * @param ratio the ratio between 0 and 1, inclusive
78       * @return a Direction at the given ratio between 0 and 1
79       */
80      public static Direction interpolate(final Direction zero, final Direction one, final double ratio)
81      {
82          Throw.when(ratio < 0.0 || ratio > 1.0, IllegalArgumentException.class,
83                  "ratio for interpolation should be between 0 and 1, but is %f", ratio);
84          return new Direction(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
85                  zero.getDisplayUnit());
86      }
87  
88      /**
89       * Return the maximum value of two absolute scalars.
90       * @param a1 the first scalar
91       * @param a2 the second scalar
92       * @return the maximum value of two absolute scalars
93       */
94      public static Direction max(final Direction a1, final Direction a2)
95      {
96          return a1.gt(a2) ? a1 : a2;
97      }
98  
99      /**
100      * Return the maximum value of more than two absolute scalars.
101      * @param a1 the first scalar
102      * @param a2 the second scalar
103      * @param an the other scalars
104      * @return the maximum value of more than two absolute scalars
105      */
106     public static Direction max(final Direction a1, final Direction a2, final Direction... an)
107     {
108         Direction maxa = a1.gt(a2) ? a1 : a2;
109         for (Direction a : an)
110         {
111             if (a.gt(maxa))
112             {
113                 maxa = a;
114             }
115         }
116         return maxa;
117     }
118 
119     /**
120      * Return the minimum value of two absolute scalars.
121      * @param a1 the first scalar
122      * @param a2 the second scalar
123      * @return the minimum value of two absolute scalars
124      */
125     public static Direction min(final Direction a1, final Direction a2)
126     {
127         return a1.lt(a2) ? a1 : a2;
128     }
129 
130     /**
131      * Return the minimum value of more than two absolute scalars.
132      * @param a1 the first scalar
133      * @param a2 the second scalar
134      * @param an the other scalars
135      * @return the minimum value of more than two absolute scalars
136      */
137     public static Direction min(final Direction a1, final Direction a2, final Direction... an)
138     {
139         Direction mina = a1.lt(a2) ? a1 : a2;
140         for (Direction a : an)
141         {
142             if (a.lt(mina))
143             {
144                 mina = a;
145             }
146         }
147         return mina;
148     }
149 
150     /**
151      * Returns a Direction representation of a textual representation of a value with a unit. The String representation that can
152      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
153      * allowed, but not required, between the value and the unit.
154      * @param text the textual representation to parse into a Direction
155      * @return the Scalar representation of the value in its unit
156      * @throws IllegalArgumentException when the text cannot be parsed
157      * @throws NullPointerException when the text argument is null
158      */
159     public static Direction valueOf(final String text)
160     {
161         Throw.whenNull(text, "Error parsing Direction: text to parse is null");
162         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Direction: empty text to parse");
163         try
164         {
165             NumberParser numberParser = new NumberParser().lenient().trailing();
166             double d = numberParser.parseDouble(text);
167             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
168             DirectionUnit unit = DirectionUnit.BASE.getUnitByAbbreviation(unitString);
169             Throw.when(unit == null, IllegalArgumentException.class, "Unit %s not found for quantity Direction", unitString);
170             return new Direction(d, unit);
171         }
172         catch (Exception exception)
173         {
174             throw new IllegalArgumentException(
175                     "Error parsing Direction from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
176                     exception);
177         }
178     }
179 
180     /**
181      * Returns a Direction based on a value and the textual representation of the unit, which can be localized.
182      * @param value the value to use
183      * @param unitString the textual representation of the unit
184      * @return the Scalar representation of the value in its unit
185      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
186      * @throws NullPointerException when the unitString argument is null
187      */
188     public static Direction of(final double value, final String unitString)
189     {
190         Throw.whenNull(unitString, "Error parsing Direction: unitString is null");
191         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Direction: empty unitString");
192         DirectionUnit unit = DirectionUnit.BASE.getUnitByAbbreviation(unitString);
193         Throw.when(unit == null, IllegalArgumentException.class, "Error parsing Direction with unit %s", unitString);
194         return new Direction(value, unit);
195     }
196 
197 }