View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import org.djunits.unit.AngleSolidUnit;
6   import org.djunits.unit.DimensionlessUnit;
7   import org.djunits.unit.Unit;
8   
9   /**
10   * Easy access methods for the AngleSolid FloatScalar, which is relative by definition. An example is Speed. Instead of:
11   * 
12   * <pre>
13   * FloatScalar.Rel&lt;AngleSolidUnit&gt; value = new FloatScalar.Rel&lt;AngleSolidUnit&gt;(100.0, AngleSolidUnit.SI);
14   * </pre>
15   * 
16   * we can now write:
17   * 
18   * <pre>
19   * FloatAngleSolid value = new FloatAngleSolid(100.0, AngleSolidUnit.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. All rights reserved. <br>
26   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
27   * <p>
28   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
29   * initial version Sep 5, 2015 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   */
33  public class FloatAngleSolid extends AbstractFloatScalarRel<AngleSolidUnit, FloatAngleSolid>
34  {
35      /** */
36      private static final long serialVersionUID = 20150901L;
37  
38      /** constant with value zero. */
39      public static final FloatAngleSolid ZERO = new FloatAngleSolid(0.0f, AngleSolidUnit.SI);
40  
41      /** constant with value NaN. */
42      @SuppressWarnings("checkstyle:constantname")
43      public static final FloatAngleSolid NaN = new FloatAngleSolid(Float.NaN, AngleSolidUnit.SI);
44  
45      /** constant with value POSITIVE_INFINITY. */
46      public static final FloatAngleSolid POSITIVE_INFINITY = new FloatAngleSolid(Float.POSITIVE_INFINITY, AngleSolidUnit.SI);
47  
48      /** constant with value NEGATIVE_INFINITY. */
49      public static final FloatAngleSolid NEGATIVE_INFINITY = new FloatAngleSolid(Float.NEGATIVE_INFINITY, AngleSolidUnit.SI);
50  
51      /** constant with value MAX_VALUE. */
52      public static final FloatAngleSolid POS_MAXVALUE = new FloatAngleSolid(Float.MAX_VALUE, AngleSolidUnit.SI);
53  
54      /** constant with value -MAX_VALUE. */
55      public static final FloatAngleSolid NEG_MAXVALUE = new FloatAngleSolid(-Float.MAX_VALUE, AngleSolidUnit.SI);
56  
57      /**
58       * Construct FloatAngleSolid scalar.
59       * @param value float value
60       * @param unit unit for the float value
61       */
62      public FloatAngleSolid(final float value, final AngleSolidUnit unit)
63      {
64          super(value, unit);
65      }
66  
67      /**
68       * Construct FloatAngleSolid scalar.
69       * @param value Scalar from which to construct this instance
70       */
71      public FloatAngleSolid(final FloatAngleSolid value)
72      {
73          super(value);
74      }
75  
76      /**
77       * Construct FloatAngleSolid scalar using a double value.
78       * @param value double value
79       * @param unit unit for the resulting float value
80       */
81      public FloatAngleSolid(final double value, final AngleSolidUnit unit)
82      {
83          super((float) value, unit);
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public final FloatAngleSolid instantiateRel(final float value, final AngleSolidUnit unit)
89      {
90          return new FloatAngleSolid(value, unit);
91      }
92  
93      /**
94       * Construct FloatAngleSolid scalar.
95       * @param value float value in SI units
96       * @return the new scalar with the SI value
97       */
98      public static final FloatAngleSolid createSI(final float value)
99      {
100         return new FloatAngleSolid(value, AngleSolidUnit.SI);
101     }
102 
103     /**
104      * Interpolate between two values.
105      * @param zero the low value
106      * @param one the high value
107      * @param ratio the ratio between 0 and 1, inclusive
108      * @return a Scalar at the ratio between
109      */
110     public static FloatAngleSolid interpolate(final FloatAngleSolid zero, final FloatAngleSolid one, final float ratio)
111     {
112         return new FloatAngleSolid(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
113     }
114 
115     /**
116      * Return the maximum value of two relative scalars.
117      * @param r1 the first scalar
118      * @param r2 the second scalar
119      * @return the maximum value of two relative scalars
120      */
121     public static FloatAngleSolid max(final FloatAngleSolid r1, final FloatAngleSolid r2)
122     {
123         return (r1.gt(r2)) ? r1 : r2;
124     }
125 
126     /**
127      * Return the maximum value of more than two relative scalars.
128      * @param r1 the first scalar
129      * @param r2 the second scalar
130      * @param rn the other scalars
131      * @return the maximum value of more than two relative scalars
132      */
133     public static FloatAngleSolid max(final FloatAngleSolid r1, final FloatAngleSolid r2, final FloatAngleSolid... rn)
134     {
135         FloatAngleSolid maxr = (r1.gt(r2)) ? r1 : r2;
136         for (FloatAngleSolid r : rn)
137         {
138             if (r.gt(maxr))
139             {
140                 maxr = r;
141             }
142         }
143         return maxr;
144     }
145 
146     /**
147      * Return the minimum value of two relative scalars.
148      * @param r1 the first scalar
149      * @param r2 the second scalar
150      * @return the minimum value of two relative scalars
151      */
152     public static FloatAngleSolid min(final FloatAngleSolid r1, final FloatAngleSolid r2)
153     {
154         return (r1.lt(r2)) ? r1 : r2;
155     }
156 
157     /**
158      * Return the minimum value of more than two relative scalars.
159      * @param r1 the first scalar
160      * @param r2 the second scalar
161      * @param rn the other scalars
162      * @return the minimum value of more than two relative scalars
163      */
164     public static FloatAngleSolid min(final FloatAngleSolid r1, final FloatAngleSolid r2, final FloatAngleSolid... rn)
165     {
166         FloatAngleSolid minr = (r1.lt(r2)) ? r1 : r2;
167         for (FloatAngleSolid r : rn)
168         {
169             if (r.lt(minr))
170             {
171                 minr = r;
172             }
173         }
174         return minr;
175     }
176 
177     /**
178      * Returns a FloatAngleSolid representation of a textual representation of a value with a unit. The String representation
179      * that can be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are
180      * allowed, but not necessary, between the value and the unit.
181      * @param text String; the textual representation to parse into a FloatAngleSolid
182      * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
183      * @throws IllegalArgumentException when the text cannot be parsed
184      */
185     public static FloatAngleSolid valueOf(final String text) throws IllegalArgumentException
186     {
187         if (text == null || text.length() == 0)
188         {
189             throw new IllegalArgumentException("Error parsing FloatAngleSolid -- null or empty argument");
190         }
191         Matcher matcher = NUMBER_PATTERN.matcher(text);
192         if (matcher.find())
193         {
194             int index = matcher.end();
195             try
196             {
197                 String unitString = text.substring(index).trim();
198                 String valueString = text.substring(0, index).trim();
199                 for (AngleSolidUnit unit : Unit.getUnits(AngleSolidUnit.class))
200                 {
201                     if (unit.getDefaultLocaleTextualRepresentations().contains(unitString))
202                     {
203                         float f = Float.parseFloat(valueString);
204                         return new FloatAngleSolid(f, unit);
205                     }
206                 }
207             }
208             catch (Exception exception)
209             {
210                 throw new IllegalArgumentException("Error parsing FloatAngleSolid from " + text, exception);
211             }
212         }
213         throw new IllegalArgumentException("Error parsing FloatAngleSolid from " + text);
214     }
215 
216     /**
217      * Calculate the division of FloatAngleSolid and FloatAngleSolid, which results in a FloatDimensionless scalar.
218      * @param v FloatAngleSolid scalar
219      * @return FloatDimensionless scalar as a division of FloatAngleSolid and FloatAngleSolid
220      */
221     public final FloatDimensionless divideBy(final FloatAngleSolid v)
222     {
223         return new FloatDimensionless(this.si / v.si, DimensionlessUnit.SI);
224     }
225 
226 }