View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.AngleUnit;
6   import org.djunits.unit.DirectionUnit;
7   import org.djunits.unit.Unit;
8   
9   /**
10   * Easy access methods for the Absolute Direction DoubleScalar. Instead of:
11   * 
12   * <pre>
13   * DoubleScalar.Abs&lt;DirectionUnit&gt; value = new DoubleScalar.Abs&lt;DirectionUnit&gt;(100.0, DirectionUnit.SI);
14   * </pre>
15   * 
16   * we can now write:
17   * 
18   * <pre>
19   * Direction value = new Direction(100.0, DirectionUnit.BASE);
20   * </pre>
21   * 
22   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
23   * used are compatible.
24   * <p>
25   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
26   * All rights reserved. <br>
27   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
28   * <p>
29   * $LastChangedDate: 2015-12-22 04:32:39 +0100 (Tue, 22 Dec 2015) $, @version $Revision: 180 $, by $Author: averbraeck $,
30   * initial version Sep 1, 2015 <br>
31   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33   */
34  public class Direction extends AbstractDoubleScalarAbs<DirectionUnit, Direction, AngleUnit, Angle>
35  {
36      /** */
37      private static final long serialVersionUID = 20150901L;
38  
39      /** constant with value zero. */
40      public static final Direction ZERO = new Direction(0.0, DirectionUnit.BASE);
41  
42      /**
43       * Construct Direction scalar.
44       * @param value double value
45       * @param unit unit for the double value
46       */
47      public Direction(final double value, final DirectionUnit unit)
48      {
49          super(value, unit);
50      }
51  
52      /**
53       * Construct Direction scalar.
54       * @param value Scalar from which to construct this instance
55       */
56      public Direction(final Direction value)
57      {
58          super(value);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final Direction instantiateAbs(final double value, final DirectionUnit unit)
64      {
65          return new Direction(value, unit);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final Angle instantiateRel(final double value, final AngleUnit unit)
71      {
72          return new Angle(value, unit);
73      }
74  
75      /**
76       * Construct %TypeAbsl% scalar.
77       * @param value double value in SI units
78       * @return the new scalar with the SI value
79       */
80      public static final Direction createSI(final double value)
81      {
82          return new Direction(value, DirectionUnit.BASE);
83      }
84  
85      /**
86       * Interpolate between two values.
87       * @param zero the low value
88       * @param one the high value
89       * @param ratio the ratio between 0 and 1, inclusive
90       * @return a Scalar at the ratio between
91       */
92      public static Direction interpolate(final Direction zero, final Direction one, final double ratio)
93      {
94          return new Direction(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
95      }
96  
97      /**
98       * Return the maximum value of two absolute scalars.
99       * @param a1 the first scalar
100      * @param a2 the second scalar
101      * @return the maximum value of two absolute scalars
102      */
103     public static Direction max(final Direction a1, final Direction a2)
104     {
105         return (a1.gt(a2)) ? a1 : a2;
106     }
107 
108     /**
109      * Return the maximum value of more than two absolute scalars.
110      * @param a1 the first scalar
111      * @param a2 the second scalar
112      * @param an the other scalars
113      * @return the maximum value of more than two absolute scalars
114      */
115     public static Direction max(final Direction a1, final Direction a2, final Direction... an)
116     {
117         Direction maxa = (a1.gt(a2)) ? a1 : a2;
118         for (Direction a : an)
119         {
120             if (a.gt(maxa))
121             {
122                 maxa = a;
123             }
124         }
125         return maxa;
126     }
127 
128     /**
129      * Return the minimum value of two absolute scalars.
130      * @param a1 the first scalar
131      * @param a2 the second scalar
132      * @return the minimum value of two absolute scalars
133      */
134     public static Direction min(final Direction a1, final Direction a2)
135     {
136         return (a1.lt(a2)) ? a1 : a2;
137     }
138 
139     /**
140      * Return the minimum value of more than two absolute scalars.
141      * @param a1 the first scalar
142      * @param a2 the second scalar
143      * @param an the other scalars
144      * @return the minimum value of more than two absolute scalars
145      */
146     public static Direction min(final Direction a1, final Direction a2, final Direction... an)
147     {
148         Direction mina = (a1.lt(a2)) ? a1 : a2;
149         for (Direction a : an)
150         {
151             if (a.lt(mina))
152             {
153                 mina = a;
154             }
155         }
156         return mina;
157     }
158 
159     /**
160      * Returns a Direction representation of a textual representation of a value with a unit. The String representation that can
161      * be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
162      * necessary, between the value and the unit.
163      * @param text String; the textual representation to parse into a Direction
164      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
165      * @throws IllegalArgumentException when the text cannot be parsed
166      */
167     public static Direction valueOf(final String text) throws IllegalArgumentException
168     {
169         if (text == null || text.length() == 0)
170         {
171             throw new IllegalArgumentException("Error parsing Direction -- null or empty argument");
172         }
173         Matcher matcher = NUMBER_PATTERN.matcher(text);
174         if (matcher.find())
175         {
176             int index = matcher.end();
177             try
178             {
179                 String unitString = text.substring(index).trim();
180                 String valueString = text.substring(0, index).trim();
181                 for (DirectionUnit unit : Unit.getUnits(DirectionUnit.class))
182                 {
183                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
184                     {
185                         double d = Double.parseDouble(valueString);
186                         return new Direction(d, unit);
187                     }
188                 }
189             }
190             catch (Exception exception)
191             {
192                 throw new IllegalArgumentException("Error parsing Direction from " + text, exception);
193             }
194         }
195         throw new IllegalArgumentException("Error parsing Direction from " + text);
196     }
197 
198 }