View Javadoc
1   package org.djunits.value.vfloat.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 Direction FloatScalar. Instead of:
11   * 
12   * <pre>
13   * FloatScalar.Abs&lt;DirectionUnit&gt; value = new FloatScalar.Abs&lt;DirectionUnit&gt;(100.0, DirectionUnit.SI);
14   * </pre>
15   * 
16   * we can now write:
17   * 
18   * <pre>
19   * FloatDirection value = new FloatDirection(100.0, DirectionUnit.SI);
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: 2019-02-27 23:44:43 +0100 (Wed, 27 Feb 2019) $, @version $Revision: 333 $, 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 FloatDirection extends AbstractFloatScalarAbs<DirectionUnit, FloatDirection, AngleUnit, FloatAngle>
35  {
36      /** */
37      private static final long serialVersionUID = 20150901L;
38  
39      /** constant with value zero. */
40      public static final FloatDirection ZERO = new FloatDirection(0.0f, DirectionUnit.BASE);
41  
42      /**
43       * Construct FloatDirection scalar.
44       * @param value float value
45       * @param unit unit for the float value
46       */
47      public FloatDirection(final float value, final DirectionUnit unit)
48      {
49          super(value, unit);
50      }
51  
52      /**
53       * Construct FloatDirection scalar using a double value.
54       * @param value double value
55       * @param unit unit for the resulting float value
56       */
57      public FloatDirection(final double value, final DirectionUnit unit)
58      {
59          super((float) value, unit);
60      }
61  
62      /**
63       * Construct FloatDirection scalar.
64       * @param value Scalar from which to construct this instance
65       */
66      public FloatDirection(final FloatDirection value)
67      {
68          super(value);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public final FloatDirection instantiateAbs(final float value, final DirectionUnit unit)
74      {
75          return new FloatDirection(value, unit);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final FloatAngle instantiateRel(final float value, final AngleUnit unit)
81      {
82          return new FloatAngle(value, unit);
83      }
84  
85      /**
86       * Construct FloatDirection scalar.
87       * @param value float value in BASE units
88       * @return the new scalar with the BASE value
89       */
90      public static final FloatDirection createSI(final float value)
91      {
92          return new FloatDirection(value, DirectionUnit.BASE);
93      }
94  
95      /**
96       * Interpolate between two values.
97       * @param zero the low value
98       * @param one the high value
99       * @param ratio the ratio between 0 and 1, inclusive
100      * @return a Scalar at the ratio between
101      */
102     public static FloatDirection interpolate(final FloatDirection zero, final FloatDirection one, final float ratio)
103     {
104         return new FloatDirection(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
105     }
106 
107     /**
108      * Return the maximum value of two absolute scalars.
109      * @param a1 the first scalar
110      * @param a2 the second scalar
111      * @return the maximum value of two absolute scalars
112      */
113     public static FloatDirection max(final FloatDirection a1, final FloatDirection a2)
114     {
115         return (a1.gt(a2)) ? a1 : a2;
116     }
117 
118     /**
119      * Return the maximum value of more than two absolute scalars.
120      * @param a1 the first scalar
121      * @param a2 the second scalar
122      * @param an the other scalars
123      * @return the maximum value of more than two absolute scalars
124      */
125     public static FloatDirection max(final FloatDirection a1, final FloatDirection a2, final FloatDirection... an)
126     {
127         FloatDirection maxa = (a1.gt(a2)) ? a1 : a2;
128         for (FloatDirection a : an)
129         {
130             if (a.gt(maxa))
131             {
132                 maxa = a;
133             }
134         }
135         return maxa;
136     }
137 
138     /**
139      * Return the minimum value of two absolute scalars.
140      * @param a1 the first scalar
141      * @param a2 the second scalar
142      * @return the minimum value of two absolute scalars
143      */
144     public static FloatDirection min(final FloatDirection a1, final FloatDirection a2)
145     {
146         return (a1.lt(a2)) ? a1 : a2;
147     }
148 
149     /**
150      * Return the minimum value of more than two absolute scalars.
151      * @param a1 the first scalar
152      * @param a2 the second scalar
153      * @param an the other scalars
154      * @return the minimum value of more than two absolute scalars
155      */
156     public static FloatDirection min(final FloatDirection a1, final FloatDirection a2, final FloatDirection... an)
157     {
158         FloatDirection mina = (a1.lt(a2)) ? a1 : a2;
159         for (FloatDirection a : an)
160         {
161             if (a.lt(mina))
162             {
163                 mina = a;
164             }
165         }
166         return mina;
167     }
168 
169     /**
170      * Returns a FloatDirection representation of a textual representation of a value with a unit. The String representation
171      * that can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are
172      * allowed, but not necessary, between the value and the unit.
173      * @param text String; the textual representation to parse into a FloatDirection
174      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
175      * @throws IllegalArgumentException when the text cannot be parsed
176      */
177     public static FloatDirection valueOf(final String text) throws IllegalArgumentException
178     {
179         if (text == null || text.length() == 0)
180         {
181             throw new IllegalArgumentException("Error parsing FloatDirection -- null or empty argument");
182         }
183         Matcher matcher = NUMBER_PATTERN.matcher(text);
184         if (matcher.find())
185         {
186             int index = matcher.end();
187             try
188             {
189                 String unitString = text.substring(index).trim();
190                 String valueString = text.substring(0, index).trim();
191                 for (DirectionUnit unit : Unit.getUnits(DirectionUnit.class))
192                 {
193                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
194                     {
195                         float f = Float.parseFloat(valueString);
196                         return new FloatDirection(f, unit);
197                     }
198                 }
199             }
200             catch (Exception exception)
201             {
202                 throw new IllegalArgumentException("Error parsing FloatDirection from " + text, exception);
203             }
204         }
205         throw new IllegalArgumentException("Error parsing FloatDirection from " + text);
206     }
207 
208 }